* 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)
| 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 |
no test coverage detected