Class: Playwright::Waiter

Inherits:
Object
  • Object
show all
Defined in:
lib/playwright/waiter.rb

Overview

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(channel_owner, wait_name:) ⇒ Waiter

Returns a new instance of Waiter.



7
8
9
10
11
12
13
14
15
# File 'lib/playwright/waiter.rb', line 7

def initialize(channel_owner, wait_name:)
  @result = Concurrent::Promises.resolvable_future
  @wait_id = SecureRandom.hex(16)
  @event = wait_name
  @channel = channel_owner.channel
  @registered_listeners = Set.new
  @logs = []
  wait_for_event_info_before
end

Instance Attribute Details

#resultObject (readonly)

Returns the value of attribute result.



116
117
118
# File 'lib/playwright/waiter.rb', line 116

def result
  @result
end

Instance Method Details

#force_fulfill(result) ⇒ Object



78
79
80
# File 'lib/playwright/waiter.rb', line 78

def force_fulfill(result)
  fulfill(result)
end

#log(message) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/playwright/waiter.rb', line 118

def log(message)
  @logs << message
  begin
    @channel.async_send_message_to_server(
      "waitForEventInfo",
      {
        "info": {
          "waitId": @wait_id,
          "phase": "log",
          "message": message,
        },
      },
    )
  rescue => err
    # ignore
  end
end

#reject_on_event(emitter, event, error_or_proc, predicate: nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/playwright/waiter.rb', line 43

def reject_on_event(emitter, event, error_or_proc, predicate: nil)
  listener = -> (*args) {
    if !predicate || predicate.call(*args)
      if error_or_proc.respond_to?(:call)
        reject(error_or_proc.call)
      else
        reject(error_or_proc)
      end
    end
  }
  emitter.on(event, listener)
  @registered_listeners << [emitter, event, listener]

  self
end

#reject_on_timeout(timeout_ms, message) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/playwright/waiter.rb', line 59

def reject_on_timeout(timeout_ms, message)
  return if timeout_ms <= 0

  Concurrent::Promises.schedule(timeout_ms / 1000.0) do
    reject(TimeoutError.new(message: message))
  end.rescue do |err|
    puts err, err.backtrace
  end

  self
end

#wait_for_event(emitter, event, predicate: nil) ⇒ Object

Parameters:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/playwright/waiter.rb', line 100

def wait_for_event(emitter, event, predicate: nil)
  listener = -> (*args) {
    begin
      if !predicate || predicate.call(*args)
        fulfill(args.first)
      end
    rescue => err
      reject(err)
    end
  }
  emitter.on(event, listener)
  @registered_listeners << [emitter, event, listener]

  self
end