diff options
Diffstat (limited to 'lib/rbot/timer.rb')
-rw-r--r-- | lib/rbot/timer.rb | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/lib/rbot/timer.rb b/lib/rbot/timer.rb index 03a4c91e..0527c1e7 100644 --- a/lib/rbot/timer.rb +++ b/lib/rbot/timer.rb @@ -2,10 +2,10 @@ module Timer # timer event, something to do and when/how often to do it class Action - + # when this action is due next (updated by tick()) attr_reader :in - + # is this action blocked? if so it won't be run attr_accessor :blocked @@ -13,7 +13,7 @@ module Timer # data:: optional data to pass to the proc # once:: optional, if true, this action will be run once then removed # func:: associate a block to be called to perform the action - # + # # create a new action def initialize(period, data=nil, once=false, &func) @blocked = false @@ -31,7 +31,7 @@ module Timer @last_tick = Time.new end - def inspect + def inspect "#<#{self.class}:#{@period}s:#{@once ? 'once' : 'repeat'}>" end @@ -52,12 +52,17 @@ module Timer end return @once end + + # reschedule the Action to change its period + def reschedule(new_period) + @period = new_period + end end - + # timer handler, manage multiple Action objects, calling them when required. # The timer must be ticked by whatever controls it, i.e. regular calls to # tick() at whatever granularity suits your application's needs. - # + # # Alternatively you can call run(), and the timer will spawn a thread and # tick itself, intelligently shutting down the thread if there are no # pending actions. @@ -71,11 +76,11 @@ module Timer @thread = false @next_action_time = 0 end - + # period:: how often (seconds) to run the action # data:: optional data to pass to the action's proc # func:: associate a block with add() to perform the action - # + # # add an action to the timer def add(period, data=nil, &func) debug "adding timer, period #{period}" @@ -88,7 +93,7 @@ module Timer # period:: how long (seconds) until the action is run # data:: optional data to pass to the action's proc # func:: associate a block with add() to perform the action - # + # # add an action to the timer which will be run just once, after +period+ def add_once(period, data=nil, &func) debug "adding one-off timer, period #{period}" @@ -102,7 +107,7 @@ module Timer def remove(handle) @timers.delete(handle) end - + # block action with handle +handle+ def block(handle) @timers[handle].blocked = true @@ -113,9 +118,14 @@ module Timer @timers[handle].blocked = false end + # reschedule action with handle +handle+ to change its period + def reschedule(handle, period) + @timers[handle].reschedule(period) + end + # you can call this when you know you're idle, or you can split off a # thread and call the run() method to do it for you. - def tick + def tick if(@lasttime == 0) # don't do anything on the first tick @lasttime = Time.now @@ -173,9 +183,9 @@ module Timer @should_be_running = false stop_thread end - + private - + def start_on_add if running? stop_thread @@ -184,12 +194,12 @@ module Timer start_thread end end - + def stop_thread return unless running? @thread.kill end - + def start_thread return if running? @thread = Thread.new do |