fix(rpc): improve handlers

This commit is contained in:
2026-09-28 02:05:40 +01:00
parent 558f04567a
commit b48d3f5240
6 changed files with 74 additions and 71 deletions
+3 -11
View File
@@ -13,7 +13,6 @@ import {
type RPCState,
} from '../utils/types'
/** Shared plumbing of the server, client and webview instances */
export class RPCInstanceBase {
protected readonly env: RPCEnvironment
protected readonly debug: boolean
@@ -26,9 +25,7 @@ export class RPCInstanceBase {
this._pending = new Pending(cfg.timeout ?? 5000)
}
// ===== LISTENERS =====
/** Registers `cb` for `event` on `emitter`; `method` is only used for logs */
/** Registers `cb` for `event` on `emitter`; `method` is only for logs */
protected listen(
emitter: Emitter,
method: string,
@@ -39,15 +36,14 @@ export class RPCInstanceBase {
emitter.on(event, cb)
return this
}
/** Unregisters `cb` for `event` on `emitter`; `method` is only for logs */
protected unlisten(emitter: Emitter, method: string, event: string): this {
this.log(`${method} ${event}`)
emitter.off(event)
return this
}
// ===== OUTGOING =====
/** Builds an event payload sent from this environment to `to` */
protected request(
event: string,
@@ -89,8 +85,6 @@ export class RPCInstanceBase {
if (!found) this.logIgnored(response)
}
// ===== INCOMING =====
/**
* Runs the listener for `request` and builds the response to send back.
* Never throws: a missing listener or a thrown error ends up in `error`.
@@ -147,8 +141,6 @@ export class RPCInstanceBase {
})
}
// ===== LOGS =====
/** Debug-only log, prefixed with the environment */
protected log(message: string): void {
if (this.debug) console.log(`[RPC]:${this.env}:${message}`)