Class: Timers::Events::Handle

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/timers/events.rb

Overview

Represents a cancellable handle for a specific timer event.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time, callback) ⇒ Handle

Returns a new instance of Handle.


33
34
35
36
# File 'lib/timers/events.rb', line 33

def initialize(time, callback)
	@time = time
	@callback = callback
end

Instance Attribute Details

#timeObject (readonly)

The absolute time that the handle should be fired at.


39
40
41
# File 'lib/timers/events.rb', line 39

def time
  @time
end

Instance Method Details

#<=>(other) ⇒ Object


53
54
55
# File 'lib/timers/events.rb', line 53

def <=> other
	@time <=> other.time
end

#cancel!Object

Cancel this timer, O(1).


42
43
44
45
46
# File 'lib/timers/events.rb', line 42

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.

Returns:

  • (Boolean)

49
50
51
# File 'lib/timers/events.rb', line 49

def cancelled?
	@callback.nil?
end

#fire(time) ⇒ Object

Fire the callback if not cancelled with the given time parameter.


58
59
60
# File 'lib/timers/events.rb', line 58

def fire(time)
	@callback.call(time) if @callback
end