/** * ConnectionManager - A reference-counted connection manager for SpacetimeDB. * * This module implements a TanStack Query-style pattern for managing WebSocket * connections in React applications. It solves the React StrictMode double-mount * problem by using reference counting and deferred cleanup. * * ## How it works: * * 1. **Reference Counting**: Each `retain()` increments a counter, `release()` decrements it. * The connection is only closed when the count reaches zero. * * 2. **Deferred Cleanup**: When refCount hits zero, cleanup is scheduled via `setTimeout(0)`. * This allows React StrictMode's rapid unmount→remount cycle to cancel the cleanup. * * 3. **useSyncExternalStore Integration**: The `subscribe()` and `getSnapshot()` methods * are designed to work with React's `useSyncExternalStore` hook for tear-free reads. * * ## StrictMode Lifecycle: * * ``` * Mount → retain() → refCount: 0→1, connection created * Unmount → release() → refCount: 1→0, cleanup SCHEDULED (not executed) * Remount → retain() → refCount: 0→1, cleanup CANCELLED * Result: Single WebSocket survives ✓ * ``` * * ## Reconnection: * * The manager forces {@link DbConnectionBuilder.withAutomaticReconnect} on * every builder it builds from, so a connection lost mid-session reconnects * *inside* the `DbConnection`: the object, its table handles and its callbacks * all survive, and the manager merely mirrors the lifecycle events into its * state snapshots. The manager itself rebuilds a connection only when the SDK * reports it will not retry (a failed initial connection, or another terminal * failure), preserving the frameworks' historical keep-trying behavior. * * @module connection_manager */ import type { DbConnectionBuilder, DbConnectionImpl, ErrorContextInterface, } from './db_connection_impl.ts'; import type { Identity } from '../lib/identity.ts'; import { ConnectionId } from '../lib/connection_id.ts'; /** Represents the current state of a managed connection. */ export type ConnectionState = { isActive: boolean; identity?: Identity; token?: string; connectionId: ConnectionId; connectionError?: Error; }; type Listener = () => void; export const CONNECTION_MANAGER_RECONNECT_BASE_DELAY_MS = 1000; export const CONNECTION_MANAGER_RECONNECT_MAX_DELAY_MS = 30_000; /** * Computes the rebuild delay for the given attempt (0-based) using * exponential backoff: the base delay doubles with each consecutive failed * attempt, capped at the maximum delay. * * This paces only the manager's own rebuild loop for failures the SDK will * not retry; reconnects after a mid-session drop are paced by the SDK's own * policy (see `computeReconnectDelayMs` in `db_connection_impl`). */ export function connectionManagerReconnectDelayMs(attempt: number): number { return Math.min( CONNECTION_MANAGER_RECONNECT_BASE_DELAY_MS * 2 ** attempt, CONNECTION_MANAGER_RECONNECT_MAX_DELAY_MS ); } type ManagedConnection = { connection?: DbConnectionImpl; builder?: DbConnectionBuilder; refCount: number; state: ConnectionState; listeners: Set; pendingRelease: ReturnType | null; reconnectTimer: ReturnType | null; reconnectAttempt: number; onConnect?: (conn: DbConnectionImpl) => void; onDisconnect?: ( ctx: ErrorContextInterface, error?: Error, nextReconnectAttempt?: number, nextReconnectDelayMs?: number ) => void; onConnectError?: ( ctx: ErrorContextInterface, error: Error, nextReconnectAttempt?: number, nextReconnectDelayMs?: number ) => void; }; function defaultState(): ConnectionState { return { isActive: false, identity: undefined, token: undefined, connectionId: ConnectionId.random(), connectionError: undefined, }; } /** * Singleton manager for SpacetimeDB connections. * Use the exported `ConnectionManager` instance rather than instantiating directly. */ class ConnectionManagerImpl { #connections = new Map(); /** Generates a unique key for a connection based on URI and module name. */ static getKey(uri: string, moduleName: string): string { return `${uri}::${moduleName}`; } /** Instance method wrapper for getKey. */ getKey(uri: string, moduleName: string): string { return ConnectionManagerImpl.getKey(uri, moduleName); } #ensureEntry(key: string): ManagedConnection { const existing = this.#connections.get(key); if (existing) { return existing; } const managed: ManagedConnection = { connection: undefined, builder: undefined, refCount: 0, state: defaultState(), listeners: new Set(), pendingRelease: null, reconnectTimer: null, reconnectAttempt: 0, }; this.#connections.set(key, managed); return managed; } #notify(managed: ManagedConnection): void { for (const listener of managed.listeners) { listener(); } } #updateState( managed: ManagedConnection, updates: Partial ): void { managed.state = { ...managed.state, ...updates }; this.#notify(managed); } #ensureCallbacks(managed: ManagedConnection): void { if (managed.onConnect) { return; } managed.onConnect = conn => { if (conn !== managed.connection) { return; } managed.reconnectAttempt = 0; this.#updateState(managed, { isActive: conn.isActive, identity: conn.identity, token: conn.token, connectionId: conn.connectionId, connectionError: undefined, }); }; // With automatic reconnection forced on, a non-undefined // `nextReconnectAttempt` means the SDK is retrying inside the same // connection object: mirror the event into state and leave the connection // alone. Only when the SDK reports it will not retry does the manager // rebuild a replacement (see #scheduleRebuild). managed.onDisconnect = (ctx, error, nextReconnectAttempt) => { if (ctx !== managed.connection) { return; } this.#updateState(managed, { isActive: false, connectionError: error ?? undefined, }); if (nextReconnectAttempt === undefined) { this.#scheduleRebuild(managed); } }; managed.onConnectError = (ctx, error, nextReconnectAttempt) => { if (ctx !== managed.connection) { return; } this.#updateState(managed, { isActive: false, connectionError: error, }); if (nextReconnectAttempt === undefined) { this.#scheduleRebuild(managed); } }; } #attachCallbacks>( managed: ManagedConnection, builder: DbConnectionBuilder ): void { this.#ensureCallbacks(managed); builder.onConnect(managed.onConnect!); builder.onAutomaticReconnect(managed.onConnect!); builder.onDisconnect(managed.onDisconnect!); builder.onConnectError(managed.onConnectError!); } #detachCallbacks( managed: ManagedConnection, connection: DbConnectionImpl ): void { if (managed.onConnect) { connection.removeOnConnect(managed.onConnect); connection.removeOnAutomaticReconnect(managed.onConnect); } if (managed.onDisconnect) { connection.removeOnDisconnect(managed.onDisconnect); } if (managed.onConnectError) { connection.removeOnConnectError(managed.onConnectError); } } /** Reuse the latest session token when rebuilding from the retained builder. */ #buildManagedConnection>( managed: ManagedConnection, builder: DbConnectionBuilder, { resumeSession = true }: { resumeSession?: boolean } = {} ): T { managed.builder = builder; builder.withAutomaticReconnect(); if (resumeSession && managed.state.token) { builder.withToken(managed.state.token); } const connection = builder.build(); managed.connection = connection; this.#attachCallbacks(managed, builder); this.#updateState(managed, { isActive: connection.isActive, identity: connection.identity, token: connection.token, connectionId: connection.connectionId, connectionError: undefined, }); return connection as T; } /** Preserve framework retries for failures the core connection will not retry. */ #scheduleRebuild(managed: ManagedConnection): void { if ( managed.refCount <= 0 || managed.pendingRelease || managed.reconnectTimer || !managed.builder ) { return; } const connection = managed.connection; if (connection) { this.#detachCallbacks(managed, connection); } managed.connection = undefined; // The application asked this connection to close; don't fight it. A // subsequent retain() will still build a fresh connection. if (connection?.isDisconnectRequested) { return; } const delay = connectionManagerReconnectDelayMs(managed.reconnectAttempt); managed.reconnectAttempt += 1; managed.reconnectTimer = setTimeout(() => { managed.reconnectTimer = null; if ( managed.refCount <= 0 || managed.pendingRelease || managed.connection || !managed.builder ) { return; } this.#buildManagedConnection(managed, managed.builder); }, delay); } /** * Retains a connection, incrementing its reference count. * Creates the connection on first call; returns existing connection on subsequent calls. * Cancels any pending release if the connection was about to be cleaned up. * * @param key - Unique identifier for the connection (use getKey to generate) * @param builder - Connection builder to create the connection if needed * @returns The managed connection instance */ retain>( key: string, builder: DbConnectionBuilder ): T { const managed = this.#ensureEntry(key); if (managed.pendingRelease) { clearTimeout(managed.pendingRelease); managed.pendingRelease = null; } if (managed.reconnectTimer) { clearTimeout(managed.reconnectTimer); managed.reconnectTimer = null; } managed.refCount += 1; managed.builder = builder; if (managed.connection) { return managed.connection as T; } return this.#buildManagedConnection(managed, builder); } /** * Tears down the current connection and builds a fresh one from `builder`, * preserving the entry's ref count and listener set. * * `retain()` deliberately ignores the builder once a connection is live — * the right behaviour for ref-counting, but it blocks "reconnect with a * fresh token" flows (e.g. swapping an anonymous session for a signed-in one * after an auth change). `rebuild()` is the supported escape hatch: pass a * builder carrying the new token and the pool swaps the live connection * under the same subscribers, so framework hooks (`useTable`, `useReducer`, * …) re-bind to the new connection automatically. * * The old connection's callbacks are detached before it is closed, so its * disconnect event never leaks into pool state, and any pending rebuild is * cancelled (the caller is driving the replacement explicitly). Returns the * newly-built connection, or `null` if the key has no retained entry. * * @param key - Unique identifier for the connection (use getKey to generate) * @param builder - Fresh connection builder; its handlers are rewired into the pool */ rebuild>( key: string, builder: DbConnectionBuilder ): T | null { const managed = this.#connections.get(key); if (!managed || managed.refCount <= 0) { return null; } // The caller is taking over the connection lifecycle explicitly; cancel a // deferred release or a pending rebuild so neither races the fresh // connection, and reset the backoff so the next terminal failure starts over. if (managed.pendingRelease) { clearTimeout(managed.pendingRelease); managed.pendingRelease = null; } if (managed.reconnectTimer) { clearTimeout(managed.reconnectTimer); managed.reconnectTimer = null; } managed.reconnectAttempt = 0; const connection = managed.connection; if (connection) { this.#detachCallbacks(managed, connection); connection.disconnect(); } managed.connection = undefined; try { // `resumeSession: false`: this is the one path that exists precisely to // *change* identity, so the replacement builder's token must win over the // outgoing session's. return this.#buildManagedConnection(managed, builder, { resumeSession: false, }) as T; } catch (error) { // The old connection is already torn down, so a failed rebuild would // otherwise leave the pool reporting a stale "live" connection. Surface // the failure into pool state (matching the onConnectError shape) so // subscribers see a disconnected/errored connection, then re-throw so // the caller can handle it. this.#updateState(managed, { isActive: false, connectionError: error instanceof Error ? error : new Error(String(error)), }); throw error; } } release(key: string): void { const managed = this.#connections.get(key); if (!managed) { return; } managed.refCount -= 1; if (managed.refCount > 0 || managed.pendingRelease) { return; } if (managed.reconnectTimer) { clearTimeout(managed.reconnectTimer); managed.reconnectTimer = null; } managed.pendingRelease = setTimeout(() => { managed.pendingRelease = null; if (managed.refCount > 0) { return; } const connection = managed.connection; managed.connection = undefined; if (connection) { this.#detachCallbacks(managed, connection); connection.disconnect(); } this.#connections.delete(key); }, 0); } subscribe(key: string, listener: Listener): () => void { const managed = this.#ensureEntry(key); managed.listeners.add(listener); return () => { managed.listeners.delete(listener); if ( managed.refCount <= 0 && managed.listeners.size === 0 && !managed.connection ) { this.#connections.delete(key); } }; } getSnapshot(key: string): ConnectionState | undefined { return this.#connections.get(key)?.state; } getConnection>(key: string): T | null { return (this.#connections.get(key)?.connection as T | undefined) ?? null; } } export const ConnectionManager = new ConnectionManagerImpl();