Class: Captive::Poller
- Inherits:
-
Object
- Object
- Captive::Poller
- Defined in:
- lib/captive/poller.rb
Instance Method Summary collapse
-
#initialize(interval:, timeout:, sleeper: method(:sleep), clock: -> { Time.now }) ⇒ Poller
constructor
A new instance of Poller.
- #poll_until(context: nil, on_tick: nil, &block) ⇒ Object
Constructor Details
#initialize(interval:, timeout:, sleeper: method(:sleep), clock: -> { Time.now }) ⇒ Poller
Returns a new instance of Poller.
7 8 9 10 11 12 |
# File 'lib/captive/poller.rb', line 7 def initialize(interval:, timeout:, sleeper: method(:sleep), clock: -> { Time.now }) @interval = interval @timeout = timeout @sleeper = sleeper @clock = clock end |
Instance Method Details
#poll_until(context: nil, on_tick: nil, &block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/captive/poller.rb', line 14 def poll_until(context: nil, on_tick: nil, &block) start = @clock.call deadline = start + @timeout loop do return true if block.call now = @clock.call raise PollTimeout, (context) if now >= deadline on_tick&.call(now - start) @sleeper.call(@interval) end end |