import { ILogger } from 'js-logger'; import { SyncStatus } from '../db/crud/SyncStatus.js'; import { BaseListener, BaseObserver } from '../utils/BaseObserver.js'; import { PowerSyncBackendConnector } from './connection/PowerSyncBackendConnector.js'; import { AdditionalConnectionOptions, InternalConnectionOptions, StreamingSyncImplementation, SubscribedStream } from './sync/stream/AbstractStreamingSyncImplementation.js'; import { SyncStream } from './sync/sync-streams.js'; /** * @internal */ export interface ConnectionManagerSyncImplementationResult { sync: StreamingSyncImplementation; /** * Additional cleanup function which is called after the sync stream implementation * is disposed. */ onDispose: () => Promise | void; } /** * The subset of {@link AbstractStreamingSyncImplementationOptions} managed by the connection manager. * * @internal */ export interface CreateSyncImplementationOptions extends AdditionalConnectionOptions { subscriptions: SubscribedStream[]; } export interface InternalSubscriptionAdapter { firstStatusMatching(predicate: (status: SyncStatus) => any, abort?: AbortSignal): Promise; resolveOfflineSyncStatus(): Promise; rustSubscriptionsCommand(payload: any): Promise; } /** * @internal */ export interface ConnectionManagerOptions { createSyncImplementation(connector: PowerSyncBackendConnector, options: CreateSyncImplementationOptions): Promise; logger: ILogger; } type StoredConnectionOptions = { connector: PowerSyncBackendConnector; options: InternalConnectionOptions; }; /** * @internal */ export interface ConnectionManagerListener extends BaseListener { syncStreamCreated: (sync: StreamingSyncImplementation) => void; } /** * @internal */ export declare class ConnectionManager extends BaseObserver { protected options: ConnectionManagerOptions; /** * Tracks active connection attempts */ protected connectingPromise: Promise | null; /** * Tracks actively instantiating a streaming sync implementation. */ protected syncStreamInitPromise: Promise | null; /** * Active disconnect operation. Calling disconnect multiple times * will resolve to the same operation. */ protected disconnectingPromise: Promise | null; /** * Tracks the last parameters supplied to `connect` calls. * Calling `connect` multiple times in succession will result in: * - 1 pending connection operation which will be aborted. * - updating the last set of parameters while waiting for the pending * attempt to be aborted * - internally connecting with the last set of parameters */ protected pendingConnectionOptions: StoredConnectionOptions | null; syncStreamImplementation: StreamingSyncImplementation | null; /** * Additional cleanup function which is called after the sync stream implementation * is disposed. */ protected syncDisposer: (() => Promise | void) | null; /** * Subscriptions managed in this connection manager. * * On the web, these local subscriptions are merged across tabs by a shared worker. */ private locallyActiveSubscriptions; constructor(options: ConnectionManagerOptions); get connector(): PowerSyncBackendConnector | null; get connectionOptions(): InternalConnectionOptions | null; get logger(): ILogger; close(): Promise; connect(connector: PowerSyncBackendConnector, options: InternalConnectionOptions): Promise; protected connectInternal(): Promise; /** * Close the sync connection. * * Use {@link connect} to connect again. */ disconnect(): Promise; protected disconnectInternal(): Promise; protected performDisconnect(): Promise; stream(adapter: InternalSubscriptionAdapter, name: string, parameters: Record | null): SyncStream; /** * @internal exposed for testing */ get activeStreams(): { name: string; params: Record | null; }[]; private subscriptionsMayHaveChanged; } export {};