/** * Tracks one-shot request/response pairs over the `postMessage` bridge. Each `allocate` reserves a * unique `requestId` and stores its resolver. `resolve` finds the pending resolver by `requestId` * and calls it, then removes the entry so the same `requestId` cannot be resolved twice. */ export class PendingRequests { private nextId = 1 private readonly entries = new Map void>() constructor(private readonly prefix: string) {} allocate(resolver: (value: T) => void): string { const requestId = `${this.prefix}-${this.nextId}` this.nextId += 1 this.entries.set(requestId, resolver) return requestId } resolve(requestId: string, value: T): boolean { const resolver = this.entries.get(requestId) if (resolver === undefined) { return false } this.entries.delete(requestId) resolver(value) return true } }