Class: Hoodie::Timers::Group
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/hoodie/timers.rb
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.
-
#pause ⇒ Object
Pause all timers.
-
#resume ⇒ Object
(also: #continue)
Resume all timers.
-
#wait(&_block) ⇒ 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:
170 171 172 |
# File 'lib/hoodie/timers.rb', line 170 def events @events end |
#paused_timers ⇒ Object (readonly)
Paused timers:
176 177 178 |
# File 'lib/hoodie/timers.rb', line 176 def paused_timers @paused_timers end |
#timers ⇒ Object (readonly)
Active timers:
173 174 175 |
# File 'lib/hoodie/timers.rb', line 173 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.
180 181 182 |
# File 'lib/hoodie/timers.rb', line 180 def after(interval, &block) Timer.new(self, interval, false, &block) end |
#cancel ⇒ Object
Cancel all timers.
244 245 246 |
# File 'lib/hoodie/timers.rb', line 244 def cancel @timers.dup.each(&:cancel) end |
#current_offset ⇒ Object
The group’s current time.
249 250 251 |
# File 'lib/hoodie/timers.rb', line 249 def current_offset @interval.to_f end |
#delay(seconds) ⇒ Object
Delay all timers.
237 238 239 240 241 |
# File 'lib/hoodie/timers.rb', line 237 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.
186 187 188 |
# File 'lib/hoodie/timers.rb', line 186 def every(interval, recur = true, &block) Timer.new(self, interval, recur, &block) end |
#fire(offset = current_offset) ⇒ Object
Fire all timers that are ready.
221 222 223 |
# File 'lib/hoodie/timers.rb', line 221 def fire(offset = current_offset) @events.fire(offset) end |
#pause ⇒ Object
Pause all timers.
226 227 228 |
# File 'lib/hoodie/timers.rb', line 226 def pause @timers.dup.each(&:pause) end |
#resume ⇒ Object Also known as: continue
Resume all timers.
231 232 233 |
# File 'lib/hoodie/timers.rb', line 231 def resume @paused_timers.dup.each(&:resume) end |
#wait(&_block) ⇒ 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).
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/hoodie/timers.rb', line 193 def wait(&_block) if block_given? yield wait_interval while interval = wait_interval and interval > 0 yield interval end else while interval = wait_interval and 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
214 215 216 217 218 |
# File 'lib/hoodie/timers.rb', line 214 def wait_interval(offset = current_offset) if handle = @events.first return handle.time - Float(offset) end end |