Sha256: 4c92600195be2e4e7bd843fbbad115c1b859c5c313adffdce5dc96b2f0ffa8db

Contents?: true

Size: 890 Bytes

Versions: 10

Compression:

Stored size: 890 Bytes

Contents

require 'hitimes'

module Timers
  # An exclusive, monotonic timeout class.
  class Wait
    def self.for(duration, &block)
      if duration
        timeout = self.new(duration)
        
        timeout.while_time_remaining(&block)
      else
        while true
          yield(nil)
        end
      end
    end
    
    def initialize(duration)
      @duration = duration
      @remaining = true
    end
    
    attr :duration
    attr :remaining
    
    # Yields while time remains for work to be done:
    def while_time_remaining(&block)
      @interval = Hitimes::Interval.new
      @interval.start
      
      while time_remaining?
        yield @remaining
      end
    ensure
      @interval.stop
      @interval = nil
    end
    
    private
    
    def time_remaining?
      @remaining = (@duration - @interval.duration)
    
      return @remaining > 0
    end
  end
end

Version data entries

10 entries across 8 versions & 4 rubygems

Version Path
honeybadger-2.4.0 vendor/gems/ruby/1.9.1/gems/timers-4.0.0/lib/timers/wait.rb
honeybadger-2.4.0 vendor/gems/ruby/2.1.0/gems/timers-4.0.0/lib/timers/wait.rb
honeybadger-2.4.0 vendor/gems/ruby/2.2.0/gems/timers-4.0.0/lib/timers/wait.rb
timers-4.0.4 lib/timers/wait.rb
timers-4.1.1 lib/timers/wait.rb
timers-4.1.0 lib/timers/wait.rb
scoot-0.0.4 .bundle/gems/ruby/2.2.0/gems/timers-4.0.1/lib/timers/wait.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/timers-4.0.1/lib/timers/wait.rb
timers-4.0.1 lib/timers/wait.rb
timers-4.0.0 lib/timers/wait.rb