import type { DBSchema, IDBPDatabase } from './entry.js'; interface Change { operation: 'add' | 'put' | 'delete'; storeName: string; key: IDBValidKey; value?: any; syncInProgressSince?: number; } interface Offset { id: IDBValidKey; updatedAt: any; } export interface SyncOptions { /** * Allow to override the request path for a given request */ buildPath: (operation: 'fetch' | 'add' | 'put' | 'delete', storeName: string, key?: IDBValidKey) => string | undefined; /** * Additional options for all HTTP requests. * * Reference: https://developer.mozilla.org/en-US/docs/Web/API/fetch */ fetchOptions: any; /** * Allow to override the query params of the fetch requests. Defaults to "?sort=updated_at:asc&size=100&after=2000-01-01T00:00:00.000Z,123". * * @param storeName * @param offset */ buildFetchParams: (storeName: string, offset: Offset) => URLSearchParams; /** * The name of the attribute that indicates the last updated date of the entity. * * @default "updatedAt" */ updatedAtAttribute: string; /** * The number of ms between two fetch requests for a given store. * * @default 30000 */ fetchInterval: number; /** * Entities without `keyPath`. * * @example * { * withoutKeyPath: { * common: [ * "user" * ] * } * } * * @see https://developer.mozilla.org/en-US/docs/Web/API/IDBObjectStore/keyPath */ withoutKeyPath: Record; } declare class FetchError extends Error { readonly response?: Response | undefined; constructor(message: string, response?: Response | undefined); } export declare class SyncManager { private readonly baseUrl; private readonly db; private readonly opts; private isOnline; private fetchLoop?; private pushLoop?; constructor(db: IDBPDatabase, baseUrl: string, opts?: Partial); /** * Starts the sync process with the remote server */ start(): void; /** * Stops the sync process */ stop(): void; private handleOnlineEvent; private startLoops; private cancelLoops; private handleOfflineEvent; private handleUpdateEvent; /** * Clears the local stores */ clear(): Promise<[void, void]>; /** * Returns whether a given entity currently has local changes that are not synced yet. * * @param storeName * @param key */ hasLocalChanges(storeName: string, key: IDBValidKey): Promise; /** * Called after some entities are successfully fetched from the remote server. * * @param storeName * @param entities * @param hasMore */ onfetchsuccess(storeName: string, entities: any[], hasMore: boolean): void; /** * Called when something goes wrong when fetching the changes from the remote server. * @param error */ onfetcherror(error: FetchError): void; /** * Called after a change is successfully pushed to the remote server. * * @param change */ onpushsuccess(change: Change): void; /** * Called when something goes wrong when pushing a change to the remote server. * * @param change * @param response * @param retryAfter * @param discardLocalChange * @param overrideRemoteChange */ onpusherror(change: Change, response: Response, retryAfter: (delay: number) => void, discardLocalChange: () => void, overrideRemoteChange: (entity: any) => void): void; } export {};