class BubbleWrap::Reactor::PeriodicTimer

Creates a repeating timer.

Attributes

interval[RW]

Public Class Methods

new(interval, *args, &blk) click to toggle source

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

# 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

Public Instance Methods

cancel() click to toggle source

Cancel the timer

# File motion/reactor/periodic_timer.rb, line 31
def cancel
  @timer.cancel!
  trigger(:cancelled)
end