Sha256: fcebd976d1154c55c0b6fe49b598f5211fa0b25aa7aa3e93b792dd6f0a9ddd7c

Contents?: true

Size: 1.15 KB

Versions: 3

Compression:

Stored size: 1.15 KB

Contents

module Fswatch
  class Watcher

    def initialize(options = {})
      @event_handlers = []

      options[:callback] = self.method(:_event_handler)

      @monitor = Fswatch::Monitor.new(options)
    end

    def watch(options = {}, &block)
      raise 'Invalid handler' unless block

      options = nil if options.empty? # reduce memory usage

      @event_handlers << [block, options]

      return nil
    end

    def start!
      raise 'Already running!' if running?

      @thread = Thread.new { @monitor.start }
    end

    def stop!
      raise 'Not running!' if running?

      @monitor.stop
    end

    def running?
      @monitor.running?
    end

    private

    def _event_handler(events)
      events.each do |event|
        filename, timestamp, *event_flags = event

        @event_handlers.each do |handler, options|
          if options
            # match filename
            next if options[:match] && !options[:match].match?(filename)

            # event
            next if options[:on] && (options[:on] & event_flags).empty?
          end

          handler.call(filename, timestamp, event_flags, options)
        end
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fswatch-rb-0.1.3 lib/fswatch/watcher.rb
fswatch-rb-0.1.2 lib/fswatch/watcher.rb
fswatch-rb-0.1.1 lib/fswatch/watcher.rb