import type { BasePowerSyncDatabase } from '../BasePowerSyncDatabase.js'; import { CreateDiffTriggerOptions, DiffTriggerOperation, LockContext, Schema, TrackDiffOptions, TriggerManager, TriggerRemoveCallback, TriggerRemoveCallbackOptions } from '@powersync/common'; /** * @experimental * @internal */ export interface TriggerManagerConfig { claimManager: TriggerClaimManager; } /** * @experimental * Manages claims on persisted SQLite triggers and destination tables to enable proper cleanup * when they are no longer actively in use. * * When using persisted triggers (especially for OPFS multi-tab scenarios), we need a reliable way to determine which resources are still actively in use across different connections/tabs so stale resources can be safely cleaned up without interfering with active triggers. * * A cleanup process runs * on database creation (and every 2 minutes) that: * 1. Queries for existing managed persisted resources * 2. Checks with the claim manager if any consumer is actively using those resources * 3. Deletes unused resources */ export interface TriggerClaimManager { /** * Obtains or marks a claim on a certain identifier. * @returns a callback to release the claim. */ obtainClaim: (identifier: string) => Promise<() => Promise>; /** * Checks if a claim is present for an identifier. */ checkClaim: (identifier: string) => Promise; } export type TriggerManagerImplOptions = TriggerManagerConfig & { db: BasePowerSyncDatabase; schema: Schema; }; export type TriggerManagerImplConfiguration = { useStorageByDefault: boolean; }; export declare const DEFAULT_TRIGGER_MANAGER_CONFIGURATION: TriggerManagerImplConfiguration; /** * @internal * @experimental */ export declare class TriggerManagerImpl implements TriggerManager { protected options: TriggerManagerImplOptions; protected schema: Schema; protected defaultConfig: TriggerManagerImplConfiguration; protected cleanupTimeout: ReturnType | null; protected isDisposed: boolean; constructor(options: TriggerManagerImplOptions); protected get db(): BasePowerSyncDatabase; protected getUUID(ctx?: LockContext): Promise; protected removeTriggers(tx: LockContext, triggerIds: string[]): Promise; dispose(): void; /** * Updates default config settings for platform specific use-cases. */ updateDefaults(config: TriggerManagerImplConfiguration): void; protected generateTriggerName(operation: DiffTriggerOperation, destinationTable: string, triggerId: string): string; /** * Cleanup any SQLite triggers or tables that are no longer in use. */ cleanupResources(): Promise; createDiffTrigger(options: CreateDiffTriggerOptions): Promise<(options?: TriggerRemoveCallbackOptions) => Promise>; trackTableDiff(options: TrackDiffOptions): Promise; }