@tanstack/query-persist-client-core 5.104.1 → 5.104.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/createPersister.cjs +65 -8
- package/build/legacy/createPersister.cjs.map +1 -1
- package/build/legacy/createPersister.d.cts +50 -9
- package/build/legacy/createPersister.d.cts.map +1 -1
- package/build/legacy/createPersister.d.ts +50 -9
- package/build/legacy/createPersister.d.ts.map +1 -1
- package/build/legacy/createPersister.js +65 -8
- package/build/legacy/createPersister.js.map +1 -1
- package/build/legacy/persist.cjs +26 -12
- package/build/legacy/persist.cjs.map +1 -1
- package/build/legacy/persist.d.cts +73 -14
- package/build/legacy/persist.d.cts.map +1 -1
- package/build/legacy/persist.d.ts +73 -14
- package/build/legacy/persist.d.ts.map +1 -1
- package/build/legacy/persist.js +26 -12
- package/build/legacy/persist.js.map +1 -1
- package/build/legacy/retryStrategies.cjs +6 -0
- package/build/legacy/retryStrategies.cjs.map +1 -1
- package/build/legacy/retryStrategies.d.cts +10 -0
- package/build/legacy/retryStrategies.d.cts.map +1 -1
- package/build/legacy/retryStrategies.d.ts +10 -0
- package/build/legacy/retryStrategies.d.ts.map +1 -1
- package/build/legacy/retryStrategies.js +6 -0
- package/build/legacy/retryStrategies.js.map +1 -1
- package/build/modern/createPersister.cjs +65 -8
- package/build/modern/createPersister.cjs.map +1 -1
- package/build/modern/createPersister.d.cts +50 -9
- package/build/modern/createPersister.d.cts.map +1 -1
- package/build/modern/createPersister.d.ts +50 -9
- package/build/modern/createPersister.d.ts.map +1 -1
- package/build/modern/createPersister.js +65 -8
- package/build/modern/createPersister.js.map +1 -1
- package/build/modern/persist.cjs +26 -12
- package/build/modern/persist.cjs.map +1 -1
- package/build/modern/persist.d.cts +73 -14
- package/build/modern/persist.d.cts.map +1 -1
- package/build/modern/persist.d.ts +73 -14
- package/build/modern/persist.d.ts.map +1 -1
- package/build/modern/persist.js +26 -12
- package/build/modern/persist.js.map +1 -1
- package/build/modern/retryStrategies.cjs +6 -0
- package/build/modern/retryStrategies.cjs.map +1 -1
- package/build/modern/retryStrategies.d.cts +10 -0
- package/build/modern/retryStrategies.d.cts.map +1 -1
- package/build/modern/retryStrategies.d.ts +10 -0
- package/build/modern/retryStrategies.d.ts.map +1 -1
- package/build/modern/retryStrategies.js +6 -0
- package/build/modern/retryStrategies.js.map +1 -1
- package/package.json +4 -4
- package/src/createPersister.ts +104 -10
- package/src/persist.ts +77 -16
- package/src/retryStrategies.ts +10 -0
package/build/modern/persist.cjs
CHANGED
|
@@ -1,23 +1,30 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
let _tanstack_query_core = require("@tanstack/query-core");
|
|
3
3
|
//#region src/persist.ts
|
|
4
|
-
/**
|
|
5
|
-
* Checks if emitted event is about cache change and not about observers.
|
|
6
|
-
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
7
|
-
*/
|
|
8
4
|
const cacheEventTypes = [
|
|
9
5
|
"added",
|
|
10
6
|
"removed",
|
|
11
7
|
"updated"
|
|
12
8
|
];
|
|
9
|
+
/**
|
|
10
|
+
* Checks if emitted event is about cache change and not about observers.
|
|
11
|
+
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
12
|
+
* @param eventType - The type of the emitted event.
|
|
13
|
+
* @returns `true` if the event is `'added'`, `'removed'`, or `'updated'`.
|
|
14
|
+
*/
|
|
13
15
|
function isCacheEventType(eventType) {
|
|
14
16
|
return cacheEventTypes.includes(eventType);
|
|
15
17
|
}
|
|
16
18
|
/**
|
|
17
|
-
* Restores persisted data to the QueryCache
|
|
18
|
-
* - data obtained from persister.restoreClient
|
|
19
|
-
* - data is hydrated using hydrateOptions
|
|
20
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
19
|
+
* Restores persisted data to the QueryCache.
|
|
20
|
+
* - data obtained from persister.restoreClient.
|
|
21
|
+
* - data is hydrated using hydrateOptions.
|
|
22
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient.
|
|
23
|
+
* @param options - The `queryClient` to restore into, the `persister`, and the `maxAge`, `buster`,
|
|
24
|
+
* and `hydrateOptions` that decide whether and how the persisted data is restored.
|
|
25
|
+
* @returns A promise that resolves once the data is restored or removed. If restoring throws, it
|
|
26
|
+
* attempts to remove the persisted data and then rejects with the restore error, or with the removal
|
|
27
|
+
* error if the removal fails too.
|
|
21
28
|
*/
|
|
22
29
|
async function persistQueryClientRestore({ queryClient, persister, maxAge = 864e5, buster = "", hydrateOptions }) {
|
|
23
30
|
try {
|
|
@@ -40,9 +47,11 @@ async function persistQueryClientRestore({ queryClient, persister, maxAge = 864e
|
|
|
40
47
|
}
|
|
41
48
|
}
|
|
42
49
|
/**
|
|
43
|
-
* Persists data from the QueryCache
|
|
44
|
-
* - data dehydrated using dehydrateOptions
|
|
45
|
-
* - data is persisted using persister.persistClient
|
|
50
|
+
* Persists data from the QueryCache.
|
|
51
|
+
* - data dehydrated using dehydrateOptions.
|
|
52
|
+
* - data is persisted using persister.persistClient.
|
|
53
|
+
* @param options - The `queryClient` to persist, the `persister`, the `buster`, and the
|
|
54
|
+
* `dehydrateOptions`.
|
|
46
55
|
*/
|
|
47
56
|
async function persistQueryClientSave({ queryClient, persister, buster = "", dehydrateOptions }) {
|
|
48
57
|
const persistClient = {
|
|
@@ -53,7 +62,9 @@ async function persistQueryClientSave({ queryClient, persister, buster = "", deh
|
|
|
53
62
|
await persister.persistClient(persistClient);
|
|
54
63
|
}
|
|
55
64
|
/**
|
|
56
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
65
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting).
|
|
66
|
+
* @param props - The `queryClient` to monitor, the `persister`, the `buster`, and the
|
|
67
|
+
* `dehydrateOptions` used for each save.
|
|
57
68
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
58
69
|
*/
|
|
59
70
|
function persistQueryClientSubscribe(props) {
|
|
@@ -70,6 +81,9 @@ function persistQueryClientSubscribe(props) {
|
|
|
70
81
|
}
|
|
71
82
|
/**
|
|
72
83
|
* Restores persisted data to QueryCache and persists further changes.
|
|
84
|
+
* @param props - The options for both restoring and saving.
|
|
85
|
+
* @returns A tuple of a function that stops persisting, and a promise that resolves once the
|
|
86
|
+
* restore is done.
|
|
73
87
|
*/
|
|
74
88
|
function persistQueryClient(props) {
|
|
75
89
|
let hasUnsubscribed = false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist.cjs","names":["dehydrate"],"sources":["../../src/persist.ts"],"sourcesContent":["import { dehydrate, hydrate } from '@tanstack/query-core'\nimport type {\n DehydrateOptions,\n DehydratedState,\n HydrateOptions,\n NotifyEventType,\n QueryClient,\n} from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient: (persistClient: PersistedClient) => Promisable<void>\n restoreClient: () => Promisable<PersistedClient | undefined>\n removeClient: () => Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClientRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n
|
|
1
|
+
{"version":3,"file":"persist.cjs","names":["dehydrate"],"sources":["../../src/persist.ts"],"sourcesContent":["import { dehydrate, hydrate } from '@tanstack/query-core'\nimport type {\n DehydrateOptions,\n DehydratedState,\n HydrateOptions,\n NotifyEventType,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * A value, or a promise-like that resolves to it.\n */\nexport type Promisable<T> = T | PromiseLike<T>\n\n/**\n * Saves, restores, and removes a {@link PersistedClient} in a storage.\n */\nexport interface Persister {\n /**\n * Saves the persisted client to the storage.\n */\n persistClient: (persistClient: PersistedClient) => Promisable<void>\n /**\n * Reads the persisted client from the storage, or `undefined` if nothing was saved.\n */\n restoreClient: () => Promisable<PersistedClient | undefined>\n /**\n * Removes the persisted client from the storage.\n */\n removeClient: () => Promisable<void>\n}\n\n/**\n * The dehydrated state of a `QueryClient` as it is persisted, with the time it was saved and its\n * `buster`.\n */\nexport interface PersistedClient {\n /**\n * When the client was saved, in milliseconds since the epoch. Compared with `maxAge` on restore.\n */\n timestamp: number\n /**\n * The `buster` the client was saved with. A persisted client with a different `buster` is\n * discarded.\n */\n buster: string\n /**\n * The dehydrated cache of the `QueryClient`.\n */\n clientState: DehydratedState\n}\n\n/**\n * The options shared by restoring and saving a persisted `QueryClient`.\n */\nexport interface PersistQueryClientRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /**\n * The Persister interface for storing and restoring the cache\n * to/from a persisted location\n */\n persister: Persister\n /**\n * A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string\n */\n buster?: string\n}\n\n/**\n * Options for restoring a persisted `QueryClient` with {@link persistQueryClientRestore}.\n */\nexport interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {\n /**\n * The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded\n */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\n/**\n * Options for saving a `QueryClient` with {@link persistQueryClientSave}.\n */\nexport interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\n/**\n * Options for {@link persistQueryClient}, which both restores and saves a `QueryClient`.\n */\nexport interface PersistQueryClientOptions\n extends\n PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClientRootOptions {}\n\nconst cacheEventTypes: Array<NotifyEventType> = ['added', 'removed', 'updated']\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n * @param eventType - The type of the emitted event.\n * @returns `true` if the event is `'added'`, `'removed'`, or `'updated'`.\n */\nfunction isCacheEventType(eventType: NotifyEventType) {\n return cacheEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache.\n * - data obtained from persister.restoreClient.\n * - data is hydrated using hydrateOptions.\n * If data is expired, busted, empty, or throws, it runs persister.removeClient.\n * @param options - The `queryClient` to restore into, the `persister`, and the `maxAge`, `buster`,\n * and `hydrateOptions` that decide whether and how the persisted data is restored.\n * @returns A promise that resolves once the data is restored or removed. If restoring throws, it\n * attempts to remove the persisted data and then rejects with the restore error, or with the removal\n * error if the removal fails too.\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n return persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n return persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n\n await persister.removeClient()\n\n throw err\n }\n}\n\n/**\n * Persists data from the QueryCache.\n * - data dehydrated using dehydrateOptions.\n * - data is persisted using persister.persistClient.\n * @param options - The `queryClient` to persist, the `persister`, the `buster`, and the\n * `dehydrateOptions`.\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting).\n * @param props - The `queryClient` to monitor, the `persister`, the `buster`, and the\n * `dehydrateOptions` used for each save.\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unsubscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unsubscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n * @param props - The options for both restoring and saving.\n * @returns A tuple of a function that stops persisting, and a promise that resolves once the\n * restore is done.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"mappings":";;;AAqGA,MAAM,kBAA0C;CAAC;CAAS;CAAW;AAAS;;;;;;;AAQ9E,SAAS,iBAAiB,WAA4B;CACpD,OAAO,gBAAgB,SAAS,SAAS;AAC3C;;;;;;;;;;;;AAaA,eAAsB,0BAA0B,EAC9C,aACA,WACA,SAAS,OACT,SAAS,IACT,kBACqC;CACrC,IAAI;EACF,MAAM,kBAAkB,MAAM,UAAU,cAAc;EAEtD,IAAI,iBAAiB;GACnB,IAAI,gBAAgB,WAAW;IAC7B,MAAM,UAAU,KAAK,IAAI,IAAI,gBAAgB,YAAY;IACzD,MAAM,SAAS,gBAAgB,WAAW;IAC1C,IAAI,WAAW,QACb,OAAO,UAAU,aAAa;SAE9B,CAAA,GAAA,qBAAA,QAAA,CAAQ,aAAa,gBAAgB,aAAa,cAAc;GAEpE,OACE,OAAO,UAAU,aAAa;EAElC;CACF,SAAS,KAAK;EACZ,IAAI,QAAQ,IAAI,aAAa,cAAc;GACzC,QAAQ,MAAM,GAAG;GACjB,QAAQ,KACN,0IACF;EACF;EAEA,MAAM,UAAU,aAAa;EAE7B,MAAM;CACR;AACF;;;;;;;;AASA,eAAsB,uBAAuB,EAC3C,aACA,WACA,SAAS,IACT,oBACkC;CAClC,MAAM,gBAAiC;EACrC;EACA,WAAW,KAAK,IAAI;EACpB,cAAA,GAAaA,qBAAAA,UAAAA,CAAU,aAAa,gBAAgB;CACtD;CAEA,MAAM,UAAU,cAAc,aAAa;AAC7C;;;;;;;AAQA,SAAgB,4BACd,OACA;CACA,MAAM,wBAAwB,MAAM,YACjC,cAAc,CAAC,CACf,WAAW,UAAU;EACpB,IAAI,iBAAiB,MAAM,IAAI,GAC7B,uBAAuB,KAAK;CAEhC,CAAC;CAEH,MAAM,2BAA2B,MAAM,YACpC,iBAAiB,CAAC,CAClB,WAAW,UAAU;EACpB,IAAI,iBAAiB,MAAM,IAAI,GAC7B,uBAAuB,KAAK;CAEhC,CAAC;CAEH,aAAa;EACX,sBAAsB;EACtB,yBAAyB;CAC3B;AACF;;;;;;;AAQA,SAAgB,mBACd,OAC6B;CAC7B,IAAI,kBAAkB;CACtB,IAAI;CACJ,MAAM,oBAAoB;EACxB,kBAAkB;EAClB,gCAAgC;CAClC;CAUA,OAAO,CAAC,aAPe,0BAA0B,KAAK,CAAC,CAAC,WAAW;EACjE,IAAI,CAAC,iBAEH,gCAAgC,4BAA4B,KAAK;CAErE,CAEkC,CAAC;AACrC"}
|
|
@@ -1,59 +1,118 @@
|
|
|
1
1
|
import { DehydrateOptions, DehydratedState, HydrateOptions, QueryClient } from "@tanstack/query-core";
|
|
2
2
|
//#region src/persist.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A value, or a promise-like that resolves to it.
|
|
5
|
+
*/
|
|
3
6
|
export type Promisable<T> = T | PromiseLike<T>;
|
|
7
|
+
/**
|
|
8
|
+
* Saves, restores, and removes a {@link PersistedClient} in a storage.
|
|
9
|
+
*/
|
|
4
10
|
export interface Persister {
|
|
11
|
+
/**
|
|
12
|
+
* Saves the persisted client to the storage.
|
|
13
|
+
*/
|
|
5
14
|
persistClient: (persistClient: PersistedClient) => Promisable<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Reads the persisted client from the storage, or `undefined` if nothing was saved.
|
|
17
|
+
*/
|
|
6
18
|
restoreClient: () => Promisable<PersistedClient | undefined>;
|
|
19
|
+
/**
|
|
20
|
+
* Removes the persisted client from the storage.
|
|
21
|
+
*/
|
|
7
22
|
removeClient: () => Promisable<void>;
|
|
8
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* The dehydrated state of a `QueryClient` as it is persisted, with the time it was saved and its
|
|
26
|
+
* `buster`.
|
|
27
|
+
*/
|
|
9
28
|
export interface PersistedClient {
|
|
29
|
+
/**
|
|
30
|
+
* When the client was saved, in milliseconds since the epoch. Compared with `maxAge` on restore.
|
|
31
|
+
*/
|
|
10
32
|
timestamp: number;
|
|
33
|
+
/**
|
|
34
|
+
* The `buster` the client was saved with. A persisted client with a different `buster` is
|
|
35
|
+
* discarded.
|
|
36
|
+
*/
|
|
11
37
|
buster: string;
|
|
38
|
+
/**
|
|
39
|
+
* The dehydrated cache of the `QueryClient`.
|
|
40
|
+
*/
|
|
12
41
|
clientState: DehydratedState;
|
|
13
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The options shared by restoring and saving a persisted `QueryClient`.
|
|
45
|
+
*/
|
|
14
46
|
export interface PersistQueryClientRootOptions {
|
|
15
47
|
/** The QueryClient to persist */
|
|
16
48
|
queryClient: QueryClient;
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
49
|
+
/**
|
|
50
|
+
* The Persister interface for storing and restoring the cache
|
|
51
|
+
* to/from a persisted location
|
|
52
|
+
*/
|
|
19
53
|
persister: Persister;
|
|
20
|
-
/**
|
|
21
|
-
*
|
|
54
|
+
/**
|
|
55
|
+
* A unique string that can be used to forcefully
|
|
56
|
+
* invalidate existing caches if they do not share the same buster string
|
|
57
|
+
*/
|
|
22
58
|
buster?: string;
|
|
23
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Options for restoring a persisted `QueryClient` with {@link persistQueryClientRestore}.
|
|
62
|
+
*/
|
|
24
63
|
export interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
|
|
25
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* The max-allowed age of the cache in milliseconds.
|
|
26
66
|
* If a persisted cache is found that is older than this
|
|
27
|
-
* time, it will be discarded
|
|
67
|
+
* time, it will be discarded
|
|
68
|
+
*/
|
|
28
69
|
maxAge?: number;
|
|
29
70
|
/** The options passed to the hydrate function */
|
|
30
71
|
hydrateOptions?: HydrateOptions;
|
|
31
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Options for saving a `QueryClient` with {@link persistQueryClientSave}.
|
|
75
|
+
*/
|
|
32
76
|
export interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
|
|
33
77
|
/** The options passed to the dehydrate function */
|
|
34
78
|
dehydrateOptions?: DehydrateOptions;
|
|
35
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Options for {@link persistQueryClient}, which both restores and saves a `QueryClient`.
|
|
82
|
+
*/
|
|
36
83
|
export interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {}
|
|
37
84
|
/**
|
|
38
|
-
* Restores persisted data to the QueryCache
|
|
39
|
-
* - data obtained from persister.restoreClient
|
|
40
|
-
* - data is hydrated using hydrateOptions
|
|
41
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
85
|
+
* Restores persisted data to the QueryCache.
|
|
86
|
+
* - data obtained from persister.restoreClient.
|
|
87
|
+
* - data is hydrated using hydrateOptions.
|
|
88
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient.
|
|
89
|
+
* @param options - The `queryClient` to restore into, the `persister`, and the `maxAge`, `buster`,
|
|
90
|
+
* and `hydrateOptions` that decide whether and how the persisted data is restored.
|
|
91
|
+
* @returns A promise that resolves once the data is restored or removed. If restoring throws, it
|
|
92
|
+
* attempts to remove the persisted data and then rejects with the restore error, or with the removal
|
|
93
|
+
* error if the removal fails too.
|
|
42
94
|
*/
|
|
43
95
|
export declare function persistQueryClientRestore({ queryClient, persister, maxAge, buster, hydrateOptions }: PersistedQueryClientRestoreOptions): Promise<void>;
|
|
44
96
|
/**
|
|
45
|
-
* Persists data from the QueryCache
|
|
46
|
-
* - data dehydrated using dehydrateOptions
|
|
47
|
-
* - data is persisted using persister.persistClient
|
|
97
|
+
* Persists data from the QueryCache.
|
|
98
|
+
* - data dehydrated using dehydrateOptions.
|
|
99
|
+
* - data is persisted using persister.persistClient.
|
|
100
|
+
* @param options - The `queryClient` to persist, the `persister`, the `buster`, and the
|
|
101
|
+
* `dehydrateOptions`.
|
|
48
102
|
*/
|
|
49
103
|
export declare function persistQueryClientSave({ queryClient, persister, buster, dehydrateOptions }: PersistedQueryClientSaveOptions): Promise<void>;
|
|
50
104
|
/**
|
|
51
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
105
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting).
|
|
106
|
+
* @param props - The `queryClient` to monitor, the `persister`, the `buster`, and the
|
|
107
|
+
* `dehydrateOptions` used for each save.
|
|
52
108
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
53
109
|
*/
|
|
54
110
|
export declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
55
111
|
/**
|
|
56
112
|
* Restores persisted data to QueryCache and persists further changes.
|
|
113
|
+
* @param props - The options for both restoring and saving.
|
|
114
|
+
* @returns A tuple of a function that stops persisting, and a promise that resolves once the
|
|
115
|
+
* restore is done.
|
|
57
116
|
*/
|
|
58
117
|
export declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
59
118
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist.d.cts","names":[],"sources":["../../src/persist.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"persist.d.cts","names":[],"sources":["../../src/persist.ts"],"mappings":";;;;;YAYY,WAAW,KAAK,IAAI,YAAY;;;;iBAK3B;;;;EAIf,gBAAgB,eAAe,oBAAoB;;;;EAInD,qBAAqB,WAAW;;;;EAIhC,oBAAoB;;;;;;iBAOL;;;;EAIf;;;;;EAKA;;;;EAIA,aAAa;;;;;iBAME;;EAEf,aAAa;;;;;EAKb,WAAW;;;;;EAKX;;;;;iBAMe,2CAA2C;;;;;;EAM1D;;EAEA,iBAAiB;;;;;iBAMF,wCAAwC;;EAEvD,mBAAmB;;;;;iBAMJ,kCAEb,oCACA,iCACA;;;;;;;;;;;;wBAyBkB,4BACpB,aACA,WACA,QACA,QACA,kBACC,qCAAkC;;;;;;;;wBAsCf,yBACpB,aACA,WACA,QACA,oBACC,kCAA+B;;;;;;;wBAgBlB,4BACd,OAAO;;;;;;;wBA8BO,mBACd,OAAO,yCACO"}
|
|
@@ -1,59 +1,118 @@
|
|
|
1
1
|
import { DehydrateOptions, DehydratedState, HydrateOptions, QueryClient } from "@tanstack/query-core";
|
|
2
2
|
//#region src/persist.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* A value, or a promise-like that resolves to it.
|
|
5
|
+
*/
|
|
3
6
|
export type Promisable<T> = T | PromiseLike<T>;
|
|
7
|
+
/**
|
|
8
|
+
* Saves, restores, and removes a {@link PersistedClient} in a storage.
|
|
9
|
+
*/
|
|
4
10
|
export interface Persister {
|
|
11
|
+
/**
|
|
12
|
+
* Saves the persisted client to the storage.
|
|
13
|
+
*/
|
|
5
14
|
persistClient: (persistClient: PersistedClient) => Promisable<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Reads the persisted client from the storage, or `undefined` if nothing was saved.
|
|
17
|
+
*/
|
|
6
18
|
restoreClient: () => Promisable<PersistedClient | undefined>;
|
|
19
|
+
/**
|
|
20
|
+
* Removes the persisted client from the storage.
|
|
21
|
+
*/
|
|
7
22
|
removeClient: () => Promisable<void>;
|
|
8
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* The dehydrated state of a `QueryClient` as it is persisted, with the time it was saved and its
|
|
26
|
+
* `buster`.
|
|
27
|
+
*/
|
|
9
28
|
export interface PersistedClient {
|
|
29
|
+
/**
|
|
30
|
+
* When the client was saved, in milliseconds since the epoch. Compared with `maxAge` on restore.
|
|
31
|
+
*/
|
|
10
32
|
timestamp: number;
|
|
33
|
+
/**
|
|
34
|
+
* The `buster` the client was saved with. A persisted client with a different `buster` is
|
|
35
|
+
* discarded.
|
|
36
|
+
*/
|
|
11
37
|
buster: string;
|
|
38
|
+
/**
|
|
39
|
+
* The dehydrated cache of the `QueryClient`.
|
|
40
|
+
*/
|
|
12
41
|
clientState: DehydratedState;
|
|
13
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* The options shared by restoring and saving a persisted `QueryClient`.
|
|
45
|
+
*/
|
|
14
46
|
export interface PersistQueryClientRootOptions {
|
|
15
47
|
/** The QueryClient to persist */
|
|
16
48
|
queryClient: QueryClient;
|
|
17
|
-
/**
|
|
18
|
-
*
|
|
49
|
+
/**
|
|
50
|
+
* The Persister interface for storing and restoring the cache
|
|
51
|
+
* to/from a persisted location
|
|
52
|
+
*/
|
|
19
53
|
persister: Persister;
|
|
20
|
-
/**
|
|
21
|
-
*
|
|
54
|
+
/**
|
|
55
|
+
* A unique string that can be used to forcefully
|
|
56
|
+
* invalidate existing caches if they do not share the same buster string
|
|
57
|
+
*/
|
|
22
58
|
buster?: string;
|
|
23
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Options for restoring a persisted `QueryClient` with {@link persistQueryClientRestore}.
|
|
62
|
+
*/
|
|
24
63
|
export interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
|
|
25
|
-
/**
|
|
64
|
+
/**
|
|
65
|
+
* The max-allowed age of the cache in milliseconds.
|
|
26
66
|
* If a persisted cache is found that is older than this
|
|
27
|
-
* time, it will be discarded
|
|
67
|
+
* time, it will be discarded
|
|
68
|
+
*/
|
|
28
69
|
maxAge?: number;
|
|
29
70
|
/** The options passed to the hydrate function */
|
|
30
71
|
hydrateOptions?: HydrateOptions;
|
|
31
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Options for saving a `QueryClient` with {@link persistQueryClientSave}.
|
|
75
|
+
*/
|
|
32
76
|
export interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
|
|
33
77
|
/** The options passed to the dehydrate function */
|
|
34
78
|
dehydrateOptions?: DehydrateOptions;
|
|
35
79
|
}
|
|
80
|
+
/**
|
|
81
|
+
* Options for {@link persistQueryClient}, which both restores and saves a `QueryClient`.
|
|
82
|
+
*/
|
|
36
83
|
export interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {}
|
|
37
84
|
/**
|
|
38
|
-
* Restores persisted data to the QueryCache
|
|
39
|
-
* - data obtained from persister.restoreClient
|
|
40
|
-
* - data is hydrated using hydrateOptions
|
|
41
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
85
|
+
* Restores persisted data to the QueryCache.
|
|
86
|
+
* - data obtained from persister.restoreClient.
|
|
87
|
+
* - data is hydrated using hydrateOptions.
|
|
88
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient.
|
|
89
|
+
* @param options - The `queryClient` to restore into, the `persister`, and the `maxAge`, `buster`,
|
|
90
|
+
* and `hydrateOptions` that decide whether and how the persisted data is restored.
|
|
91
|
+
* @returns A promise that resolves once the data is restored or removed. If restoring throws, it
|
|
92
|
+
* attempts to remove the persisted data and then rejects with the restore error, or with the removal
|
|
93
|
+
* error if the removal fails too.
|
|
42
94
|
*/
|
|
43
95
|
export declare function persistQueryClientRestore({ queryClient, persister, maxAge, buster, hydrateOptions }: PersistedQueryClientRestoreOptions): Promise<void>;
|
|
44
96
|
/**
|
|
45
|
-
* Persists data from the QueryCache
|
|
46
|
-
* - data dehydrated using dehydrateOptions
|
|
47
|
-
* - data is persisted using persister.persistClient
|
|
97
|
+
* Persists data from the QueryCache.
|
|
98
|
+
* - data dehydrated using dehydrateOptions.
|
|
99
|
+
* - data is persisted using persister.persistClient.
|
|
100
|
+
* @param options - The `queryClient` to persist, the `persister`, the `buster`, and the
|
|
101
|
+
* `dehydrateOptions`.
|
|
48
102
|
*/
|
|
49
103
|
export declare function persistQueryClientSave({ queryClient, persister, buster, dehydrateOptions }: PersistedQueryClientSaveOptions): Promise<void>;
|
|
50
104
|
/**
|
|
51
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
105
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting).
|
|
106
|
+
* @param props - The `queryClient` to monitor, the `persister`, the `buster`, and the
|
|
107
|
+
* `dehydrateOptions` used for each save.
|
|
52
108
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
53
109
|
*/
|
|
54
110
|
export declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
55
111
|
/**
|
|
56
112
|
* Restores persisted data to QueryCache and persists further changes.
|
|
113
|
+
* @param props - The options for both restoring and saving.
|
|
114
|
+
* @returns A tuple of a function that stops persisting, and a promise that resolves once the
|
|
115
|
+
* restore is done.
|
|
57
116
|
*/
|
|
58
117
|
export declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
59
118
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist.d.ts","names":[],"sources":["../../src/persist.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"persist.d.ts","names":[],"sources":["../../src/persist.ts"],"mappings":";;;;;YAYY,WAAW,KAAK,IAAI,YAAY;;;;iBAK3B;;;;EAIf,gBAAgB,eAAe,oBAAoB;;;;EAInD,qBAAqB,WAAW;;;;EAIhC,oBAAoB;;;;;;iBAOL;;;;EAIf;;;;;EAKA;;;;EAIA,aAAa;;;;;iBAME;;EAEf,aAAa;;;;;EAKb,WAAW;;;;;EAKX;;;;;iBAMe,2CAA2C;;;;;;EAM1D;;EAEA,iBAAiB;;;;;iBAMF,wCAAwC;;EAEvD,mBAAmB;;;;;iBAMJ,kCAEb,oCACA,iCACA;;;;;;;;;;;;wBAyBkB,4BACpB,aACA,WACA,QACA,QACA,kBACC,qCAAkC;;;;;;;;wBAsCf,yBACpB,aACA,WACA,QACA,oBACC,kCAA+B;;;;;;;wBAgBlB,4BACd,OAAO;;;;;;;wBA8BO,mBACd,OAAO,yCACO"}
|
package/build/modern/persist.js
CHANGED
|
@@ -1,22 +1,29 @@
|
|
|
1
1
|
import { dehydrate, hydrate } from "@tanstack/query-core";
|
|
2
2
|
//#region src/persist.ts
|
|
3
|
-
/**
|
|
4
|
-
* Checks if emitted event is about cache change and not about observers.
|
|
5
|
-
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
6
|
-
*/
|
|
7
3
|
const cacheEventTypes = [
|
|
8
4
|
"added",
|
|
9
5
|
"removed",
|
|
10
6
|
"updated"
|
|
11
7
|
];
|
|
8
|
+
/**
|
|
9
|
+
* Checks if emitted event is about cache change and not about observers.
|
|
10
|
+
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
11
|
+
* @param eventType - The type of the emitted event.
|
|
12
|
+
* @returns `true` if the event is `'added'`, `'removed'`, or `'updated'`.
|
|
13
|
+
*/
|
|
12
14
|
function isCacheEventType(eventType) {
|
|
13
15
|
return cacheEventTypes.includes(eventType);
|
|
14
16
|
}
|
|
15
17
|
/**
|
|
16
|
-
* Restores persisted data to the QueryCache
|
|
17
|
-
* - data obtained from persister.restoreClient
|
|
18
|
-
* - data is hydrated using hydrateOptions
|
|
19
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
18
|
+
* Restores persisted data to the QueryCache.
|
|
19
|
+
* - data obtained from persister.restoreClient.
|
|
20
|
+
* - data is hydrated using hydrateOptions.
|
|
21
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient.
|
|
22
|
+
* @param options - The `queryClient` to restore into, the `persister`, and the `maxAge`, `buster`,
|
|
23
|
+
* and `hydrateOptions` that decide whether and how the persisted data is restored.
|
|
24
|
+
* @returns A promise that resolves once the data is restored or removed. If restoring throws, it
|
|
25
|
+
* attempts to remove the persisted data and then rejects with the restore error, or with the removal
|
|
26
|
+
* error if the removal fails too.
|
|
20
27
|
*/
|
|
21
28
|
async function persistQueryClientRestore({ queryClient, persister, maxAge = 864e5, buster = "", hydrateOptions }) {
|
|
22
29
|
try {
|
|
@@ -39,9 +46,11 @@ async function persistQueryClientRestore({ queryClient, persister, maxAge = 864e
|
|
|
39
46
|
}
|
|
40
47
|
}
|
|
41
48
|
/**
|
|
42
|
-
* Persists data from the QueryCache
|
|
43
|
-
* - data dehydrated using dehydrateOptions
|
|
44
|
-
* - data is persisted using persister.persistClient
|
|
49
|
+
* Persists data from the QueryCache.
|
|
50
|
+
* - data dehydrated using dehydrateOptions.
|
|
51
|
+
* - data is persisted using persister.persistClient.
|
|
52
|
+
* @param options - The `queryClient` to persist, the `persister`, the `buster`, and the
|
|
53
|
+
* `dehydrateOptions`.
|
|
45
54
|
*/
|
|
46
55
|
async function persistQueryClientSave({ queryClient, persister, buster = "", dehydrateOptions }) {
|
|
47
56
|
const persistClient = {
|
|
@@ -52,7 +61,9 @@ async function persistQueryClientSave({ queryClient, persister, buster = "", deh
|
|
|
52
61
|
await persister.persistClient(persistClient);
|
|
53
62
|
}
|
|
54
63
|
/**
|
|
55
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
64
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting).
|
|
65
|
+
* @param props - The `queryClient` to monitor, the `persister`, the `buster`, and the
|
|
66
|
+
* `dehydrateOptions` used for each save.
|
|
56
67
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
57
68
|
*/
|
|
58
69
|
function persistQueryClientSubscribe(props) {
|
|
@@ -69,6 +80,9 @@ function persistQueryClientSubscribe(props) {
|
|
|
69
80
|
}
|
|
70
81
|
/**
|
|
71
82
|
* Restores persisted data to QueryCache and persists further changes.
|
|
83
|
+
* @param props - The options for both restoring and saving.
|
|
84
|
+
* @returns A tuple of a function that stops persisting, and a promise that resolves once the
|
|
85
|
+
* restore is done.
|
|
72
86
|
*/
|
|
73
87
|
function persistQueryClient(props) {
|
|
74
88
|
let hasUnsubscribed = false;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist.js","names":[],"sources":["../../src/persist.ts"],"sourcesContent":["import { dehydrate, hydrate } from '@tanstack/query-core'\nimport type {\n DehydrateOptions,\n DehydratedState,\n HydrateOptions,\n NotifyEventType,\n QueryClient,\n} from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient: (persistClient: PersistedClient) => Promisable<void>\n restoreClient: () => Promisable<PersistedClient | undefined>\n removeClient: () => Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClientRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n
|
|
1
|
+
{"version":3,"file":"persist.js","names":[],"sources":["../../src/persist.ts"],"sourcesContent":["import { dehydrate, hydrate } from '@tanstack/query-core'\nimport type {\n DehydrateOptions,\n DehydratedState,\n HydrateOptions,\n NotifyEventType,\n QueryClient,\n} from '@tanstack/query-core'\n\n/**\n * A value, or a promise-like that resolves to it.\n */\nexport type Promisable<T> = T | PromiseLike<T>\n\n/**\n * Saves, restores, and removes a {@link PersistedClient} in a storage.\n */\nexport interface Persister {\n /**\n * Saves the persisted client to the storage.\n */\n persistClient: (persistClient: PersistedClient) => Promisable<void>\n /**\n * Reads the persisted client from the storage, or `undefined` if nothing was saved.\n */\n restoreClient: () => Promisable<PersistedClient | undefined>\n /**\n * Removes the persisted client from the storage.\n */\n removeClient: () => Promisable<void>\n}\n\n/**\n * The dehydrated state of a `QueryClient` as it is persisted, with the time it was saved and its\n * `buster`.\n */\nexport interface PersistedClient {\n /**\n * When the client was saved, in milliseconds since the epoch. Compared with `maxAge` on restore.\n */\n timestamp: number\n /**\n * The `buster` the client was saved with. A persisted client with a different `buster` is\n * discarded.\n */\n buster: string\n /**\n * The dehydrated cache of the `QueryClient`.\n */\n clientState: DehydratedState\n}\n\n/**\n * The options shared by restoring and saving a persisted `QueryClient`.\n */\nexport interface PersistQueryClientRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /**\n * The Persister interface for storing and restoring the cache\n * to/from a persisted location\n */\n persister: Persister\n /**\n * A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string\n */\n buster?: string\n}\n\n/**\n * Options for restoring a persisted `QueryClient` with {@link persistQueryClientRestore}.\n */\nexport interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {\n /**\n * The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded\n */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\n/**\n * Options for saving a `QueryClient` with {@link persistQueryClientSave}.\n */\nexport interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\n/**\n * Options for {@link persistQueryClient}, which both restores and saves a `QueryClient`.\n */\nexport interface PersistQueryClientOptions\n extends\n PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClientRootOptions {}\n\nconst cacheEventTypes: Array<NotifyEventType> = ['added', 'removed', 'updated']\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n * @param eventType - The type of the emitted event.\n * @returns `true` if the event is `'added'`, `'removed'`, or `'updated'`.\n */\nfunction isCacheEventType(eventType: NotifyEventType) {\n return cacheEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache.\n * - data obtained from persister.restoreClient.\n * - data is hydrated using hydrateOptions.\n * If data is expired, busted, empty, or throws, it runs persister.removeClient.\n * @param options - The `queryClient` to restore into, the `persister`, and the `maxAge`, `buster`,\n * and `hydrateOptions` that decide whether and how the persisted data is restored.\n * @returns A promise that resolves once the data is restored or removed. If restoring throws, it\n * attempts to remove the persisted data and then rejects with the restore error, or with the removal\n * error if the removal fails too.\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n return persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n return persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n\n await persister.removeClient()\n\n throw err\n }\n}\n\n/**\n * Persists data from the QueryCache.\n * - data dehydrated using dehydrateOptions.\n * - data is persisted using persister.persistClient.\n * @param options - The `queryClient` to persist, the `persister`, the `buster`, and the\n * `dehydrateOptions`.\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting).\n * @param props - The `queryClient` to monitor, the `persister`, the `buster`, and the\n * `dehydrateOptions` used for each save.\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unsubscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unsubscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n * @param props - The options for both restoring and saving.\n * @returns A tuple of a function that stops persisting, and a promise that resolves once the\n * restore is done.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"mappings":";;AAqGA,MAAM,kBAA0C;CAAC;CAAS;CAAW;AAAS;;;;;;;AAQ9E,SAAS,iBAAiB,WAA4B;CACpD,OAAO,gBAAgB,SAAS,SAAS;AAC3C;;;;;;;;;;;;AAaA,eAAsB,0BAA0B,EAC9C,aACA,WACA,SAAS,OACT,SAAS,IACT,kBACqC;CACrC,IAAI;EACF,MAAM,kBAAkB,MAAM,UAAU,cAAc;EAEtD,IAAI,iBAAiB;GACnB,IAAI,gBAAgB,WAAW;IAC7B,MAAM,UAAU,KAAK,IAAI,IAAI,gBAAgB,YAAY;IACzD,MAAM,SAAS,gBAAgB,WAAW;IAC1C,IAAI,WAAW,QACb,OAAO,UAAU,aAAa;SAE9B,QAAQ,aAAa,gBAAgB,aAAa,cAAc;GAEpE,OACE,OAAO,UAAU,aAAa;EAElC;CACF,SAAS,KAAK;EACZ,IAAI,QAAQ,IAAI,aAAa,cAAc;GACzC,QAAQ,MAAM,GAAG;GACjB,QAAQ,KACN,0IACF;EACF;EAEA,MAAM,UAAU,aAAa;EAE7B,MAAM;CACR;AACF;;;;;;;;AASA,eAAsB,uBAAuB,EAC3C,aACA,WACA,SAAS,IACT,oBACkC;CAClC,MAAM,gBAAiC;EACrC;EACA,WAAW,KAAK,IAAI;EACpB,aAAa,UAAU,aAAa,gBAAgB;CACtD;CAEA,MAAM,UAAU,cAAc,aAAa;AAC7C;;;;;;;AAQA,SAAgB,4BACd,OACA;CACA,MAAM,wBAAwB,MAAM,YACjC,cAAc,CAAC,CACf,WAAW,UAAU;EACpB,IAAI,iBAAiB,MAAM,IAAI,GAC7B,uBAAuB,KAAK;CAEhC,CAAC;CAEH,MAAM,2BAA2B,MAAM,YACpC,iBAAiB,CAAC,CAClB,WAAW,UAAU;EACpB,IAAI,iBAAiB,MAAM,IAAI,GAC7B,uBAAuB,KAAK;CAEhC,CAAC;CAEH,aAAa;EACX,sBAAsB;EACtB,yBAAyB;CAC3B;AACF;;;;;;;AAQA,SAAgB,mBACd,OAC6B;CAC7B,IAAI,kBAAkB;CACtB,IAAI;CACJ,MAAM,oBAAoB;EACxB,kBAAkB;EAClB,gCAAgC;CAClC;CAUA,OAAO,CAAC,aAPe,0BAA0B,KAAK,CAAC,CAAC,WAAW;EACjE,IAAI,CAAC,iBAEH,gCAAgC,4BAA4B,KAAK;CAErE,CAEkC,CAAC;AACrC"}
|
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
2
|
//#region src/retryStrategies.ts
|
|
3
|
+
/**
|
|
4
|
+
* A {@link PersistRetryer} that drops the query with the oldest `dataUpdatedAt` and tries again, until
|
|
5
|
+
* no queries are left.
|
|
6
|
+
* @param props - The `persistedClient` that failed to save.
|
|
7
|
+
* @returns A copy of the client without its oldest query, or `undefined` if it has no queries.
|
|
8
|
+
*/
|
|
3
9
|
const removeOldestQuery = ({ persistedClient }) => {
|
|
4
10
|
const mutations = [...persistedClient.clientState.mutations];
|
|
5
11
|
const queries = [...persistedClient.clientState.queries];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.cjs","names":[],"sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from './persist'\n\nexport type PersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => PersistedClient | undefined\n\nexport const removeOldestQuery: PersistRetryer = ({ persistedClient }) => {\n const mutations = [...persistedClient.clientState.mutations]\n const queries = [...persistedClient.clientState.queries]\n const client: PersistedClient = {\n ...persistedClient,\n clientState: { mutations, queries },\n }\n\n // sort queries by dataUpdatedAt (oldest first)\n const sortedQueries = [...queries].sort(\n (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt,\n )\n\n // clean oldest query\n if (sortedQueries.length > 0) {\n const oldestData = sortedQueries.shift()\n client.clientState.queries = queries.filter((q) => q !== oldestData)\n return client\n }\n\n return undefined\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"retryStrategies.cjs","names":[],"sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from './persist'\n\n/**\n * Called when saving the persisted client fails. Returns a smaller client to try saving again, or\n * `undefined` to give up.\n */\nexport type PersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => PersistedClient | undefined\n\n/**\n * A {@link PersistRetryer} that drops the query with the oldest `dataUpdatedAt` and tries again, until\n * no queries are left.\n * @param props - The `persistedClient` that failed to save.\n * @returns A copy of the client without its oldest query, or `undefined` if it has no queries.\n */\nexport const removeOldestQuery: PersistRetryer = ({ persistedClient }) => {\n const mutations = [...persistedClient.clientState.mutations]\n const queries = [...persistedClient.clientState.queries]\n const client: PersistedClient = {\n ...persistedClient,\n clientState: { mutations, queries },\n }\n\n // sort queries by dataUpdatedAt (oldest first)\n const sortedQueries = [...queries].sort(\n (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt,\n )\n\n // clean oldest query\n if (sortedQueries.length > 0) {\n const oldestData = sortedQueries.shift()\n client.clientState.queries = queries.filter((q) => q !== oldestData)\n return client\n }\n\n return undefined\n}\n"],"mappings":";;;;;;;;AAkBA,MAAa,qBAAqC,EAAE,sBAAsB;CACxE,MAAM,YAAY,CAAC,GAAG,gBAAgB,YAAY,SAAS;CAC3D,MAAM,UAAU,CAAC,GAAG,gBAAgB,YAAY,OAAO;CACvD,MAAM,SAA0B;EAC9B,GAAG;EACH,aAAa;GAAE;GAAW;EAAQ;CACpC;CAGA,MAAM,gBAAgB,CAAC,GAAG,OAAO,CAAC,CAAC,MAChC,GAAG,MAAM,EAAE,MAAM,gBAAgB,EAAE,MAAM,aAC5C;CAGA,IAAI,cAAc,SAAS,GAAG;EAC5B,MAAM,aAAa,cAAc,MAAM;EACvC,OAAO,YAAY,UAAU,QAAQ,QAAQ,MAAM,MAAM,UAAU;EACnE,OAAO;CACT;AAGF"}
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { PersistedClient } from "./persist.cjs";
|
|
2
2
|
//#region src/retryStrategies.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Called when saving the persisted client fails. Returns a smaller client to try saving again, or
|
|
5
|
+
* `undefined` to give up.
|
|
6
|
+
*/
|
|
3
7
|
export type PersistRetryer = (props: {
|
|
4
8
|
persistedClient: PersistedClient;
|
|
5
9
|
error: Error;
|
|
6
10
|
errorCount: number;
|
|
7
11
|
}) => PersistedClient | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* A {@link PersistRetryer} that drops the query with the oldest `dataUpdatedAt` and tries again, until
|
|
14
|
+
* no queries are left.
|
|
15
|
+
* @param props - The `persistedClient` that failed to save.
|
|
16
|
+
* @returns A copy of the client without its oldest query, or `undefined` if it has no queries.
|
|
17
|
+
*/
|
|
8
18
|
export declare const removeOldestQuery: PersistRetryer;
|
|
9
19
|
//#endregion
|
|
10
20
|
//# sourceMappingURL=retryStrategies.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.d.cts","names":[],"sources":["../../src/retryStrategies.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"retryStrategies.d.cts","names":[],"sources":["../../src/retryStrategies.ts"],"mappings":";;;;;;YAMY,kBAAkB;EAC5B,iBAAiB;EACjB,OAAO;EACP;MACI;;;;;;;qBAQO,mBAAmB"}
|
|
@@ -1,10 +1,20 @@
|
|
|
1
1
|
import { PersistedClient } from "./persist.js";
|
|
2
2
|
//#region src/retryStrategies.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* Called when saving the persisted client fails. Returns a smaller client to try saving again, or
|
|
5
|
+
* `undefined` to give up.
|
|
6
|
+
*/
|
|
3
7
|
export type PersistRetryer = (props: {
|
|
4
8
|
persistedClient: PersistedClient;
|
|
5
9
|
error: Error;
|
|
6
10
|
errorCount: number;
|
|
7
11
|
}) => PersistedClient | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* A {@link PersistRetryer} that drops the query with the oldest `dataUpdatedAt` and tries again, until
|
|
14
|
+
* no queries are left.
|
|
15
|
+
* @param props - The `persistedClient` that failed to save.
|
|
16
|
+
* @returns A copy of the client without its oldest query, or `undefined` if it has no queries.
|
|
17
|
+
*/
|
|
8
18
|
export declare const removeOldestQuery: PersistRetryer;
|
|
9
19
|
//#endregion
|
|
10
20
|
//# sourceMappingURL=retryStrategies.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.d.ts","names":[],"sources":["../../src/retryStrategies.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"retryStrategies.d.ts","names":[],"sources":["../../src/retryStrategies.ts"],"mappings":";;;;;;YAMY,kBAAkB;EAC5B,iBAAiB;EACjB,OAAO;EACP;MACI;;;;;;;qBAQO,mBAAmB"}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
//#region src/retryStrategies.ts
|
|
2
|
+
/**
|
|
3
|
+
* A {@link PersistRetryer} that drops the query with the oldest `dataUpdatedAt` and tries again, until
|
|
4
|
+
* no queries are left.
|
|
5
|
+
* @param props - The `persistedClient` that failed to save.
|
|
6
|
+
* @returns A copy of the client without its oldest query, or `undefined` if it has no queries.
|
|
7
|
+
*/
|
|
2
8
|
const removeOldestQuery = ({ persistedClient }) => {
|
|
3
9
|
const mutations = [...persistedClient.clientState.mutations];
|
|
4
10
|
const queries = [...persistedClient.clientState.queries];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.js","names":[],"sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from './persist'\n\nexport type PersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => PersistedClient | undefined\n\nexport const removeOldestQuery: PersistRetryer = ({ persistedClient }) => {\n const mutations = [...persistedClient.clientState.mutations]\n const queries = [...persistedClient.clientState.queries]\n const client: PersistedClient = {\n ...persistedClient,\n clientState: { mutations, queries },\n }\n\n // sort queries by dataUpdatedAt (oldest first)\n const sortedQueries = [...queries].sort(\n (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt,\n )\n\n // clean oldest query\n if (sortedQueries.length > 0) {\n const oldestData = sortedQueries.shift()\n client.clientState.queries = queries.filter((q) => q !== oldestData)\n return client\n }\n\n return undefined\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"retryStrategies.js","names":[],"sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from './persist'\n\n/**\n * Called when saving the persisted client fails. Returns a smaller client to try saving again, or\n * `undefined` to give up.\n */\nexport type PersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => PersistedClient | undefined\n\n/**\n * A {@link PersistRetryer} that drops the query with the oldest `dataUpdatedAt` and tries again, until\n * no queries are left.\n * @param props - The `persistedClient` that failed to save.\n * @returns A copy of the client without its oldest query, or `undefined` if it has no queries.\n */\nexport const removeOldestQuery: PersistRetryer = ({ persistedClient }) => {\n const mutations = [...persistedClient.clientState.mutations]\n const queries = [...persistedClient.clientState.queries]\n const client: PersistedClient = {\n ...persistedClient,\n clientState: { mutations, queries },\n }\n\n // sort queries by dataUpdatedAt (oldest first)\n const sortedQueries = [...queries].sort(\n (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt,\n )\n\n // clean oldest query\n if (sortedQueries.length > 0) {\n const oldestData = sortedQueries.shift()\n client.clientState.queries = queries.filter((q) => q !== oldestData)\n return client\n }\n\n return undefined\n}\n"],"mappings":";;;;;;;AAkBA,MAAa,qBAAqC,EAAE,sBAAsB;CACxE,MAAM,YAAY,CAAC,GAAG,gBAAgB,YAAY,SAAS;CAC3D,MAAM,UAAU,CAAC,GAAG,gBAAgB,YAAY,OAAO;CACvD,MAAM,SAA0B;EAC9B,GAAG;EACH,aAAa;GAAE;GAAW;EAAQ;CACpC;CAGA,MAAM,gBAAgB,CAAC,GAAG,OAAO,CAAC,CAAC,MAChC,GAAG,MAAM,EAAE,MAAM,gBAAgB,EAAE,MAAM,aAC5C;CAGA,IAAI,cAAc,SAAS,GAAG;EAC5B,MAAM,aAAa,cAAc,MAAM;EACvC,OAAO,YAAY,UAAU,QAAQ,QAAQ,MAAM,MAAM,UAAU;EACnE,OAAO;CACT;AAGF"}
|