Class: Timers::Wait

Inherits:
Object
  • Object
show all
Defined in:
lib/timers/wait.rb

Overview

An exclusive, monotonic timeout class.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(duration) ⇒ Wait

Returns a new instance of Wait.



19
20
21
22
# File 'lib/timers/wait.rb', line 19

def initialize(duration)
  @duration = duration
  @remaining = true
end

Instance Attribute Details

#durationObject (readonly)

Returns the value of attribute duration.



24
25
26
# File 'lib/timers/wait.rb', line 24

def duration
  @duration
end

#remainingObject (readonly)

Returns the value of attribute remaining.



25
26
27
# File 'lib/timers/wait.rb', line 25

def remaining
  @remaining
end

Class Method Details

.for(duration, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/timers/wait.rb', line 7

def self.for(duration, &block)
  if duration
    timeout = self.new(duration)
    
    timeout.while_time_remaining(&block)
  else
    while true
      yield(nil)
    end
  end
end

Instance Method Details

#while_time_remaining(&block) ⇒ Object

Yields while time remains for work to be done:



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/timers/wait.rb', line 28

def while_time_remaining(&block)
  @interval = Hitimes::Interval.new
  @interval.start
  
  while time_remaining?
    yield @remaining
  end
ensure
  @interval.stop
  @interval = nil
end