rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
47 lines (46 loc) • 2.17 kB
TypeScript
import { type SyncedStateClient, type SyncedStateStatus, type StatusChangeCallback, type Connection } from "../connection/types.js";
type Subscription = {
key: string;
handler: (value: unknown) => void;
client: SyncedStateClient;
};
type BackoffState = {
attempt: number;
timer: ReturnType<typeof setTimeout> | null;
};
/**
* Owns all cross-request state for the hibernation sync-state client:
* cached clients, open connections, active subscriptions, status listeners,
* and per-endpoint reconnection backoff.
*
* A single module-level instance is used because the client cache and
* subscription registry must survive across React renders and component
* unmount/mount cycles on the same page.
*/
export declare class SyncedStateClientManager {
clients: Map<string, SyncedStateClient>;
connections: Map<string, Connection>;
subscriptions: Set<Subscription>;
statusListeners: Map<string, StatusChangeCallback[]>;
backoff: Map<string, BackoffState>;
normalizeEndpoint(endpoint: string): string;
getClient(endpoint: string): SyncedStateClient | undefined;
setClient(endpoint: string, client: SyncedStateClient): void;
deleteClient(endpoint: string): void;
getConnection(endpoint: string): Connection | undefined;
setConnection(endpoint: string, connection: Connection): void;
deleteConnection(endpoint: string): void;
getBackoff(endpoint: string): BackoffState;
setBackoff(endpoint: string, state: BackoffState): void;
resetBackoff(endpoint: string): void;
notifyStatusChange: (endpoint: string, status: SyncedStateStatus) => void;
onStatusChange: (endpoint: string, callback: StatusChangeCallback) => (() => void);
addSubscription(key: string, handler: (value: unknown) => void, client: SyncedStateClient): void;
removeSubscription(key: string, handler: (value: unknown) => void, client: SyncedStateClient): void;
subscriptionsForClient(client: SyncedStateClient): Subscription[];
clearSubscriptions(): void;
clearForTesting(endpoint: string): void;
beforeUnload: () => void;
}
export declare const manager: SyncedStateClientManager;
export {};