Class: Hoodie::Timers::Events::Handle

Inherits:
Object
  • Object
show all
Defined in:
lib/hoodie/timers.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.



258
259
260
261
# File 'lib/hoodie/timers.rb', line 258

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

Instance Attribute Details

#timeObject (readonly)

The absolute time that the handle should be fired at.



264
265
266
# File 'lib/hoodie/timers.rb', line 264

def time
  @time
end

Instance Method Details

#>(other) ⇒ Object



278
279
280
# File 'lib/hoodie/timers.rb', line 278

def >(other)
  @time > other.to_f
end

#cancel!Object

Cancel this timer, O(1).



267
268
269
270
271
# File 'lib/hoodie/timers.rb', line 267

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)


274
275
276
# File 'lib/hoodie/timers.rb', line 274

def cancelled?
  @callback.nil?
end

#fire(time) ⇒ Object

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



287
288
289
290
291
# File 'lib/hoodie/timers.rb', line 287

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

#to_fObject



282
283
284
# File 'lib/hoodie/timers.rb', line 282

def to_f
  @time
end