Sha256: 877049042d468f2204ced672a2c636df9bd363a478c5559c37a136845326b76f
Contents?: true
Size: 974 Bytes
Versions: 2
Compression:
Stored size: 974 Bytes
Contents
# frozen_string_literal: true # Released under the MIT License. # Copyright, 2014-2022, by Samuel Williams. # Copyright, 2014-2016, by Tony Arcieri. # Copyright, 2015, by Utenmiki. # Copyright, 2015, by Donovan Keme. require_relative "interval" module Timers # An exclusive, monotonic timeout class. class Wait def self.for(duration, &block) if duration timeout = new(duration) timeout.while_time_remaining(&block) else loop do yield(nil) end end end def initialize(duration) @duration = duration @remaining = true end attr_reader :duration attr_reader :remaining # Yields while time remains for work to be done: def while_time_remaining @interval = Interval.new @interval.start yield @remaining while time_remaining? ensure @interval.stop @interval = nil end private def time_remaining? @remaining = (@duration - @interval.to_f) @remaining > 0 end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
timers-4.3.5 | lib/timers/wait.rb |
timers-4.3.4 | lib/timers/wait.rb |