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

current_events[R]

@return [Array<Hash>] List of current events to execute.

event_time[R]

@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}
start_time[RW]

@return [Time] The Time to start looking for an event.

Any event earlier than this will be discarded!

Public Class Methods

new() click to toggle source
# 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

add_event(event) click to toggle source

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
execute!() click to toggle source

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
has_events?() click to toggle source

@return [true, false] Were any events found?

# File lib/tef/Sequencing/EventCollector.rb, line 152
def has_events?
        !@current_events.empty?
end
offset_collector(offset, slope) click to toggle source

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() click to toggle source

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
wait_until_event() click to toggle source

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