Class: Larynx::RestartableTimer
- Inherits:
-
EM::Timer
- Object
- EM::Timer
- Larynx::RestartableTimer
- 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
- #fire ⇒ Object
-
#initialize(interval, callback = nil, &block) ⇒ RestartableTimer
constructor
A new instance of RestartableTimer.
-
#restart ⇒ Object
Restart the timer.
- #schedule ⇒ Object
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
#fire ⇒ Object
21 22 23 |
# File 'lib/larynx/restartable_timer.rb', line 21 def fire @code.call end |
#restart ⇒ Object
Restart the timer
12 13 14 15 |
# File 'lib/larynx/restartable_timer.rb', line 12 def restart cancel schedule end |
#schedule ⇒ Object
17 18 19 |
# File 'lib/larynx/restartable_timer.rb', line 17 def schedule @signature = EM::add_timer(@interval, method(:fire)) end |