Class: Puppeteer::Bidi::WaitTask

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/bidi/wait_task.rb,
sig/puppeteer/bidi/wait_task.rbs

Overview

WaitTask orchestrates polling for a predicate using Puppeteer's Poller classes. This is a faithful port of Puppeteer's WaitTask implementation: https://github.com/puppeteer/puppeteer/blob/main/packages/puppeteer-core/src/common/WaitTask.ts

Note: signal and AbortSignal are not implemented as they are JavaScript-specific

Instance Method Summary collapse

Constructor Details

#initialize(world, options, fn, *args) ⇒ Object

Corresponds to Puppeteer's WaitTask constructor

Parameters:

  • world (Realm)

    The realm to execute in (matches Puppeteer's World abstraction)

  • options (Hash)

    Options for waiting

  • fn (String)

    JavaScript function to evaluate

  • args (Array)

    Arguments to pass to the function

Options Hash (options):

  • :polling (String, Numeric)

    Polling strategy ('raf', 'mutation', or interval in ms)

  • :timeout (Numeric)

    Timeout in milliseconds

  • :root (ElementHandle)

    Root element for mutation polling



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/puppeteer/bidi/wait_task.rb', line 20

def initialize(world, options, fn, *args)
  @world = world
  @polling = options[:polling]
  @root = options[:root]

  # Convert function to string format
  # Corresponds to Puppeteer's switch (typeof fn)
  if fn.is_a?(String)
    # Check if the string is already a function (starts with "function ", "(", or "async ")
    # If so, use it as-is. Otherwise, wrap it as an expression.
    if fn.strip.match?(/\A(?:function\s|\(|async\s)/)
      @fn = fn
    else
      @fn = "() => {return (#{fn});}"
    end
  else
    raise ArgumentError, 'fn must be a string'
  end
  @args = args

  # Corresponds to Puppeteer's #timeout and #timeoutError
  @timeout_task = nil
  @generic_error = StandardError.new('Waiting failed')
  @timeout_error = nil

  # Corresponds to Puppeteer's #result = Deferred.create<HandleFor<T>>()
  @result = Async::Promise.new

  # Corresponds to Puppeteer's #poller?: JSHandle<Poller<T>>
  @poller = nil

  # Track the active rerun Async task so we can cancel it when rerunning
  @rerun_task = nil

  # Validate polling interval
  if @polling.is_a?(Numeric) && @polling < 0
    raise ArgumentError, "Cannot poll with non-positive interval: #{@polling}"
  end

  # Corresponds to Puppeteer's this.#world.taskManager.add(this)
  @world.task_manager.add(self)

  # Store timeout value and start timeout task
  # Corresponds to Puppeteer's setTimeout(() => { void this.terminate(this.#timeoutError); }, options.timeout)
  default_timeout = if @world.respond_to?(:default_timeout)
                      @world.default_timeout
                    elsif @world.respond_to?(:page)
                      @world.page.default_timeout
                    end
  @timeout_ms = options.key?(:timeout) ? options[:timeout] : default_timeout
  if @timeout_ms && @timeout_ms > 0
    @timeout_error = Puppeteer::Bidi::TimeoutError.new(
      "Waiting failed: #{@timeout_ms}ms exceeded"
    )
    # Start timeout task in background
    @timeout_task = Async do |task|
      task.sleep(@timeout_ms / 1000.0)
      @timeout_task = nil # prevent stopping in terminate
      terminate(@timeout_error)
    end
  end

  # Start polling
  # Corresponds to Puppeteer's void this.rerun()
  rerun
end

Instance Method Details

#create_interval_pollerObject

Create IntervalPoller instance Corresponds to Puppeteer's evaluateHandle call for IntervalPoller

Returns:

  • (Object)


251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/puppeteer/bidi/wait_task.rb', line 251

def create_interval_poller
  util_handle = @world.puppeteer_util
  interval = @polling.is_a?(Numeric) ? @polling : 100

  # Corresponds to Puppeteer's evaluateHandle with LazyArg
  script = <<~JAVASCRIPT
    ({IntervalPoller, createFunction}, ms, fn, ...args) => {
      const fun = createFunction(fn);
      return new IntervalPoller(() => {
        return fun(...args);
      }, ms);
    }
  JAVASCRIPT

  @world.evaluate_handle(script, util_handle, interval, @fn, *@args)
end

#create_mutation_pollerObject

Create MutationPoller instance Corresponds to Puppeteer's evaluateHandle call for MutationPoller

Returns:

  • (Object)


233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/puppeteer/bidi/wait_task.rb', line 233

def create_mutation_poller
  util_handle = @world.puppeteer_util

  # Corresponds to Puppeteer's evaluateHandle with LazyArg
  script = <<~JAVASCRIPT
    ({MutationPoller, createFunction}, root, fn, ...args) => {
      const fun = createFunction(fn);
      return new MutationPoller(() => {
        return fun(...args);
      }, root || document);
    }
  JAVASCRIPT

  @world.evaluate_handle(script, util_handle, @root, @fn, *@args)
end

#create_raf_pollerObject

Create RAFPoller instance Corresponds to Puppeteer's evaluateHandle call for RAFPoller

Returns:

  • (Object)


214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/puppeteer/bidi/wait_task.rb', line 214

def create_raf_poller
  util_handle = @world.puppeteer_util

  # Corresponds to Puppeteer's evaluateHandle with LazyArg
  script = <<~JAVASCRIPT
    ({RAFPoller, createFunction}, fn, ...args) => {
      const fun = createFunction(fn);
      return new RAFPoller(() => {
        return fun(...args);
      });
    }
  JAVASCRIPT

  handle = @world.evaluate_handle(script, util_handle, @fn, *@args)
  handle
end

#get_bad_error(error) ⇒ Exception?

Check if error should terminate task Corresponds to Puppeteer's getBadError(error: unknown): Error | undefined

Parameters:

  • error (Exception)

    Error to check

Returns:

  • (Exception, nil)

    Error if it should terminate, nil if recoverable



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/puppeteer/bidi/wait_task.rb', line 272

def get_bad_error(error)
  return nil unless error

  error_message = error.message

  # Frame detachment is fatal
  # Corresponds to Puppeteer's error.message.includes('Execution context is not available in detached frame')
  if error_message.include?('Execution context is not available in detached frame')
    return StandardError.new('Waiting failed: Frame detached')
  end

  # These are recoverable (realm was destroyed/recreated)
  # Corresponds to Puppeteer's error.message.includes('Execution context was destroyed')
  return nil if error_message.include?('Execution context was destroyed')

  # Corresponds to Puppeteer's error.message.includes('Cannot find context with specified id')
  return nil if error_message.include?('Cannot find context with specified id')

  # Corresponds to Puppeteer's error.message.includes('DiscardedBrowsingContextError')
  return nil if error_message.include?('DiscardedBrowsingContextError')

  # Recoverable when the browsing context is torn down during navigation.
  return nil if error_message.include?('Browsing Context with id')
  return nil if error_message.include?('no such frame')

  # Happens when handles become invalid after realm/navigation changes.
  return nil if error_message.include?('Unable to find an object reference for "handle"')

  # All other errors are fatal
  # Corresponds to Puppeteer's return error;
  error
end

#perform_rerunObject

Run a single rerun cycle. Mirrors the async rerun logic from Puppeteer.

Returns:

  • (Object)


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/puppeteer/bidi/wait_task.rb', line 149

def perform_rerun
  poller = nil
  schedule_rerun = false

  begin
    # Create Poller instance based on polling mode
    # Corresponds to Puppeteer's switch (this.#polling)
    poller = case @polling
             when 'raf'
               create_raf_poller
             when 'mutation'
               create_mutation_poller
             else
               create_interval_poller
             end

    @poller = poller

    # Start the poller
    # Corresponds to Puppeteer's await this.#poller.evaluate(poller => { void poller.start(); });
    poller.evaluate('poller => { void poller.start(); }')

    # Get the result
    # Corresponds to Puppeteer's const result = await this.#poller.evaluateHandle(poller => { return poller.result(); });
    # Note: poller.result() returns a Promise, so we need to await it
    # evaluateHandle with awaitPromise: true will wait for the Promise to resolve
    result_handle = poller.evaluate_handle('poller => { return poller.result(); }')

    # Resolve the result
    # Corresponds to Puppeteer's this.#result.resolve(result);
    @result.resolve(result_handle)

    # Terminate cleanly
    # Corresponds to Puppeteer's await this.terminate();
    terminate
  rescue Async::Stop
    # Propagate cancellation so caller can distinguish from regular errors
    raise
  rescue => error
    # Check if this is a bad error
    # Corresponds to Puppeteer's const badError = this.getBadError(error);
    bad_error = get_bad_error(error)
    if bad_error
      # Corresponds to Puppeteer's this.#genericError.cause = badError;
      @generic_error = StandardError.new(@generic_error.message)
      @generic_error.set_backtrace([bad_error.message] + bad_error.backtrace)
      # Corresponds to Puppeteer's await this.terminate(this.#genericError);
      terminate(@generic_error)
    else
      schedule_rerun = true
    end
    # If badError is nil, it's a recoverable error and we don't terminate
    # Puppeteer would rerun automatically via realm 'updated' event
  ensure
    if poller && @poller.equal?(poller)
      stop_and_dispose_poller(poller)
      @poller = nil
    end
  end

  rerun if schedule_rerun && !@result.resolved?
end

#rerunObject

Rerun the polling task Corresponds to Puppeteer's async rerun(): Promise

Returns:

  • (Object)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/puppeteer/bidi/wait_task.rb', line 96

def rerun
  # Cancel previous rerun task if one is active
  if (previous_task = @rerun_task)
    @rerun_task = nil if @rerun_task.equal?(previous_task)
    previous_task.stop
  end

  # Launch the rerun asynchronously so it can be cancelled like Puppeteer's AbortController
  @rerun_task = Async do |task|
    begin
      perform_rerun
    rescue Async::Stop
      # Rerun was cancelled; poller cleanup happens in ensure block
    ensure
      @rerun_task = nil if @rerun_task.equal?(task)
    end
  end
end

#resultAsync::Promise

Get the result as a promise Corresponds to Puppeteer's get result(): Promise<HandleFor>

Returns:



90
91
92
# File 'lib/puppeteer/bidi/wait_task.rb', line 90

def result
  @result
end

#stop_and_dispose_poller(poller) ⇒ Object

Safely stop and dispose a poller handle, ignoring cleanup errors

Parameters:

  • poller (Object)

Returns:

  • (Object)


306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/puppeteer/bidi/wait_task.rb', line 306

def stop_and_dispose_poller(poller)
  return unless poller

  begin
    poller.evaluate('async poller => { await poller.stop(); }')
  rescue StandardError
    # Ignore errors from stopping the poller
  end

  begin
    poller.dispose
  rescue StandardError
    # Ignore dispose errors as they are low-level cleanup
  end
end

#terminate(error = nil) ⇒ Object

Terminate the task Corresponds to Puppeteer's async terminate(error?: Error): Promise

Parameters:

  • error (Exception, nil) (defaults to: nil)

    Error to reject with

Returns:

  • (Object)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/puppeteer/bidi/wait_task.rb', line 118

def terminate(error = nil)
  # Corresponds to Puppeteer's this.#world.taskManager.delete(this)
  @world.task_manager.delete(self)

  # Note: this.#signal?.removeEventListener('abort', this.#onAbortSignal) is skipped
  # AbortSignal is not implemented

  # Clear timeout task
  # Corresponds to Puppeteer's clearTimeout(this.#timeout)
  if @timeout_task
    @timeout_task.stop
    @timeout_task = nil
  end

  # Reject result if not finished
  # Corresponds to Puppeteer's if (error && !this.#result.finished()) { this.#result.reject(error); }
  if error && !@result.resolved?
    @result.reject(error)
  end

  # Stop and dispose poller
  # Corresponds to Puppeteer's if (this.#poller) { ... }
  if @poller
    stop_and_dispose_poller(@poller)
    @poller = nil
  end
end