3 # timer event, something to do and when/how often to do it
6 # when this action is due next (updated by tick())
9 # is this action blocked? if so it won't be run
10 attr_accessor :blocked
12 # period:: how often (seconds) to run the action
13 # data:: optional data to pass to the proc
14 # once:: optional, if true, this action will be run once then removed
15 # func:: associate a block to be called to perform the action
18 def initialize(period, data=nil, once=false, &func)
29 diff = Time.new - @last_tick
35 "#<#{self.class}:#{@period}s:#{@once ? 'once' : 'repeat'}>"
42 # run the action by calling its proc
45 # really short duration timers can overrun and leave @in negative,
46 # for these we set @in to @period
47 @in = @period if @in <= 0
57 # timer handler, manage multiple Action objects, calling them when required.
58 # The timer must be ticked by whatever controls it, i.e. regular calls to
59 # tick() at whatever granularity suits your application's needs.
61 # Alternatively you can call run(), and the timer will spawn a thread and
62 # tick itself, intelligently shutting down the thread if there are no
65 def initialize(granularity = 0.1)
66 @granularity = granularity
70 @should_be_running = false
75 # period:: how often (seconds) to run the action
76 # data:: optional data to pass to the action's proc
77 # func:: associate a block with add() to perform the action
79 # add an action to the timer
80 def add(period, data=nil, &func)
81 debug "adding timer, period #{period}"
83 @timers[@handle] = Action.new(period, data, &func)
88 # period:: how long (seconds) until the action is run
89 # data:: optional data to pass to the action's proc
90 # func:: associate a block with add() to perform the action
92 # add an action to the timer which will be run just once, after +period+
93 def add_once(period, data=nil, &func)
94 debug "adding one-off timer, period #{period}"
96 @timers[@handle] = Action.new(period, data, true, &func)
101 # remove action with handle +handle+ from the timer
103 @timers.delete(handle)
106 # block action with handle +handle+
108 @timers[handle].blocked = true
111 # unblock action with handle +handle+
113 @timers[handle].blocked = false
116 # you can call this when you know you're idle, or you can split off a
117 # thread and call the run() method to do it for you.
120 # don't do anything on the first tick
124 @next_action_time = 0
125 diff = (Time.now - @lasttime).to_f
127 @timers.each { |key,timer|
129 next if timer.blocked
136 if @next_action_time == 0 || timer.in < @next_action_time
137 @next_action_time = timer.in
140 #debug "ticked. now #{@timers.length} timers remain"
141 #debug "next timer due at #{@next_action_time}"
144 # for backwards compat - this is a bit primitive
145 def run(granularity=0.1)
153 @thread && @thread.alive?
156 # return the number of seconds until the next action is due, or 0 if
157 # none are outstanding - will only be accurate immediately after a
163 # start the timer, it spawns a thread to tick the timer, intelligently
164 # shutting down if no events remain and starting again when needed.
167 @should_be_running = true
168 start_thread unless @timers.empty?
171 # stop the timer from ticking
173 @should_be_running = false
183 elsif @should_be_running
189 return unless running?
195 @thread = Thread.new do
198 exit if @timers.empty?
199 sleep(@next_action_time)