Class: BubbleWrap::Reactor::PeriodicTimer

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

Overview

Creates a repeating timer.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Eventable

#off, #on, #trigger

Constructor Details

- (PeriodicTimer) initialize(interval, *args, &blk)

Create a new timer that fires after a given number of seconds

Raises:

  • (ArgumentError)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'motion/reactor/periodic_timer.rb', line 10

def initialize(interval, *args, &blk)
  callback = args.first.respond_to?(:call) ? args.first : blk
  raise ArgumentError, "No callback or block supplied to periodic timer" unless callback
  callback.weak! if callback && BubbleWrap.use_weak_callbacks?

  options = args.last.is_a?(Hash) ? args.last : {}
  if options[:common_modes]
    NSLog "[DEPRECATED - Option :common_modes] a Run Loop Mode is no longer needed."
  end
  
  self.interval = interval

  leeway = interval
  queue  = Dispatch::Queue.current
  @timer = Dispatch::Source.timer(leeway, interval, 0.0, queue) do
    callback.call
    trigger(:fired)
  end
end

Instance Attribute Details

- (Object) interval

Returns the value of attribute interval



7
8
9
# File 'motion/reactor/periodic_timer.rb', line 7

def interval
  @interval
end

Instance Method Details

- (Object) cancel

Cancel the timer



31
32
33
34
# File 'motion/reactor/periodic_timer.rb', line 31

def cancel
  @timer.cancel!
  trigger(:cancelled)
end