Sha256: 31f64254c9b78c20b483d8315287854b443b6d17e60565da6f0c95fa959a3a52

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

require "rb-fsevent"

module VagrantPlugins
  module RsyncBlitz
    class ListenOSX
      def initialize(paths, ignores, latency, logger, callback)
        @paths = paths
        @ignores = ignores
        @latency = latency
        @options = {
          # We set this to a small value to ensure that we can coalesce the
          # events together to prevent rsyncing too often under heavy write
          # load.
          :latency => 0.1,
          :no_defer => false,
        }
        @logger = logger
        @callback = callback
      end

      def run
        @logger.info("Listening via: rb-fsevent on Mac OS X.")
        changes = Queue.new

        fsevent = FSEvent.new
        fsevent.watch @paths.keys, @options do |directories|
          directories.each { |d| changes << d }
        end
        Thread.new { fsevent.run }

        loop do
          directories = Set.new
          begin
            loop do
              @logger.info("Starting the timeout at #{Time.now.to_s}.")
              change = Timeout::timeout(@latency) {
                changes.pop
              }
              directories << change unless change.nil?
            end
          rescue Timeout::Error, ThreadError
            @logger.info("Breaking out of the loop at #{Time.now.to_s}.")
          end

          @logger.info("Detected changes to #{directories.inspect}.") unless directories.empty?

          @callback.call(@paths, @ignores, directories) unless directories.empty?
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vagrant-rsync-blitz-2.0.0 lib/vagrant-rsync-blitz/listen/listenosx.rb