X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=lib%2Frbot%2Ftimer.rb;h=64b0ee431d4aeb261541e43bda38f31dc9953770;hb=8218e3f05e8ccd95497dd3c7aa115cfde8b01a40;hp=8adb14d8d42f955480c22402ec48f478e1b4468f;hpb=922c6a35bb34598da397d2a0078adc397e84a853;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/lib/rbot/timer.rb b/lib/rbot/timer.rb index 8adb14d8..64b0ee43 100644 --- a/lib/rbot/timer.rb +++ b/lib/rbot/timer.rb @@ -62,7 +62,7 @@ class Timer @repeat = opts[:repeat] if opts.include? :repeat if block_given? - @block = block + @block = block elsif opts[:code] @block = opts[:code] end @@ -214,7 +214,10 @@ class Timer end def stop - raise 'already stopped' unless @thread + unless @thread + warning 'trying to stop already stopped timer' + return + end debug "stopping timer #{self}..." @stopping = true self.synchronize { @tick.signal } @@ -233,31 +236,23 @@ class Timer end def run_actions(now = Time.now) - nxt = nil @actions.keys.each do |k| return -1 if @stopping - a = @actions[k] - next if (!a) or a.blocked? - - if a.next <= now - begin - @current = k - v = a.run(now) - ensure - @current = nil - end - - unless v - @actions.delete k - next - end - else - v = a.next + a = @actions[k] or next + next if a.blocked? || a.next > now + + begin + @current = k + a.run(now) + ensure + @current = nil end - nxt = v if v and ((!nxt) or (v < nxt)) + @actions.delete k unless a.next end + nxt = @actions.values.find_all { |v| !v.blocked? }.map{ |v| v.next }.min + if nxt delta = nxt - now delta = 0 if delta < 0