Class: Larynx::RestartableTimer

Inherits:
EM::Timer
  • Object
show all
Defined in:
lib/larynx/restartable_timer.rb

Overview

Adds restart to EM timer class. Implementation influenced by EM::PeriodicTimer class so hopefully it should not cause any issues.

Instance Method Summary collapse

Constructor Details

#initialize(interval, callback = nil, &block) ⇒ RestartableTimer

Returns a new instance of RestartableTimer.



5
6
7
8
9
# File 'lib/larynx/restartable_timer.rb', line 5

def initialize(interval, callback=nil, &block)
  @interval = interval
  @code = callback || block
  schedule
end

Instance Method Details

#fireObject



21
22
23
# File 'lib/larynx/restartable_timer.rb', line 21

def fire
  @code.call
end

#restartObject

Restart the timer



12
13
14
15
# File 'lib/larynx/restartable_timer.rb', line 12

def restart
  cancel
  schedule
end

#scheduleObject



17
18
19
# File 'lib/larynx/restartable_timer.rb', line 17

def schedule
  @signature = EM::add_timer(@interval, method(:fire))
end