feat(rpc): extract _pending* maps to Pending util

This commit is contained in:
2026-09-28 02:05:40 +01:00
parent 46933c1656
commit 79faa3b23c
6 changed files with 99 additions and 50 deletions
+13
View File
@@ -1,5 +1,6 @@
import { Emitter } from '../utils/emitter'
import { parse } from '../utils/funcs'
import { Pending } from '../utils/pending'
import {
type RPCConfig,
type RPCEnvironment,
@@ -11,16 +12,28 @@ import {
export class Wrapper {
protected env: RPCEnvironment
protected _emitterLocal: Emitter
protected _pending: Pending
protected debug: boolean
protected console: Console
constructor(cfg: RPCConfig<RPCEnvironment>) {
this.env = cfg.env
this._emitterLocal = new Emitter()
this._pending = new Pending(cfg.timeout ?? 5000)
this.debug = cfg.debug ?? false
this.console = console
}
/** Settles the call waiting for this response; ignores late or unexpected ones */
protected resolvePending(payload: RPCState): void {
const found = this._pending.resolve(payload.uuid, payload.data?.[0])
if (!found && this.debug) {
this.console.log(
`[RPC]:ignored response ${payload.event} ${payload.uuid} (no pending call, possibly timed out)`,
)
}
}
protected verifyEvent(state: Emitter, data: RPCStateRaw | RPCState) {
const rpcData = typeof data === 'string' ? parse(data) : data