Class: Hoodie::Timers::Events::Handle
Overview
Represents a cancellable handle for a specific timer event.
Instance Attribute Summary collapse
-
#time ⇒ Object
readonly
The absolute time that the handle should be fired at.
Instance Method Summary collapse
- #>(other) ⇒ Object
-
#cancel! ⇒ Object
Cancel this timer, O(1).
-
#cancelled? ⇒ Boolean
Has this timer been cancelled? Cancelled timer’s don’t fire.
-
#fire(time) ⇒ Object
Fire the callback if not cancelled with the given time parameter.
-
#initialize(time, callback) ⇒ Handle
constructor
A new instance of Handle.
- #to_f ⇒ Object
Constructor Details
#initialize(time, callback) ⇒ Handle
Returns a new instance of Handle.
258 259 260 261 |
# File 'lib/hoodie/timers.rb', line 258 def initialize(time, callback) @time = time @callback = callback end |
Instance Attribute Details
#time ⇒ Object (readonly)
The absolute time that the handle should be fired at.
264 265 266 |
# File 'lib/hoodie/timers.rb', line 264 def time @time end |
Instance Method Details
#>(other) ⇒ Object
278 279 280 |
# File 'lib/hoodie/timers.rb', line 278 def >(other) @time > other.to_f end |
#cancel! ⇒ Object
Cancel this timer, O(1).
267 268 269 270 271 |
# File 'lib/hoodie/timers.rb', line 267 def cancel! # The simplest way to keep track of cancelled status is to nullify the # callback. This should also be optimal for garbage collection. @callback = nil end |
#cancelled? ⇒ Boolean
Has this timer been cancelled? Cancelled timer’s don’t fire.
274 275 276 |
# File 'lib/hoodie/timers.rb', line 274 def cancelled? @callback.nil? end |
#fire(time) ⇒ Object
Fire the callback if not cancelled with the given time parameter.
287 288 289 290 291 |
# File 'lib/hoodie/timers.rb', line 287 def fire(time) if @callback @callback.call(time) end end |
#to_f ⇒ Object
282 283 284 |
# File 'lib/hoodie/timers.rb', line 282 def to_f @time end |