Sha256: 672a8eb81e988d89d7289ffd705bbde23f865753c3f9d6f2837b08a97b69fc08

Contents?: true

Size: 1009 Bytes

Versions: 2

Compression:

Stored size: 1009 Bytes

Contents

require 'rb-fsevent'

module Space
  module Shell
    class Watch
      LATENCY = 0
      NO_DEFER = FALSE

      attr_reader :path, :callback, :mutex

      def initialize(path, &block)
        @path = File.expand_path(path)
        @callback = block
        @mutex = Mutex.new
        self
      end

      def run
        Thread.new do
          App.logger.debug("WATCHING #{path}")
          watch
        end
      end

      private

        def watch
          fsevent.watch(path, file: file?, latency: LATENCY, no_defer: NO_DEFER) do |paths|
            App.logger.debug("WATCH triggered: #{paths.inspect}")
            fsevent.stop
            mutex.synchronize do
              callback.call(paths)
            end
            fsevent.run
          end
          fsevent.run
        rescue Exception => e
          puts e.message, e.backtrace
        end

        def file?
          File.file?(path)
        end

        def fsevent
          @fsevent ||= FSEvent.new
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
space-0.0.5 lib/space/shell/watch.rb
space-0.0.4 lib/space/shell/watch.rb