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.



12
13
14
15
# File 'lib/timers/events.rb', line 12

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

Instance Attribute Details

#timeObject (readonly)

The absolute time that the handle should be fired at.



18
19
20
# File 'lib/timers/events.rb', line 18

def time
  @time
end

Instance Method Details

#>(other) ⇒ Object



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

def > other
  @time > other.to_f
end

#cancel!Object

Cancel this timer, O(1).



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

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)


28
29
30
# File 'lib/timers/events.rb', line 28

def cancelled?
  @callback.nil?
end

#fire(time) ⇒ Object

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



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

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

#to_fObject



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

def to_f
  @time
end