{"version":3,"file":"offline-persistence.cjs","names":[],"sources":["../../src/query/offline-persistence.ts"],"sourcesContent":["import { dehydrate, hydrate, type QueryClient } from \"@tanstack/react-query\";\nimport { createOfflineStore } from \"../offline/create-offline-store\";\n\n/** Options for {@link persistQueryClientOffline}. */\nexport interface OfflineQueryPersistenceOptions {\n    /** The `QueryClient` to snapshot and restore. */\n    queryClient: QueryClient;\n    /** IndexedDB database name for the snapshot. Default `\"TempestQueryCache\"`. */\n    databaseName?: string;\n    /** Row key under which the snapshot is stored. Default `\"tanstack-query\"`. */\n    key?: string;\n    /**\n     * Minimum gap between snapshot writes, in ms — cache churn is coalesced into\n     * one trailing write per window. Default `1000`.\n     */\n    throttleMs?: number;\n}\n\n/** Handle returned by {@link persistQueryClientOffline}. */\nexport interface OfflineQueryPersistence {\n    /**\n     * Load the persisted snapshot into the `QueryClient`. Call once on boot,\n     * before rendering queries, so cached data is available offline.\n     */\n    restore: () => Promise<void>;\n    /** Write the current cache snapshot immediately (bypassing the throttle). */\n    flush: () => Promise<void>;\n    /** Stop persisting and drop the pending write. */\n    unsubscribe: () => void;\n    /** Delete the persisted snapshot (e.g. on logout). */\n    clear: () => Promise<void>;\n}\n\ninterface Snapshot {\n    id: string;\n    state: ReturnType<typeof dehydrate>;\n}\n\n/**\n * Persist a TanStack `QueryClient` cache to IndexedDB (via\n * {@link createOfflineStore}) and restore it on boot — so a reload or a cold\n * offline start shows the last-known data instead of empty screens.\n *\n * Self-contained: uses `dehydrate`/`hydrate` from `@tanstack/react-query`\n * directly, so no `@tanstack/react-query-persist-client` dependency is needed.\n * Writes are throttled and subscribe to the query cache; call `unsubscribe` on\n * teardown. Dexie is a peer dependency of the offline store — install it.\n *\n * @param options - The client plus storage/throttle configuration.\n * @returns A handle with `restore`, `flush`, `clear` and `unsubscribe`.\n *\n * @example\n * const persistence = persistQueryClientOffline({ queryClient });\n * await persistence.restore(); // before first render\n * // ... later, on logout:\n * await persistence.clear();\n */\nexport function persistQueryClientOffline(\n    options: OfflineQueryPersistenceOptions,\n): OfflineQueryPersistence {\n    const { queryClient, throttleMs = 1000 } = options;\n    const key = options.key ?? \"tanstack-query\";\n    const store = createOfflineStore<Snapshot, string>({\n        databaseName: options.databaseName ?? \"TempestQueryCache\",\n        version: 1,\n        tableName: \"cache\",\n        indexes: \"&id\",\n        keyPath: \"id\",\n    });\n\n    async function save(): Promise<void> {\n        await store.put({ id: key, state: dehydrate(queryClient) });\n    }\n\n    let timer: ReturnType<typeof setTimeout> | null = null;\n    function schedule(): void {\n        if (timer) return;\n        timer = setTimeout(() => {\n            timer = null;\n            void save();\n        }, throttleMs);\n    }\n\n    const unsubscribeCache = queryClient.getQueryCache().subscribe(schedule);\n\n    return {\n        async restore() {\n            const snapshot = await store.get(key);\n            if (snapshot) hydrate(queryClient, snapshot.state);\n        },\n        async flush() {\n            if (timer) {\n                clearTimeout(timer);\n                timer = null;\n            }\n            await save();\n        },\n        unsubscribe() {\n            if (timer) {\n                clearTimeout(timer);\n                timer = null;\n            }\n            unsubscribeCache();\n        },\n        async clear() {\n            await store.delete(key);\n        },\n    };\n}\n"],"mappings":"8FAyDA,SAAgB,EACZ,EACuB,CACvB,GAAM,CAAE,cAAa,aAAa,KAAS,EACrC,EAAM,EAAQ,KAAO,iBACrB,EAAQ,EAAA,mBAAqC,CAC/C,aAAc,EAAQ,cAAgB,oBACtC,QAAS,EACT,UAAW,QACX,QAAS,MACT,QAAS,IACb,CAAC,EAED,eAAe,GAAsB,CACjC,MAAM,EAAM,IAAI,CAAE,GAAI,EAAK,OAAA,EAAO,EAAA,UAAA,CAAU,CAAW,CAAE,CAAC,CAC9D,CAEA,IAAI,EAA8C,KAClD,SAAS,GAAiB,CAClB,AACJ,IAAQ,eAAiB,CACrB,EAAQ,KACR,EAAU,CACd,EAAG,CAAU,CACjB,CAEA,IAAM,EAAmB,EAAY,cAAc,CAAC,CAAC,UAAU,CAAQ,EAEvE,MAAO,CACH,MAAM,SAAU,CACZ,IAAM,EAAW,MAAM,EAAM,IAAI,CAAG,EAChC,IAAU,EAAA,EAAA,QAAA,CAAQ,EAAa,EAAS,KAAK,CACrD,EACA,MAAM,OAAQ,CACV,AAEI,KADA,aAAa,CAAK,EACV,MAEZ,MAAM,EAAK,CACf,EACA,aAAc,CACV,AAEI,KADA,aAAa,CAAK,EACV,MAEZ,EAAiB,CACrB,EACA,MAAM,OAAQ,CACV,MAAM,EAAM,OAAO,CAAG,CAC1B,CACJ,CACJ"}