rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
30 lines (29 loc) • 1.41 kB
TypeScript
export type SyncedStateClient = {
getState(key: string): Promise<unknown>;
setState(value: unknown, key: string): Promise<void>;
subscribe(key: string, handler: (value: unknown) => void): Promise<void>;
unsubscribe(key: string, handler: (value: unknown) => void): Promise<void>;
};
/**
* Returns a cached client for the provided endpoint, creating it when necessary.
* The client is wrapped to track subscriptions for cleanup on page reload.
* @param endpoint Endpoint to connect to.
* @returns RPC client instance.
*/
export declare const getSyncedStateClient: (endpoint?: string) => SyncedStateClient;
/**
* Initializes and caches an RPC client instance for the sync state endpoint.
* The client is wrapped to track subscriptions for cleanup on page reload.
* @param options Optional endpoint override.
* @returns Cached client instance or `null` when running without `window`.
*/
export declare const initSyncedStateClient: (options?: {
endpoint?: string;
}) => SyncedStateClient | null;
/**
* Injects a client instance for tests and updates the cached endpoint.
* Also clears the subscription registry for test isolation.
* @param client Stub client instance or `null` to clear the cache.
* @param endpoint Endpoint associated with the injected client.
*/
export declare const setSyncedStateClientForTesting: (client: SyncedStateClient | null, endpoint?: string) => void;