export { getBroadcastAdapter, installBroadcastAdapter, _resetBroadcastAdapterForTesting, DefaultBroadcastAdapter }; export type { BroadcastAdapter, BroadcastTransport, BroadcastUnsubscribe, BroadcastPublishResult, BroadcastOnMessage, BroadcastBinaryOnMessage, }; import type { WirePublishInfo } from '../shared-ws.js'; /** Transport-level publish result. */ type BroadcastPublishResult = WirePublishInfo & { meta?: Record; }; /** Callback for delivering a broadcast message to a subscriber. */ type BroadcastOnMessage = (serialized: string, info: WirePublishInfo) => void; type BroadcastBinaryOnMessage = (data: Uint8Array, info: WirePublishInfo) => void; type BroadcastUnsubscribe = () => void; type BroadcastAdapter = { subscribe(key: string, onMessage: BroadcastOnMessage): BroadcastUnsubscribe; publish(key: string, serialized: string): BroadcastPublishResult | Promise; subscribeBinary(key: string, onMessage: BroadcastBinaryOnMessage): BroadcastUnsubscribe; publishBinary(key: string, data: Uint8Array): BroadcastPublishResult | Promise; }; /** * Minimal interface for a broadcast transport backend. * * Implement these 4 methods to get a full BroadcastAdapter via `new DefaultBroadcastAdapter(transport)`. * Subscriber multiplexing and lifecycle are handled for you. */ type BroadcastTransport = { /** Send a text message. Must return the assigned seq and timestamp. */ send(key: string, payload: string): { seq: number; timestamp: number; } | Promise<{ seq: number; timestamp: number; }>; /** Listen for text messages on a key. Called at most once per key. Return an unsubscribe function. */ listen(key: string, onMessage: (payload: string, info: { seq: number; timestamp: number; }) => void): () => void; /** Send a binary message. Must return the assigned seq and timestamp. */ sendBinary(key: string, payload: Uint8Array): { seq: number; timestamp: number; } | Promise<{ seq: number; timestamp: number; }>; /** Listen for binary messages on a key. Called at most once per key. Return an unsubscribe function. */ listenBinary(key: string, onMessage: (payload: Uint8Array, info: { seq: number; timestamp: number; }) => void): () => void; }; declare class DefaultBroadcastAdapter implements BroadcastAdapter { private readonly transport; private readonly subscriptions; private readonly binarySubscriptions; private readonly transportUnsubs; private readonly transportBinaryUnsubs; /** Per-key seq counter for in-memory mode. */ private readonly keySeqs; constructor(transport?: BroadcastTransport); subscribe(key: string, onMessage: BroadcastOnMessage): BroadcastUnsubscribe; publish(key: string, serialized: string): BroadcastPublishResult | Promise; subscribeBinary(key: string, onMessage: BroadcastBinaryOnMessage): BroadcastUnsubscribe; publishBinary(key: string, data: Uint8Array): BroadcastPublishResult | Promise; private _publishInMemory; private _deliver; private _releaseUnsub; } declare function getBroadcastAdapter(): BroadcastAdapter; /** Per-isolate singleton. The factory runs once: the first caller installs the * adapter, subsequent callers receive the already-installed one. This handles * Vite HMR reloads and bundler quirks that evaluate the user's entry * module more than once in the same isolate. Returns the installed instance so the caller * can keep using the canonical reference. */ declare function installBroadcastAdapter(factory: () => T): T; /** @internal — test-only escape hatch. */ declare function _resetBroadcastAdapterForTesting(adapter: BroadcastAdapter): void;