/** * TanStack Sync Collection * * Provides local-first sync with background synchronization. * Uses Electric-style shape subscriptions for real-time data. */ import type { BaseRecord, SyncCollectionOptions, Collection, SyncState, ConflictResolver } from '../types'; /** * Extended SyncCollection options with additional features */ interface ExtendedSyncCollectionOptions extends SyncCollectionOptions { retryOnReconnect?: boolean; onConflict?: ConflictResolver; batchMutations?: { maxBatchSize: number; batchDelayMs: number; }; onConnectionChange?: (event: { connected: boolean; previousState: SyncState; }) => void; autoReconnect?: { enabled: boolean; maxRetries: number; initialDelay: number; maxDelay: number; backoffMultiplier: number; }; healthCheck?: { interval: number; timeout: number; }; offlineSupport?: boolean; } /** * SyncCollection - A collection that maintains real-time sync */ export declare class SyncCollection implements Collection { readonly id: string; readonly table: string; readonly syncUrl: string; private items; private subscribers; private options; private syncState; private pollIntervalId; private pendingMutations; private debug; private retryOnReconnect; private onConflict?; private batchConfig?; private batchTimeout?; private pendingBatch; private onConnectionChange?; private autoReconnectConfig?; private _healthCheckConfig?; private offlineSupport; private isCurrentlyOffline; private syncStateChangeHandlers; private healthStatus; constructor(options: ExtendedSyncCollectionOptions); /** * Get the health check configuration */ getHealthCheckConfig(): { interval: number; timeout: number; } | undefined; /** * Expose fetchData for testing */ fetchData(): Promise; /** * Debug log helper - only logs if debug mode is enabled */ private debugLog; /** * Get all items in the collection */ getAll(): T[]; /** * Get item by ID */ get(id: string | number): T | undefined; /** * Connect to the sync endpoint and start syncing */ connect(): Promise; /** * Schedule an auto-reconnect attempt */ private scheduleReconnect; /** * Register a sync state change handler */ onSyncStateChange(handler: (event: string, state: SyncState) => void): () => void; /** * Get health status */ getHealthStatus(): { status: 'healthy' | 'degraded' | 'unhealthy'; lastCheckAt: number; latencyMs: number; consecutiveFailures: number; }; /** * Check if the collection is currently offline */ isOffline(): boolean; /** * Disconnect from sync */ disconnect(): void; /** * Clear all local data and pending mutations */ clear(): void; /** * Get the current subscriber count for debugging */ getSubscriberCount(): number; /** * Insert a new item */ insert(data: Omit & { id?: T['id']; }): Promise; /** * Update an existing item */ update(id: string | number, data: Partial): Promise; /** * Delete an item */ delete(id: string | number): Promise; /** * Subscribe to changes */ subscribe(callback: (items: T[]) => void): () => void; /** * Get current sync state */ getSyncState(): SyncState; /** * Get the pending mutation count */ getPendingMutationCount(): number; /** * Dispose of all resources - call when the collection is no longer needed */ dispose(): void; private buildSyncUrl; private mergeData; /** * Clear pending mutations that have been confirmed by the server. * - Insert/Update mutations are confirmed when the item appears in sync response * - Delete mutations are confirmed when the item does NOT appear in sync response */ private clearConfirmedMutations; private scheduleBatchFlush; private startPolling; private stopPolling; private generateId; private generateMutationId; private updatePendingCount; /** * Emit a sync state change event to all registered handlers */ private emitSyncStateEvent; private notifySubscribers; } /** * Factory function to create a SyncCollection. * * @param options - Configuration including table name, sync URL, and polling parameters * @returns A new SyncCollection instance configured for real-time sync * * @example * ```typescript * const todos = createSyncCollection({ * id: 'todos', * table: 'todos', * syncUrl: 'https://db.postgres.do/v1/shape', * pollInterval: 5000, * }) * await todos.connect() * ``` */ export declare function createSyncCollection(options: SyncCollectionOptions): SyncCollection; /** * SyncEngine - Manages multiple sync collections */ export interface SyncEngineOptions { baseUrl: string; } export declare class SyncEngine { private baseUrl; private collections; constructor(options: SyncEngineOptions); /** * Register a collection for syncing */ registerCollection(options: Omit, 'syncUrl'> & { syncUrl?: string; }): SyncCollection; /** * Get a registered collection */ getCollection(id: string): SyncCollection | undefined; /** * Get all collection IDs */ getCollectionIds(): string[]; /** * Connect all collections */ connectAll(): Promise; /** * Disconnect all collections */ disconnectAll(): void; /** * Get total pending mutations across all collections */ getTotalPendingMutations(): number; /** * Dispose all resources */ dispose(): Promise; } export {}; //# sourceMappingURL=sync-collection.d.ts.map