Class: BubbleWrap::Reactor::Timer
- Inherits:
-
Object
- Object
- BubbleWrap::Reactor::Timer
- Includes:
- Eventable
- Defined in:
- motion/reactor/timer.rb
Overview
Creates a one-time timer.
Instance Method Summary collapse
-
#cancel ⇒ Object
Cancel the timer.
-
#initialize(leeway, callback = nil, &blk) ⇒ Timer
constructor
Create a new timer that fires after a given number of seconds.
Methods included from Eventable
Constructor Details
#initialize(leeway, callback = nil, &blk) ⇒ Timer
Create a new timer that fires after a given number of seconds
8 9 10 11 12 13 14 15 16 17 18 |
# File 'motion/reactor/timer.rb', line 8 def initialize(leeway, callback=nil, &blk) queue = Dispatch::Queue.current @timer = Dispatch::Source.timer(leeway, Dispatch::TIME_FOREVER, 0.0, queue) do |src| begin (callback || blk).call trigger(:fired) ensure src.cancel! end end end |
Instance Method Details
#cancel ⇒ Object
Cancel the timer
21 22 23 24 25 |
# File 'motion/reactor/timer.rb', line 21 def cancel @timer.cancel! if @timer trigger(:cancelled) true end |