* Represents class to run events and commands of Script module. * Described in https://w3c.github.io/webdriver-bidi/#module-script. * @class
| 39 | * @class |
| 40 | */ |
| 41 | class ScriptManager { |
| 42 | #callbackId = 0 |
| 43 | #listener |
| 44 | |
| 45 | constructor(driver) { |
| 46 | this._driver = driver |
| 47 | this.#listener = new Map() |
| 48 | this.#listener.set(ScriptEvent.MESSAGE, new Map()) |
| 49 | this.#listener.set(ScriptEvent.REALM_CREATED, new Map()) |
| 50 | this.#listener.set(ScriptEvent.REALM_DESTROYED, new Map()) |
| 51 | } |
| 52 | |
| 53 | addCallback(eventType, callback) { |
| 54 | const id = ++this.#callbackId |
| 55 | |
| 56 | const eventCallbackMap = this.#listener.get(eventType) |
| 57 | eventCallbackMap.set(id, callback) |
| 58 | return id |
| 59 | } |
| 60 | |
| 61 | removeCallback(id) { |
| 62 | let hasId = false |
| 63 | for (const [, callbacks] of this.#listener) { |
| 64 | if (callbacks.has(id)) { |
| 65 | callbacks.delete(id) |
| 66 | hasId = true |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | if (!hasId) { |
| 71 | throw Error(`Callback with id ${id} not found`) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | invokeCallbacks(eventType, data) { |
| 76 | const callbacks = this.#listener.get(eventType) |
| 77 | if (callbacks) { |
| 78 | for (const [, callback] of callbacks) { |
| 79 | callback(data) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | async init(browsingContextIds) { |
| 85 | if (!(await this._driver.getCapabilities()).get('webSocketUrl')) { |
| 86 | throw Error('WebDriver instance must support BiDi protocol') |
| 87 | } |
| 88 | |
| 89 | this.bidi = await this._driver.getBidi() |
| 90 | this._browsingContextIds = browsingContextIds |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Disowns the handles in the specified realm. |
| 95 | * |
| 96 | * @param {string} realmId - The ID of the realm. |
| 97 | * @param {string[]} handles - The handles to disown to allow garbage collection. |
| 98 | * @returns {Promise<void>} - A promise that resolves when the command is sent. |
no outgoing calls
no test coverage detected