* Subscribe to events. * * Not the correct implementation — this is not tied to a subscription id, so * unsubscribe below cancels by event/context name and can affect a * subscription made elsewhere (including via addCallback) for the * same event. Kept as-is only beca
(events, browsingContexts)
| 293 | * @returns {Promise<void>} |
| 294 | */ |
| 295 | async subscribe(events, browsingContexts) { |
| 296 | function toArray(arg) { |
| 297 | if (arg === undefined) { |
| 298 | return [] |
| 299 | } |
| 300 | |
| 301 | return Array.isArray(arg) ? [...arg] : [arg] |
| 302 | } |
| 303 | |
| 304 | const eventsArray = toArray(events) |
| 305 | const contextsArray = toArray(browsingContexts) |
| 306 | |
| 307 | const params = { |
| 308 | method: 'session.subscribe', |
| 309 | params: {}, |
| 310 | } |
| 311 | |
| 312 | if (eventsArray.length && eventsArray.some((event) => typeof event !== 'string')) { |
| 313 | throw new TypeError('events should be string or string array') |
| 314 | } |
| 315 | |
| 316 | if (contextsArray.length && contextsArray.some((context) => typeof context !== 'string')) { |
| 317 | throw new TypeError('browsingContexts should be string or string array') |
| 318 | } |
| 319 | |
| 320 | if (eventsArray.length) { |
| 321 | params.params.events = eventsArray |
| 322 | } |
| 323 | |
| 324 | if (contextsArray.length) { |
| 325 | params.params.contexts = contextsArray |
| 326 | } |
| 327 | |
| 328 | this.events.push(...eventsArray) |
| 329 | |
| 330 | await this.send(params) |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Unsubscribe to events. See the note on {@link subscribe} above — this |
no test coverage detected