Module: Ferrum::Utils::Poll

Defined in:
lib/ferrum/utils/poll.rb,
sig/ferrum/utils/poll.rbs

Overview

The loop behind every wait_* method: runs a block until it returns a truthy value or the timeout is reached, sleeping between the attempts.

Class Method Summary collapse

Class Method Details

.attempt(deadline) { ... } ⇒ Object?

Runs the block once, turning a TimeoutError it raises into nil once the deadline has expired.

Parameters:

Yields:

Yield Returns:

  • (T)

Returns:

  • (Object, nil)


48
49
50
51
52
53
54
# File 'lib/ferrum/utils/poll.rb', line 48

def attempt(deadline)
  yield
rescue TimeoutError
  raise unless deadline.expired?

  nil
end

.call(timeout, interval) { ... } ⇒ Object?

Runs the block until it returns a truthy value. Calls in the block share the timeout, see Deadline.within.

Parameters:

  • timeout (Numeric, Deadline) —

    How long to keep trying.

  • interval (Numeric) —

    Seconds to sleep between the attempts.

Yields:

  • The check to repeat.

Yield Returns:

  • (T)

Returns:

  • (Object, nil) —

    What the block returned, nil if it didn't return a truthy value in time.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ferrum/utils/poll.rb', line 27

def call(timeout, interval, &)
  deadline = Deadline.wrap(timeout)
  Deadline.within(deadline) do
    loop do
      value = attempt(deadline, &)
      return value if value
      return if deadline.expired?

      sleep([interval, deadline.remaining].min)
    end
  end
end