class TEF::Sequencing::EventCollector
Event Collector class
This class provides the means to efficiently fetch the next event from a list of {BaseSequence}s, and is mainly meant for internal purposes. It is created by {Player}
Attributes
@return [Array<Hash>] List of current events to execute.
@return [nil, Time] The Time of the current event, or nil if
there is no event. Any event later than this will be discarded. Any event equal to this time will be appended to {#current_events}
@return [Time] The Time to start looking for an event.
Any event earlier than this will be discarded!
Public Class Methods
# File lib/tef/Sequencing/EventCollector.rb, line 122 def initialize() @current_events = [] @start_time = Time.at(0); @event_time = nil; init_x_log("Sequence Player") end
Public Instance Methods
Internal function to add an event. The event will be discarded if it is earlier than or equal to start time, or later than the event time. It if it is earlier than event time it will set the new event time and set the event list to [event], else append the event to the event list.
# File lib/tef/Sequencing/EventCollector.rb, line 136 def add_event(event) event = event.clone return if event[:time] <= @start_time return if (!@event_time.nil?) && (event[:time] > @event_time) return unless event[:code].is_a? Proc if (!@event_time.nil?) && (event[:time] == @event_time) @current_events << event else @current_events = [event] @event_time = event[:time] end end
Wait until the next event and then execute the code of each event.
# File lib/tef/Sequencing/EventCollector.rb, line 175 def execute! return unless has_events? wait_until_event @current_events.each do |event| event[:code].call() end @start_time = @event_time restart(); end
@return [true, false] Were any events found?
# File lib/tef/Sequencing/EventCollector.rb, line 152 def has_events? !@current_events.empty? end
Generate a {OffsetCollector} This is mainly an internal function used by {BaseSequence} to provide an {OffsetCollector}. It converts between the global time-frame used by this collector, and the local timeframes of each sub-sequence.
# File lib/tef/Sequencing/EventCollector.rb, line 200 def offset_collector(offset, slope) OffsetCollector.new(self, offset, slope); end
Restart this collector. Will clear {#current_events} and {#event_time}
# File lib/tef/Sequencing/EventCollector.rb, line 190 def restart() @current_events = [] @event_time = nil; end
This function will try to wait until {#event_time}. If no event was found it will return immediately, and Thread.run() can be used to prematurely end the wait.
# File lib/tef/Sequencing/EventCollector.rb, line 159 def wait_until_event return unless has_events? t_diff = @event_time - Time.now(); if t_diff < -0.5 x_logf('Sequence long overdue!') elsif t_diff < -0.1 x_logw('Sequencing overdue') end sleep t_diff if t_diff > 0 end