/** * db4ai Real-Time Subscription Manager * * Provides real-time subscription functionality for query results and single documents. * Built on top of @db4/rpc WebSocket subscriptions. * * @packageDocumentation */ import type { WhereClause } from './query.js'; /** * Subscription state. */ export type SubscriptionState = 'pending' | 'active' | 'paused' | 'closed' | 'error'; /** * Subscription event type. */ export type SubscriptionEventType = 'change' | 'create' | 'update' | 'delete' | 'error' | 'initial'; /** * Subscription event data. */ export interface SubscriptionEvent { /** Event type */ type: SubscriptionEventType; /** Event data */ data: T; /** Previous value (for updates) */ previousData?: T; /** Timestamp of the event */ timestamp: number; } /** * Callback function for query subscriptions. */ export type QuerySubscriptionCallback = (docs: T[], event?: SubscriptionEvent) => void; /** * Callback function for single document subscriptions. */ export type DocumentSubscriptionCallback = (doc: T | null, event?: SubscriptionEvent) => void; /** * Unsubscribe function returned by subscribe methods. */ export type Unsubscribe = () => void; /** * Subscription options. */ export interface SubscriptionOptions { /** Debounce changes before emitting (ms) */ debounce?: number; /** Include initial data on subscribe */ includeInitial?: boolean; } /** * WebSocket connection state. */ export type WebSocketState = 'connecting' | 'open' | 'closing' | 'closed'; /** * Reconnection options. */ export interface ReconnectOptions { /** Maximum reconnect attempts */ maxAttempts?: number; /** Initial delay in ms */ initialDelay?: number; /** Maximum delay in ms */ maxDelay?: number; /** Backoff multiplier */ backoffMultiplier?: number; } /** * Manages real-time subscriptions for db4ai. */ export declare class SubscriptionManager { private subscriptions; private subscriptionCounter; private ws; private wsState; private reconnectAttempts; private reconnectTimer; private pendingSubscriptions; private readonly url; private readonly reconnectOptions; private messageHandlers; private mockDataStore; private mockQueryResults; constructor(url: string, options?: ReconnectOptions); /** * Connect to the WebSocket server. */ connect(): Promise; /** * Disconnect from the WebSocket server. */ disconnect(): void; /** * Get the current WebSocket state. */ get connectionState(): WebSocketState; /** * Subscribe to query results. */ subscribe(filter: WhereClause, callback: QuerySubscriptionCallback, options?: SubscriptionOptions): Unsubscribe; /** * Subscribe to a single document by ID. */ subscribeOne(documentId: string, callback: DocumentSubscriptionCallback, options?: SubscriptionOptions): Unsubscribe; /** * Unsubscribe from a subscription. */ private unsubscribe; /** * Get all active subscriptions. */ getSubscriptions(): Array<{ id: string; type: 'query' | 'document'; state: SubscriptionState; }>; /** * Emit a change event for testing purposes. */ emitChange(type: SubscriptionEventType, data: T, documentId?: string): void; /** * Simulate connection loss for testing. */ simulateConnectionLoss(): void; /** * Simulate reconnection for testing. */ simulateReconnect(): Promise; /** * Set mock data for testing. */ setMockData(documentId: string, data: TDocument): void; /** * Set mock query results for testing. */ setMockQueryResults(filterKey: string, results: TDocument[]): void; private generateSubscriptionId; private sendSubscribe; private handleMessage; private handleDisconnect; private resubscribeAll; private getMatchingDocuments; private documentMatchesFilter; private getDocumentId; } /** * Creates a subscription interface for a collection. */ export declare function createCollectionSubscription(manager: SubscriptionManager, collectionName: string): { /** * Subscribe to query results. */ subscribe(filter: WhereClause, callback: QuerySubscriptionCallback, options?: SubscriptionOptions): Unsubscribe; /** * Subscribe to a single document by ID. */ subscribeOne(documentId: string, callback: DocumentSubscriptionCallback, options?: SubscriptionOptions): Unsubscribe; }; /** * Create a new subscription manager. */ export declare function createSubscriptionManager(url: string, options?: ReconnectOptions): SubscriptionManager; //# sourceMappingURL=subscription.d.ts.map