MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / addCallback

Method addCallback

javascript/selenium-webdriver/bidi/index.js:401–431  ·  view source on GitHub ↗

* Registers `handler` to be called on every delivered `method` event, * globally (no context/user-context scoping). This is the correct mechanism * — see the note on subscribe/unsubscribe above, which is a * separate, imprecise, event-name-scoped mechanism kept only for the

(method, handler)

Source from the content-addressed store, hash-verified

399 * @returns {Promise<{id: string, unsubscribe: function(): Promise<void>}>}
400 */
401 async addCallback(method, handler) {
402 this.on(method, handler)
403
404 try {
405 const response = await this.send({
406 method: 'session.subscribe',
407 params: { events: [method] },
408 })
409 // send() resolves with the raw reply on any response, including a
410 // wire-level error — check for one explicitly and surface it plainly,
411 // rather than letting it fall through to the generic "no subscription
412 // id" message below (matching how Domain#send() reports the same shape).
413 if (response?.error !== undefined) {
414 throw new Error(`${response.error}: ${response.message}`)
415 }
416 const subscriptionId = response?.result?.subscription
417 if (typeof subscriptionId !== 'string' || subscriptionId === '') {
418 throw new Error(`session.subscribe did not return a valid subscription id: ${JSON.stringify(response)}`)
419 }
420
421 this._callbacks.set(subscriptionId, { method, handler })
422
423 return {
424 id: subscriptionId,
425 unsubscribe: () => this.removeCallback(subscriptionId),
426 }
427 } catch (err) {
428 this.off(method, handler)
429 throw err
430 }
431 }
432
433 /**
434 * Removes exactly the callback registered under `subscriptionId` (as

Callers 7

index_test.jsFile · 0.45
domain_test.jsFile · 0.45
network_test.jsFile · 0.45
log_test.jsFile · 0.45
input_test.jsFile · 0.45
script_test.jsFile · 0.45

Calls 4

sendMethod · 0.95
removeCallbackMethod · 0.95
setMethod · 0.65
onMethod · 0.45

Tested by

no test coverage detected