Class: Timers::Events::Handle
- Inherits:
-
Object
- Object
- Timers::Events::Handle
- Defined in:
- lib/timers/events.rb
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
- #>=(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.
30 31 32 33 |
# File 'lib/timers/events.rb', line 30 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.
36 37 38 |
# File 'lib/timers/events.rb', line 36 def time @time end |
Instance Method Details
#>(other) ⇒ Object
50 51 52 |
# File 'lib/timers/events.rb', line 50 def > other @time > other.to_f end |
#>=(other) ⇒ Object
54 55 56 |
# File 'lib/timers/events.rb', line 54 def >= other @time >= other.to_f end |
#cancel! ⇒ Object
Cancel this timer, O(1).
39 40 41 42 43 |
# File 'lib/timers/events.rb', line 39 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.
46 47 48 |
# File 'lib/timers/events.rb', line 46 def cancelled? @callback.nil? end |
#fire(time) ⇒ Object
Fire the callback if not cancelled with the given time parameter.
63 64 65 |
# File 'lib/timers/events.rb', line 63 def fire(time) @callback.call(time) if @callback end |
#to_f ⇒ Object
58 59 60 |
# File 'lib/timers/events.rb', line 58 def to_f @time end |