/** * Timing primitives for the bridge child's wallet poll. * * Extracted into a typed module (BridgeChild.ts is `@ts-nocheck`) so this * infrastructure is type-checked. */ // Child's give-up deadline for the wallet poll: writes [] via fillEmptyDefaults // if the parent never reports ready. The injected-connector consumer derives its // own poll budget (BRIDGE_WALLET_POLL_MS) from this and MUST exceed it, so it can // observe a wallet the child marshals on its final tick. export const MAX_POLL_MS = 5000 export const POLL_INTERVAL_MS = 50 // A parent read is a postMessage round-trip — typically <5ms, so 150ms is // generous. A spurious timeout (parent briefly busy) just retries on the next // tick; it never drops a result. const PARENT_READ_TIMEOUT_MS = 150 export class BridgeReadTimeout extends Error { constructor() { super('bridge parent read timed out') this.name = 'BridgeReadTimeout' } } /** * A Comlink read to a parent that hasn't `Comlink.expose`d yet never settles * (comlink's `requestResponseMessage` has no timeout, and the request message is * dropped — so it won't resolve even once the parent comes up; a *new* read must * be posted). The parent is typically created on the iframe's `load` event, * which can fire after the child starts polling, so the first reads would hang * the poll loop forever. Race every read against a short deadline so a pre-expose * tick rejects (and the caller retries) instead of stalling. */ export function raceParentRead( read: PromiseLike | T ): Promise> { let timer: ReturnType | undefined const timeout = new Promise((_, reject) => { timer = setTimeout( () => reject(new BridgeReadTimeout()), PARENT_READ_TIMEOUT_MS ) }) // clearTimeout on settle so a winning read doesn't leak a pending timer. (The // losing Comlink read can't be cancelled, but the timer can.) return Promise.race([Promise.resolve(read), timeout]).finally(() => clearTimeout(timer) ) }