Class: ZMQMachine::Timer
- Inherits:
-
Object
- Object
- ZMQMachine::Timer
- Includes:
- Comparable
- Defined in:
- lib/zm/timers.rb
Overview
Used to track the specific expiration time and execution code for each timer.
This should never be instantiated directly by user code. A timer must be known to the #Reactor in which it is running, so use the #Reactor#oneshot_timer and #Reactor#periodical_timer convenience methods. It ensures that new timers are installed in the correct #Reactor.
Instance Attribute Summary collapse
-
#fire_time ⇒ Object
readonly
Returns the value of attribute fire_time.
-
#timer_proc ⇒ Object
readonly
Returns the value of attribute timer_proc.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#cancel ⇒ Object
Cancels this timer from firing.
-
#expired?(time) ⇒ Boolean
True when the timer should be fired; false otherwise.
-
#fire ⇒ Object
Executes the callback.
-
#initialize(timers, delay, periodical, timer_proc = nil, &blk) ⇒ Timer
constructor
delay
is in milliseconds. - #inspect ⇒ Object
-
#periodical? ⇒ Boolean
True when this is a periodical timer; false otherwise.
- #reschedule ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(timers, delay, periodical, timer_proc = nil, &blk) ⇒ Timer
delay
is in milliseconds
239 240 241 242 243 244 245 |
# File 'lib/zm/timers.rb', line 239 def initialize timers, delay, periodical, timer_proc = nil, &blk @timers = timers @delay = delay.to_i @periodical = periodical @timer_proc = timer_proc || blk schedule_firing_time end |
Instance Attribute Details
#fire_time ⇒ Object (readonly)
Returns the value of attribute fire_time.
235 236 237 |
# File 'lib/zm/timers.rb', line 235 def fire_time @fire_time end |
#timer_proc ⇒ Object (readonly)
Returns the value of attribute timer_proc.
235 236 237 |
# File 'lib/zm/timers.rb', line 235 def timer_proc @timer_proc end |
Instance Method Details
#<=>(other) ⇒ Object
265 266 267 |
# File 'lib/zm/timers.rb', line 265 def <=>(other) self.fire_time <=> other.fire_time end |
#cancel ⇒ Object
Cancels this timer from firing.
261 262 263 |
# File 'lib/zm/timers.rb', line 261 def cancel @timers.cancel self end |
#expired?(time) ⇒ Boolean
True when the timer should be fired; false otherwise.
271 272 273 274 |
# File 'lib/zm/timers.rb', line 271 def expired? time time ||= Timers.now time > @fire_time end |
#fire ⇒ Object
Executes the callback.
Returns true
when the timer is a one-shot; Returns false
when the timer is periodical and has rescheduled itself.
253 254 255 256 257 |
# File 'lib/zm/timers.rb', line 253 def fire @timer_proc.call schedule_firing_time if @periodical end |
#inspect ⇒ Object
290 |
# File 'lib/zm/timers.rb', line 290 def inspect; to_s; end |
#periodical? ⇒ Boolean
True when this is a periodical timer; false otherwise.
278 279 280 |
# File 'lib/zm/timers.rb', line 278 def periodical? @periodical end |
#reschedule ⇒ Object
282 283 284 |
# File 'lib/zm/timers.rb', line 282 def reschedule schedule_firing_time end |