import { ClientMessage, DocumentBaseline, EventSubscriber, FileData, Operation, ServerMessage } from '@verdant-web/common'; import { Context } from '../context/context.js'; import { FilePullResult, FileUploadResult } from './FileSync.js'; import { PresenceManager } from './PresenceManager.js'; import { ServerSyncEndpointProviderConfig } from './ServerSyncEndpointProvider.js'; import { AutomaticTransportSelection } from './TransportSelector.js'; type SyncEvents = { onlineChange: (isOnline: boolean) => void; syncingChange: (syncing: boolean) => void; synced: () => void; /** When the server has lost data and re-requests data from the past */ serverReset: (since: string | null) => void; }; export type SyncTransportEvents = SyncEvents & { message: (message: ServerMessage) => void; }; export interface SyncTransport extends EventSubscriber { readonly presence: PresenceManager; readonly mode: SyncTransportMode; readonly hasSynced: boolean; send(message: ClientMessage): void; start(): Promise; ignoreIncoming(): void; stop(): Promise | void; destroy(): void; reconnect(): void; readonly isConnected: boolean; readonly status: 'active' | 'paused'; } export interface Sync extends EventSubscriber { setMode(mode: SyncTransportMode): void; setPullInterval(interval: number): void; readonly pullInterval: number; uploadFile(data: FileData): Promise; getFile(fileId: string): Promise; readonly presence: PresenceManager; send(message: ClientMessage): void; start(): Promise; stop(): Promise | void; ignoreIncoming(): void; destroy(): void; reconnect(): void; syncOnce(): Promise; readonly isConnected: boolean; readonly status: 'active' | 'paused'; readonly mode: SyncTransportMode; readonly hasSynced: boolean; } export declare class NoSync extends EventSubscriber implements Sync { readonly mode = "pull"; readonly hasSynced = false; send(): void; start(): Promise; stop(): void; ignoreIncoming(): void; destroy: () => void; reconnect(): void; setMode(): void; setPullInterval(): void; readonly isConnected = false; readonly status = "paused"; readonly pullInterval = 0; readonly presence: PresenceManager; constructor(ctx: Context); uploadFile: () => Promise<{ success: boolean; retry: boolean; }>; getFile: () => Promise; syncOnce: () => Promise; } export type SyncTransportMode = 'realtime' | 'pull'; export interface ServerSyncOptions extends ServerSyncEndpointProviderConfig { /** * When a client first connects, it will use this presence value. */ initialPresence: Presence; /** * Before connecting to the server, the local client will have * this value for their profile data. You can either cache and store * profile data from a previous connection or provide defaults like * empty strings. */ defaultProfile: Profile; /** * Provide `false` to disable transport selection. Transport selection * automatically switches between HTTP and WebSocket based sync depending * on the number of peers connected. If a user is alone, they will use * HTTP push/pull to sync changes. If another user joins, both users will * be upgraded to websockets. * * Provide `peers-only` to only automatically use websockets if other * users connect, but not if another device for the current user connects. * By default, automatic transport selection will upgrade to websockets if * another device from the current user connects, but if realtime sync is * not necessary for such cases, you can save bandwidth by disabling this. * * Turning off this feature allows you more control over the transport * which can be useful for low-power devices or to save server traffic. * To modify transport modes manually, utilize `client.sync.setMode`. * The built-in behavior is essentially switching modes based on * the number of peers detected by client.sync.presence. */ automaticTransportSelection?: AutomaticTransportSelection; initialTransport?: SyncTransportMode; /** * How long, in milliseconds, to keep a connected WebSocket without other * replicas before automatically switching to HTTP. Defaults to 1000. * Applies only when automatic transport selection is enabled. */ realtimeGracePeriod?: number; autoStart?: boolean; /** * Optionally specify an interval, in milliseconds, to poll the server * when in pull mode. */ pullInterval?: number; /** * Presence updates are batched to reduce number of requests / messages * sent to the server. You can specify the batching time slice, in milliseconds, */ presenceUpdateBatchTimeout?: number; /** * Experimental: sync messages over a broadcast channel between tabs. * Fixes tabs not reactively updating to changes when other tabs are open, * but is not yet thoroughly vetted. */ useBroadcastChannel?: boolean; /** * Listen for outgoing messages from the client to the server. * Not sure why you want to do this, but be careful. */ onOutgoingMessage?: (message: ClientMessage) => void; EXPERIMENTAL_backgroundSync?: boolean; } export declare class ServerSync extends EventSubscriber implements Sync { private webSocketSync; private pushPullSync; private fileSync; private activeSync; private endpointProvider; private onData; private broadcastChannel; private _activelySyncing; private _hasSynced; private transportSelector; readonly presence: PresenceManager; private onOutgoingMessage?; private ctx; constructor({ authEndpoint, fetchAuth, initialPresence, automaticTransportSelection, autoStart, initialTransport, realtimeGracePeriod, pullInterval, presenceUpdateBatchTimeout, defaultProfile, useBroadcastChannel, onOutgoingMessage, EXPERIMENTAL_backgroundSync, }: ServerSyncOptions, { ctx, onData, }: { ctx: Context; onData: (data: { operations: Operation[]; baselines?: DocumentBaseline[]; reset?: boolean; }) => Promise; }); get canDoRealtime(): boolean; get syncing(): boolean; get hasSynced(): boolean; private handleBroadcastChannelMessage; private handleMessage; private handleOnlineChange; private handlePresenceUpdate; private handlePresenceVersionReset; setMode: (transport: SyncTransportMode) => void; setPullInterval: (interval: number) => void; get pullInterval(): number; send: (message: ClientMessage) => Promise; uploadFile: (info: FileData) => Promise; getFile: (id: string) => Promise; private getSyncStartPromise; start: () => Promise; stop: () => void | Promise; ignoreIncoming(): void; destroy: () => void; reconnect: () => void; /** * Runs one isolated sync cycle over HTTP. This is useful for * syncing via a periodic background job in a service worker, * which keeps a client up to date while the app isn't open. */ syncOnce: () => Promise; get isConnected(): boolean; get status(): "active" | "paused"; get mode(): SyncTransportMode; } export {};