Class: Appom::Wait

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

Constant Summary collapse

DEFAULT_TIMEOUT =
5
DEFAULT_INTERVAL =
0.25

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Wait

Create a new Wait instance

Parameters:

  • opts (Hash) (defaults to: {})

    Options for this instance

Options Hash (opts):

  • :timeout (Numeric) — default: 5

    Seconds to wait before timing out.

  • :interval (Numeric) — default: 0.25

    Seconds to sleep between polls.



13
14
15
16
# File 'lib/appom/wait.rb', line 13

def initialize(opts = {})
  @timeout  = opts.fetch(:timeout, DEFAULT_TIMEOUT)
  @interval = opts.fetch(:interval, DEFAULT_INTERVAL)
end

Instance Method Details

#untilObject

Wait until the given block returns a true value.

Returns:

  • (Object)

    the result of the block

Raises:

  • (Error::TimeOutError)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/appom/wait.rb', line 24

def until
  end_time = Time.now + @timeout

  until Time.now > end_time
    begin
      result = yield
      return result if result
    rescue
    end

    sleep @interval
  end

  raise Appom::TimeoutError, "Timed out after #{@timeout}s."
end