* Unsubscribe to events. See the note on subscribe above — this * cancels by event/context name, not by subscription id, so it can affect a * subscription this same connection made elsewhere. New code should call * the returned handle's `unsubscribe()` from addCallback inste
(events, browsingContexts)
| 340 | * @returns {Promise<void>} |
| 341 | */ |
| 342 | async unsubscribe(events, browsingContexts) { |
| 343 | const eventsToRemove = typeof events === 'string' ? [events] : events |
| 344 | |
| 345 | // Check if the eventsToRemove are in the subscribed events array |
| 346 | // Filter out events that are not in this.events before filtering |
| 347 | const existingEvents = eventsToRemove.filter((event) => this.events.includes(event)) |
| 348 | |
| 349 | // Remove the events from the subscribed events array |
| 350 | this.events = this.events.filter((event) => !existingEvents.includes(event)) |
| 351 | |
| 352 | if (typeof browsingContexts === 'string') { |
| 353 | this.browsingContexts.pop() |
| 354 | } else if (Array.isArray(browsingContexts)) { |
| 355 | this.browsingContexts = this.browsingContexts.filter((id) => !browsingContexts.includes(id)) |
| 356 | } |
| 357 | |
| 358 | if (existingEvents.length === 0) { |
| 359 | return |
| 360 | } |
| 361 | const params = { |
| 362 | method: 'session.unsubscribe', |
| 363 | params: { |
| 364 | events: existingEvents, |
| 365 | }, |
| 366 | } |
| 367 | |
| 368 | if (this.browsingContexts.length > 0) { |
| 369 | params.params.contexts = this.browsingContexts |
| 370 | } |
| 371 | |
| 372 | await this.send(params) |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Registers `handler` to be called on every delivered `method` event, |