Sha256: dbe0b4b190d08dcf7fcf6919e4cb39e405d5f981557a1883cb5a242370bdbccd

Contents?: true

Size: 1.03 KB

Versions: 8

Compression:

Stored size: 1.03 KB

Contents

module BubbleWrap
  module Reactor
    # Creates a repeating timer.
    class PeriodicTimer
      include Eventable

      attr_accessor :interval

      # Create a new timer that fires after a given number of seconds
      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

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

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
bubble-wrap-1.9.7 motion/reactor/periodic_timer.rb
bubble-wrap-1.9.6 motion/reactor/periodic_timer.rb
bubble-wrap-1.9.5 motion/reactor/periodic_timer.rb
bubble-wrap-1.9.4 motion/reactor/periodic_timer.rb
bubble-wrap-1.9.3 motion/reactor/periodic_timer.rb
bubble-wrap-1.9.2 motion/reactor/periodic_timer.rb
bubble-wrap-1.9.1 motion/reactor/periodic_timer.rb
bubble-wrap-1.9.0 motion/reactor/periodic_timer.rb