Class: Timers::Events::Handle
- Inherits:
-
Object
- Object
- Timers::Events::Handle
- Includes:
- Comparable
- 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
-
#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.
Constructor Details
#initialize(time, callback) ⇒ Handle
Returns a new instance of Handle.
21 22 23 24 |
# File 'lib/timers/events.rb', line 21 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.
27 28 29 |
# File 'lib/timers/events.rb', line 27 def time @time end |
Instance Method Details
#<=>(other) ⇒ Object
41 42 43 |
# File 'lib/timers/events.rb', line 41 def <=> other @time <=> other.time end |
#cancel! ⇒ Object
Cancel this timer, O(1).
30 31 32 33 34 |
# File 'lib/timers/events.rb', line 30 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.
37 38 39 |
# File 'lib/timers/events.rb', line 37 def cancelled? @callback.nil? end |
#fire(time) ⇒ Object
Fire the callback if not cancelled with the given time parameter.
46 47 48 |
# File 'lib/timers/events.rb', line 46 def fire(time) @callback.call(time) if @callback end |