4 attr_accessor :condition, :at
6 def initialize(condition, interval)
7 self.condition = condition
8 self.at = Time.now.to_i + interval
15 attr_reader :events, :timer
27 # Start the scheduler loop to handle events
32 @timer = Thread.new do
34 # get the current time
37 # iterate over each event and trigger any that are due
38 @events.each do |event|
49 # sleep until next check
55 # Create and register a new TimerEvent with the given parameters
56 def schedule(condition, interval = condition.interval)
58 @events << TimerEvent.new(condition, interval)
59 @events.sort! { |x, y| x.at <=> y.at }
63 # Remove any TimerEvents for the given condition
64 def unschedule(condition)
66 @events.reject! { |x| x.condition == condition }
71 Hub.trigger(event.condition)
74 # Join the timer thread