class Listen::Event::Processor
Attributes
config[R]
Public Class Methods
new(config, reasons)
click to toggle source
# File lib/listen/event/processor.rb, line 8 def initialize(config, reasons) @config = config @listener = config.listener @reasons = reasons _reset_no_unprocessed_events end
Public Instance Methods
loop_for(latency)
click to toggle source
TODO: implement this properly instead of checking the state at arbitrary points in time
# File lib/listen/event/processor.rb, line 17 def loop_for(latency) @latency = latency loop do event = _wait_until_events _check_stopped _wait_until_events_calm_down _wait_until_no_longer_paused _process_changes(event) end rescue Stopped Listen.logger.debug('Processing stopped') end
Private Instance Methods
_check_stopped()
click to toggle source
# File lib/listen/event/processor.rb, line 55 def _check_stopped if @listener.stopped? _flush_wakeup_reasons raise Stopped end end
_deadline()
click to toggle source
# File lib/listen/event/processor.rb, line 82 def _deadline @_remember_time_of_first_unprocessed_event + @latency end
_flush_wakeup_reasons() { |reason| ... }
click to toggle source
# File lib/listen/event/processor.rb, line 94 def _flush_wakeup_reasons until @reasons.empty? reason = @reasons.pop yield reason if block_given? end end
_process_changes(event)
click to toggle source
for easier testing without sleep loop
# File lib/listen/event/processor.rb, line 102 def _process_changes(event) _reset_no_unprocessed_events changes = [event] changes << config.event_queue.pop until config.event_queue.empty? return unless config.callable? hash = config.optimize_changes(changes) result = [hash[:modified], hash[:added], hash[:removed]] return if result.all?(&:empty?) block_start = MonotonicTime.now exception_note = " (exception)" ::Listen::Thread.rescue_and_log('_process_changes') do config.call(*result) exception_note = nil end Listen.logger.debug "Callback#{exception_note} took #{MonotonicTime.now - block_start} sec" end
_remember_time_of_first_unprocessed_event()
click to toggle source
# File lib/listen/event/processor.rb, line 74 def _remember_time_of_first_unprocessed_event @_remember_time_of_first_unprocessed_event ||= MonotonicTime.now end
_reset_no_unprocessed_events()
click to toggle source
# File lib/listen/event/processor.rb, line 78 def _reset_no_unprocessed_events @_remember_time_of_first_unprocessed_event = nil end
_sleep(seconds)
click to toggle source
# File lib/listen/event/processor.rb, line 62 def _sleep(seconds) _check_stopped config.sleep(seconds) _check_stopped _flush_wakeup_reasons do |reason| if reason == :event && !@listener.paused? _remember_time_of_first_unprocessed_event end end end
_wait_until_events()
click to toggle source
blocks until event is popped returns the event or ‘nil` when the event_queue is closed
# File lib/listen/event/processor.rb, line 88 def _wait_until_events config.event_queue.pop.tap do |_event| @_remember_time_of_first_unprocessed_event ||= MonotonicTime.now end end
_wait_until_events_calm_down()
click to toggle source
# File lib/listen/event/processor.rb, line 36 def _wait_until_events_calm_down loop do now = MonotonicTime.now # Assure there's at least latency between callbacks to allow # for accumulating changes diff = _deadline - now break if diff <= 0 # give events a bit of time to accumulate so they can be # compressed/optimized _sleep(diff) end end
_wait_until_no_longer_paused()
click to toggle source
# File lib/listen/event/processor.rb, line 51 def _wait_until_no_longer_paused @listener.wait_for_state(*(Listener.states.keys - [:paused])) end