Class: Timers::Group
- Inherits:
-
Object
- Object
- Timers::Group
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/timers/group.rb
Overview
A collection of timers which may fire at different times
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Scheduled events:.
-
#paused_timers ⇒ Object
readonly
Paused timers:.
-
#timers ⇒ Object
readonly
Active timers:.
Instance Method Summary collapse
-
#after(interval, &block) ⇒ Object
Call the given block after the given interval.
-
#cancel ⇒ Object
Cancel all timers.
-
#current_offset ⇒ Object
The group’s current time.
-
#delay(seconds) ⇒ Object
Delay all timers.
-
#every(interval, recur = true, &block) ⇒ Object
Call the given block periodically at the given interval.
-
#fire(offset = current_offset) ⇒ Object
Fire all timers that are ready.
-
#initialize ⇒ Group
constructor
A new instance of Group.
-
#now_and_after(interval, &block) ⇒ Object
Call the given block immediately, and then after the given interval.
-
#now_and_every(interval, recur = true, &block) ⇒ Object
Call the given block immediately, and then periodically at the given interval.
-
#pause ⇒ Object
Pause all timers.
-
#resume ⇒ Object
(also: #continue)
Resume all timers.
-
#wait ⇒ Object
Wait for the next timer and fire it.
-
#wait_interval(offset = current_offset) ⇒ Object
Interval to wait until when the next timer will fire.
Constructor Details
Instance Attribute Details
#events ⇒ Object (readonly)
Scheduled events:
49 50 51 |
# File 'lib/timers/group.rb', line 49 def events @events end |
#paused_timers ⇒ Object (readonly)
Paused timers:
55 56 57 |
# File 'lib/timers/group.rb', line 55 def paused_timers @paused_timers end |
#timers ⇒ Object (readonly)
Active timers:
52 53 54 |
# File 'lib/timers/group.rb', line 52 def timers @timers end |
Instance Method Details
#after(interval, &block) ⇒ Object
Call the given block after the given interval. The first argument will be the time at which the group was asked to fire timers for.
59 60 61 |
# File 'lib/timers/group.rb', line 59 def after(interval, &block) Timer.new(self, interval, false, &block) end |
#cancel ⇒ Object
Cancel all timers.
138 139 140 |
# File 'lib/timers/group.rb', line 138 def cancel @timers.dup.each(&:cancel) end |
#current_offset ⇒ Object
The group’s current time.
143 144 145 |
# File 'lib/timers/group.rb', line 143 def current_offset @interval.to_f end |
#delay(seconds) ⇒ Object
Delay all timers.
131 132 133 134 135 |
# File 'lib/timers/group.rb', line 131 def delay(seconds) @timers.each do |timer| timer.delay(seconds) end end |
#every(interval, recur = true, &block) ⇒ Object
Call the given block periodically at the given interval. The first argument will be the time at which the group was asked to fire timers for.
72 73 74 |
# File 'lib/timers/group.rb', line 72 def every(interval, recur = true, &block) Timer.new(self, interval, recur, &block) end |
#fire(offset = current_offset) ⇒ Object
Fire all timers that are ready.
114 115 116 |
# File 'lib/timers/group.rb', line 114 def fire(offset = current_offset) @events.fire(offset) end |
#now_and_after(interval, &block) ⇒ Object
Call the given block immediately, and then after the given interval. The first argument will be the time at which the group was asked to fire timers for.
65 66 67 68 |
# File 'lib/timers/group.rb', line 65 def now_and_after(interval, &block) yield after(interval, &block) end |
#now_and_every(interval, recur = true, &block) ⇒ Object
Call the given block immediately, and then periodically at the given interval. The first argument will be the time at which the group was asked to fire timers for.
78 79 80 81 |
# File 'lib/timers/group.rb', line 78 def now_and_every(interval, recur = true, &block) yield every(interval, recur, &block) end |
#pause ⇒ Object
Pause all timers.
119 120 121 |
# File 'lib/timers/group.rb', line 119 def pause @timers.dup.each(&:pause) end |
#resume ⇒ Object Also known as: continue
Resume all timers.
124 125 126 |
# File 'lib/timers/group.rb', line 124 def resume @paused_timers.dup.each(&:resume) end |
#wait ⇒ Object
Wait for the next timer and fire it. Can take a block, which should behave like sleep(n), except that n may be nil (sleep forever) or a negative number (fire immediately after return).
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/timers/group.rb', line 86 def wait if block_given? yield wait_interval while (interval = wait_interval) && interval > 0 yield interval end else while (interval = wait_interval) && interval > 0 # We cannot assume that sleep will wait for the specified time, it might be +/- a bit. sleep interval end end fire end |
#wait_interval(offset = current_offset) ⇒ Object
Interval to wait until when the next timer will fire.
-
nil: no timers
-
-ve: timers expired already
-
0: timers ready to fire
-
+ve: timers waiting to fire
108 109 110 111 |
# File 'lib/timers/group.rb', line 108 def wait_interval(offset = current_offset) handle = @events.first handle.time - Float(offset) if handle end |