Class: Timers::Group
- Inherits:
-
Object
- Object
- Timers::Group
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/timers/group.rb
Instance Method Summary collapse
- #add(timer) ⇒ Object
-
#after(interval, &block) ⇒ Object
Call the given block after the given interval.
-
#after_milliseconds(interval, &block) ⇒ Object
(also: #after_ms)
Call the given block after the given interval has expired.
- #continue(timer = nil) ⇒ Object
- #continue_all ⇒ Object
- #current_offset ⇒ Object
- #delay(seconds) ⇒ Object
-
#every(interval, &block) ⇒ Object
Call the given block periodically at the given interval.
-
#fire(offset = self.current_offset) ⇒ Object
Fire all timers that are ready.
-
#initialize ⇒ Group
constructor
A new instance of Group.
- #pause(timer = nil) ⇒ Object
- #pause_all ⇒ Object
-
#wait ⇒ Object
Wait for the next timer and fire it.
-
#wait_interval(offset = self.current_offset) ⇒ Object
Interval to wait until when the next timer will fire.
Constructor Details
#initialize ⇒ Group
Returns a new instance of Group.
14 15 16 17 18 19 |
# File 'lib/timers/group.rb', line 14 def initialize @timers = SortedSet.new @paused_timers = SortedSet.new @interval = Hitimes::Interval.new @interval.start end |
Instance Method Details
#add(timer) ⇒ Object
69 70 71 72 |
# File 'lib/timers/group.rb', line 69 def add(timer) raise TypeError, "not a Timers::Timer" unless timer.is_a? Timers::Timer @timers.add(timer) end |
#after(interval, &block) ⇒ Object
Call the given block after the given interval
22 23 24 |
# File 'lib/timers/group.rb', line 22 def after(interval, &block) Timer.new(self, interval, false, &block) end |
#after_milliseconds(interval, &block) ⇒ Object Also known as: after_ms
31 32 33 |
# File 'lib/timers/group.rb', line 31 def after_milliseconds(interval, &block) after(interval / 1000.0, &block) end |
#continue(timer = nil) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/timers/group.rb', line 85 def continue(timer = nil) return continue_all if timer.nil? raise TypeError, "not a Timers::Timer" unless timer.is_a? Timers::Timer @paused_timers.delete timer @timers.add timer end |
#continue_all ⇒ Object
92 93 94 |
# File 'lib/timers/group.rb', line 92 def continue_all @paused_timers.each {|timer| timer.continue} end |
#current_offset ⇒ Object
102 103 104 |
# File 'lib/timers/group.rb', line 102 def current_offset @interval.to_f end |
#delay(seconds) ⇒ Object
96 97 98 |
# File 'lib/timers/group.rb', line 96 def delay(seconds) @timers.each {|timer| timer.delay(seconds)} end |
#every(interval, &block) ⇒ Object
Call the given block periodically at the given interval
37 38 39 |
# File 'lib/timers/group.rb', line 37 def every(interval, &block) Timer.new(self, interval, true, &block) end |
#fire(offset = self.current_offset) ⇒ Object
Fire all timers that are ready
61 62 63 64 65 66 67 |
# File 'lib/timers/group.rb', line 61 def fire(offset = self.current_offset) time = Float(offset) while (timer = @timers.first) && (time >= timer.offset) @timers.delete timer timer.fire(offset) end end |
#pause(timer = nil) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/timers/group.rb', line 74 def pause(timer = nil) return pause_all if timer.nil? raise TypeError, "not a Timers::Timer" unless timer.is_a? Timers::Timer @timers.delete timer @paused_timers.add timer end |
#pause_all ⇒ Object
81 82 83 |
# File 'lib/timers/group.rb', line 81 def pause_all @timers.each {|timer| timer.pause} end |
#wait ⇒ Object
Wait for the next timer and fire it
42 43 44 45 46 47 48 49 50 |
# File 'lib/timers/group.rb', line 42 def wait # Repeatedly call sleep until there is no longer any wait_interval: while i = wait_interval # We cannot assume that sleep will wait for the specified time, it might be +/- a bit. sleep i end fire end |
#wait_interval(offset = self.current_offset) ⇒ Object
Interval to wait until when the next timer will fire
53 54 55 56 57 58 |
# File 'lib/timers/group.rb', line 53 def wait_interval(offset = self.current_offset) timer = @timers.first return unless timer interval = timer.offset - Float(offset) interval > 0 ? interval : nil end |