Sha256: 9f85f4c6f8b634cbcb5f94f983b04ce42c396be634f75768e4080bd5db3c920a

Contents?: true

Size: 663 Bytes

Versions: 8

Compression:

Stored size: 663 Bytes

Contents

module BubbleWrap
  module Reactor
    # Creates a one-time timer.
    class Timer
      include Eventable

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

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

    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

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