Class: Captive::Poller

Inherits:
Object
  • Object
show all
Defined in:
lib/captive/poller.rb

Instance Method Summary collapse

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, timeout_message(context) if now >= deadline

    on_tick&.call(now - start)
    @sleeper.call(@interval)
  end
end