Sha256: 2b05da9d7447a483392a35faea6089ae783c3b1820e1f34f7a6de36edbbac6a2

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

require 'rb-fsevent'

module Space
  module Source
    class Watch
      LATENCY = 0.1
      NO_DEFER = FALSE

      attr_reader :path, :callback, :fsevent

      def initialize(path, &block)
        @path = File.expand_path(path)
        @callback = block
        @fsevent = FSEvent.new
      end

      def run
        @thread = Thread.new do
          log "WATCHING #{path}"
          watch
        end
        self
      end

      private

        def watch
          fsevent.watch(path, file: file?, latency: LATENCY, no_defer: NO_DEFER) do |paths|
            # git touches the .git dir on `git status` as we use this command
            # internally we need to ignore this. as no other relevant action
            # only touches the .git dir, we can just skip it.
            paths.reject!(&method(:git_dir?))

            unless paths.empty?
              log "=> WATCH triggered: #{paths.inspect}"
              fsevent.stop
              callback.call(paths)
              fsevent.run
            end
          end
          fsevent.run
        rescue Exception => e
          puts e.message, e.backtrace
        end

        def file?
          File.file?(path)
        end

        def git_dir?(path)
          File.basename(path) == '.git'
        end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
space-0.0.9 lib/space/source/watch.rb
space-0.0.8 lib/space/source/watch.rb
space-0.0.7 lib/space/source/watch.rb