Class: BubbleWrap::Reactor::Timer

Inherits:
Object
  • Object
show all
Includes:
Eventable
Defined in:
motion/reactor/timer.rb

Overview

Creates a one-time timer.

Instance Method Summary collapse

Methods included from Eventable

#off, #on, #trigger

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

#cancelObject

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