Sha256: 74c12e98b5f92cba2c4c66af53346a78f4f7bad58486e56bbaf634afe4593c56

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

require 'rb-fsevent'

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

      attr_reader :path, :callback, :suspended, :fsevent

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

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

      # def suspended?
      #   !suspended.empty?
      # end

      # def suspend
      #   suspended << true
      # end

      # def unsuspend
      #   suspended.pop
      # end

      private

        def watch
          fsevent.watch(path, file: file?, latency: LATENCY, no_defer: NO_DEFER) do |paths|
            unless git_dir?(paths)
              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?(paths)
          paths.size == 1 && File.basename(paths.first) == '.git'
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
space-0.0.6 lib/space/source/watch.rb