class Timers::Wait

An exclusive, monotonic timeout class.

Attributes

duration[R]
remaining[R]

Public Class Methods

for(duration) { |nil| ... } click to toggle source
# File lib/timers/wait.rb, line 13
def self.for(duration, &block)
        if duration
                timeout = new(duration)

                timeout.while_time_remaining(&block)
        else
                loop do
                        yield(nil)
                end
        end
end
new(duration) click to toggle source
# File lib/timers/wait.rb, line 25
def initialize(duration)
        @duration = duration
        @remaining = true
end

Public Instance Methods

while_time_remaining() { |remaining while time_remaining?| ... } click to toggle source

Yields while time remains for work to be done:

# File lib/timers/wait.rb, line 34
def while_time_remaining
        @interval = Interval.new
        @interval.start

        yield @remaining while time_remaining?
ensure
        @interval.stop
        @interval = nil
end

Private Instance Methods

time_remaining?() click to toggle source
# File lib/timers/wait.rb, line 46
def time_remaining?
        @remaining = (@duration - @interval.to_f)

        @remaining > 0
end