Class: Mfp::ResettableTimer
- Inherits:
-
Object
- Object
- Mfp::ResettableTimer
- Defined in:
- lib/mfp/resettable_timer.rb
Instance Method Summary collapse
-
#initialize(timeout, notify_queue, tag: nil) ⇒ ResettableTimer
constructor
A new instance of ResettableTimer.
- #reset ⇒ Object
- #start(task) ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(timeout, notify_queue, tag: nil) ⇒ ResettableTimer
Returns a new instance of ResettableTimer.
5 6 7 8 9 10 11 12 |
# File 'lib/mfp/resettable_timer.rb', line 5 def initialize(timeout, notify_queue, tag: nil) @timeout = timeout @reset_condition = Async::Condition.new @running = true @notify_queue = notify_queue @tag = tag @itask = nil end |
Instance Method Details
#reset ⇒ Object
39 |
# File 'lib/mfp/resettable_timer.rb', line 39 def reset = @reset_condition.signal |
#start(task) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mfp/resettable_timer.rb', line 14 def start(task) @itask = task.async do |task| while @running result = Async::Queue.new sleeper = task.async do |s| s.sleep(@timeout) result.enqueue(:timeout) end waiter = task.async do @reset_condition.wait result.enqueue(:reset) end msg = result.dequeue sleeper.stop unless sleeper.stopped? waiter.stop unless waiter.stopped? next if msg == :reset @notify_queue.enqueue [msg, @tag] end end end |
#stop ⇒ Object
41 42 43 44 45 |
# File 'lib/mfp/resettable_timer.rb', line 41 def stop @itask&.stop @running = false @reset_condition.signal end |