class SassListen::Event::Processor

Attributes

config[R]

Public Class Methods

new(config, reasons) click to toggle source
# File lib/sass-listen/event/processor.rb, line 4
def initialize(config, reasons)
  @config = config
  @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/sass-listen/event/processor.rb, line 12
def loop_for(latency)
  @latency = latency

  loop do
    _wait_until_events
    _wait_until_events_calm_down
    _wait_until_no_longer_paused
    _process_changes
  end
rescue Stopped
  SassListen::Logger.debug('Processing stopped')
end

Private Instance Methods

_check_stopped() click to toggle source
# File lib/sass-listen/event/processor.rb, line 50
def _check_stopped
  return unless config.stopped?

  _flush_wakeup_reasons
  raise Stopped
end
_deadline() click to toggle source
# File lib/sass-listen/event/processor.rb, line 78
def _deadline
  @first_unprocessed_event_time + @latency
end
_flush_wakeup_reasons() { |reason| ... } click to toggle source
# File lib/sass-listen/event/processor.rb, line 88
def _flush_wakeup_reasons
  reasons = @reasons
  until reasons.empty?
    reason = reasons.pop
    yield reason if block_given?
  end
end
_process_changes() click to toggle source

for easier testing without sleep loop

# File lib/sass-listen/event/processor.rb, line 101
def _process_changes
  _reset_no_unprocessed_events

  changes = []
  changes << config.event_queue.pop until config.event_queue.empty?

  callable = config.callable?
  return unless callable

  hash = config.optimize_changes(changes)
  result = [hash[:modified], hash[:added], hash[:removed]]
  return if result.all?(&:empty?)

  block_start = _timestamp
  config.call(*result)
  SassListen::Logger.debug "Callback took #{_timestamp - block_start} sec"
end
_remember_time_of_first_unprocessed_event() click to toggle source
# File lib/sass-listen/event/processor.rb, line 70
def _remember_time_of_first_unprocessed_event
  @first_unprocessed_event_time ||= _timestamp
end
_reset_no_unprocessed_events() click to toggle source
# File lib/sass-listen/event/processor.rb, line 74
def _reset_no_unprocessed_events
  @first_unprocessed_event_time = nil
end
_sleep(_local_reason, *args) click to toggle source
# File lib/sass-listen/event/processor.rb, line 57
def _sleep(_local_reason, *args)
  _check_stopped
  sleep_duration = config.sleep(*args)
  _check_stopped

  _flush_wakeup_reasons do |reason|
    next unless reason == :event
    _remember_time_of_first_unprocessed_event unless config.paused?
  end

  sleep_duration
end
_timestamp() click to toggle source
# File lib/sass-listen/event/processor.rb, line 96
def _timestamp
  config.timestamp
end
_wait_until_events() click to toggle source
# File lib/sass-listen/event/processor.rb, line 82
def _wait_until_events
  # TODO: long sleep may not be a good idea?
  _sleep(:waiting_for_events) while config.event_queue.empty?
  @first_unprocessed_event_time ||= _timestamp
end
_wait_until_events_calm_down() click to toggle source
# File lib/sass-listen/event/processor.rb, line 30
def _wait_until_events_calm_down
  loop do
    now = _timestamp

    # 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(:waiting_until_latency, diff)
  end
end
_wait_until_no_longer_paused() click to toggle source
# File lib/sass-listen/event/processor.rb, line 45
def _wait_until_no_longer_paused
  # TODO: may not be a good idea?
  _sleep(:waiting_for_unpause) while config.paused?
end