Class: Timers::Events::Handle

Inherits:
Object
  • Object
show all
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.



15
16
17
18
# File 'lib/timers/events.rb', line 15

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

Instance Attribute Details

#timeObject (readonly)

The absolute time that the handle should be fired at.



21
22
23
# File 'lib/timers/events.rb', line 21

def time
  @time
end

Instance Method Details

#>(other) ⇒ Object



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

def > other
	@time > other.to_f
end

#>=(other) ⇒ Object



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

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

#cancel!Object

Cancel this timer, O(1).



24
25
26
27
28
# File 'lib/timers/events.rb', line 24

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)


31
32
33
# File 'lib/timers/events.rb', line 31

def cancelled?
	@callback.nil?
end

#fire(time) ⇒ Object

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



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

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

#to_fObject



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

def to_f
	@time
end