@tanstack/query-persist-client-core 5.0.0-alpha.6 → 5.0.0-alpha.63
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/lib/__tests__/persist.test.d.ts +1 -0
- package/build/lib/__tests__/persist.test.d.ts.map +1 -0
- package/build/lib/__tests__/utils.d.ts +2 -1
- package/build/lib/__tests__/utils.d.ts.map +1 -0
- package/build/lib/index.cjs +13 -0
- package/build/lib/index.cjs.map +1 -0
- package/build/lib/index.d.ts +1 -0
- package/build/lib/index.d.ts.map +1 -0
- package/build/lib/index.js +2 -12
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.legacy.cjs +13 -0
- package/build/lib/index.legacy.cjs.map +1 -0
- package/build/lib/index.legacy.js +3 -0
- package/build/lib/index.legacy.js.map +1 -0
- package/build/lib/{persist.mjs → persist.cjs} +10 -5
- package/build/lib/persist.cjs.map +1 -0
- package/build/lib/persist.d.ts +2 -1
- package/build/lib/persist.d.ts.map +1 -0
- package/build/lib/persist.js +5 -10
- package/build/lib/persist.js.map +1 -1
- package/build/lib/persist.legacy.cjs +116 -0
- package/build/lib/persist.legacy.cjs.map +1 -0
- package/build/lib/{persist.esm.js → persist.legacy.js} +1 -1
- package/build/lib/persist.legacy.js.map +1 -0
- package/build/lib/{retryStrategies.mjs → retryStrategies.cjs} +4 -2
- package/build/lib/retryStrategies.cjs.map +1 -0
- package/build/lib/retryStrategies.d.ts +3 -2
- package/build/lib/retryStrategies.d.ts.map +1 -0
- package/build/lib/retryStrategies.js +1 -3
- package/build/lib/retryStrategies.js.map +1 -1
- package/build/lib/retryStrategies.legacy.cjs +29 -0
- package/build/lib/retryStrategies.legacy.cjs.map +1 -0
- package/build/lib/{retryStrategies.esm.js → retryStrategies.legacy.js} +1 -1
- package/build/lib/retryStrategies.legacy.js.map +1 -0
- package/package.json +14 -10
- package/src/__tests__/utils.ts +1 -4
- package/src/persist.ts +1 -1
- package/src/retryStrategies.ts +1 -1
- package/build/lib/index.esm.js +0 -3
- package/build/lib/index.esm.js.map +0 -1
- package/build/lib/index.mjs +0 -3
- package/build/lib/index.mjs.map +0 -1
- package/build/lib/persist.esm.js.map +0 -1
- package/build/lib/persist.mjs.map +0 -1
- package/build/lib/retryStrategies.esm.js.map +0 -1
- package/build/lib/retryStrategies.mjs.map +0 -1
- package/build/umd/index.development.js +0 -146
- package/build/umd/index.development.js.map +0 -1
- package/build/umd/index.production.js +0 -2
- package/build/umd/index.production.js.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist.test.d.ts","sourceRoot":"","sources":["../../../src/__tests__/persist.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { QueryClientConfig } from '@tanstack/query-core';
|
|
2
2
|
import { QueryClient } from '@tanstack/query-core';
|
|
3
|
-
import type { Persister } from '
|
|
3
|
+
import type { Persister } from '../persist';
|
|
4
4
|
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
5
5
|
export declare function sleep(timeout: number): Promise<void>;
|
|
6
6
|
export declare const createMockPersister: () => Persister;
|
|
7
7
|
export declare const createSpyablePersister: () => Persister;
|
|
8
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/__tests__/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AAC7D,OAAO,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAClD,OAAO,KAAK,EAAE,SAAS,EAAmB,MAAM,YAAY,CAAA;AAG5D,wBAAgB,iBAAiB,CAAC,MAAM,CAAC,EAAE,iBAAiB,GAAG,WAAW,CAEzE;AAED,wBAAgB,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIpD;AAED,eAAO,MAAM,mBAAmB,QAAO,SAetC,CAAA;AAED,eAAO,MAAM,sBAAsB,QAAO,SAMzC,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var persist = require('./persist.cjs');
|
|
4
|
+
var retryStrategies = require('./retryStrategies.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.persistQueryClient = persist.persistQueryClient;
|
|
9
|
+
exports.persistQueryClientRestore = persist.persistQueryClientRestore;
|
|
10
|
+
exports.persistQueryClientSave = persist.persistQueryClientSave;
|
|
11
|
+
exports.persistQueryClientSubscribe = persist.persistQueryClientSubscribe;
|
|
12
|
+
exports.removeOldestQuery = retryStrategies.removeOldestQuery;
|
|
13
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
package/build/lib/index.d.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAEA,cAAc,WAAW,CAAA;AACzB,cAAc,mBAAmB,CAAA"}
|
package/build/lib/index.js
CHANGED
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var persist = require('./persist.js');
|
|
4
|
-
var retryStrategies = require('./retryStrategies.js');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
exports.persistQueryClient = persist.persistQueryClient;
|
|
9
|
-
exports.persistQueryClientRestore = persist.persistQueryClientRestore;
|
|
10
|
-
exports.persistQueryClientSave = persist.persistQueryClientSave;
|
|
11
|
-
exports.persistQueryClientSubscribe = persist.persistQueryClientSubscribe;
|
|
12
|
-
exports.removeOldestQuery = retryStrategies.removeOldestQuery;
|
|
1
|
+
export { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
|
|
2
|
+
export { removeOldestQuery } from './retryStrategies.js';
|
|
13
3
|
//# sourceMappingURL=index.js.map
|
package/build/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var persist = require('./persist.legacy.cjs');
|
|
4
|
+
var retryStrategies = require('./retryStrategies.legacy.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.persistQueryClient = persist.persistQueryClient;
|
|
9
|
+
exports.persistQueryClientRestore = persist.persistQueryClientRestore;
|
|
10
|
+
exports.persistQueryClientSave = persist.persistQueryClientSave;
|
|
11
|
+
exports.persistQueryClientSubscribe = persist.persistQueryClientSubscribe;
|
|
12
|
+
exports.removeOldestQuery = retryStrategies.removeOldestQuery;
|
|
13
|
+
//# sourceMappingURL=index.legacy.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.legacy.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.legacy.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var queryCore = require('@tanstack/query-core');
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Checks if emitted event is about cache change and not about observers.
|
|
@@ -31,7 +33,7 @@ async function persistQueryClientRestore({
|
|
|
31
33
|
if (expired || busted) {
|
|
32
34
|
persister.removeClient();
|
|
33
35
|
} else {
|
|
34
|
-
hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
36
|
+
queryCore.hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
35
37
|
}
|
|
36
38
|
} else {
|
|
37
39
|
persister.removeClient();
|
|
@@ -60,7 +62,7 @@ async function persistQueryClientSave({
|
|
|
60
62
|
const persistClient = {
|
|
61
63
|
buster,
|
|
62
64
|
timestamp: Date.now(),
|
|
63
|
-
clientState: dehydrate(queryClient, dehydrateOptions)
|
|
65
|
+
clientState: queryCore.dehydrate(queryClient, dehydrateOptions)
|
|
64
66
|
};
|
|
65
67
|
await persister.persistClient(persistClient);
|
|
66
68
|
}
|
|
@@ -107,5 +109,8 @@ function persistQueryClient(props) {
|
|
|
107
109
|
return [unsubscribe, restorePromise];
|
|
108
110
|
}
|
|
109
111
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
+
exports.persistQueryClient = persistQueryClient;
|
|
113
|
+
exports.persistQueryClientRestore = persistQueryClientRestore;
|
|
114
|
+
exports.persistQueryClientSave = persistQueryClientSave;
|
|
115
|
+
exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
|
|
116
|
+
//# sourceMappingURL=persist.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist.cjs","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;;;AAuDA;AACA;AACA;AACA;A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;AAC3B,EAAA,IAAIC,6BAAuD,CAAA;EAC3D,MAAMC,WAAW,GAAGA,MAAM;AACxBF,IAAAA,eAAe,GAAG,IAAI,CAAA;AACtBC,IAAAA,6BAA6B,IAAI,CAAA;GAClC,CAAA;;AAED;EACA,MAAME,cAAc,GAAGxC,yBAAyB,CAAC4B,KAAK,CAAC,CAACa,IAAI,CAAC,MAAM;IACjE,IAAI,CAACJ,eAAe,EAAE;AACpB;AACAC,MAAAA,6BAA6B,GAAGX,2BAA2B,CAACC,KAAK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;;;;"}
|
package/build/lib/persist.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { QueryClient, DehydratedState, DehydrateOptions, HydrateOptions } from '@tanstack/query-core';
|
|
2
|
-
export
|
|
2
|
+
export type Promisable<T> = T | PromiseLike<T>;
|
|
3
3
|
export interface Persister {
|
|
4
4
|
persistClient(persistClient: PersistedClient): Promisable<void>;
|
|
5
5
|
restoreClient(): Promisable<PersistedClient | undefined>;
|
|
@@ -56,3 +56,4 @@ export declare function persistQueryClientSubscribe(props: PersistedQueryClientS
|
|
|
56
56
|
* Restores persisted data to QueryCache and persists further changes.
|
|
57
57
|
*/
|
|
58
58
|
export declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
59
|
+
//# sourceMappingURL=persist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist.d.ts","sourceRoot":"","sources":["../../src/persist.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,cAAc,EAEf,MAAM,sBAAsB,CAAA;AAG7B,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAA;AAE9C,MAAM,WAA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AAC,IAAI,CAAC,CAAC,CAiB7B"}
|
package/build/lib/persist.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var queryCore = require('@tanstack/query-core');
|
|
1
|
+
import { hydrate, dehydrate } from '@tanstack/query-core';
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* Checks if emitted event is about cache change and not about observers.
|
|
@@ -33,7 +31,7 @@ async function persistQueryClientRestore({
|
|
|
33
31
|
if (expired || busted) {
|
|
34
32
|
persister.removeClient();
|
|
35
33
|
} else {
|
|
36
|
-
|
|
34
|
+
hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
37
35
|
}
|
|
38
36
|
} else {
|
|
39
37
|
persister.removeClient();
|
|
@@ -62,7 +60,7 @@ async function persistQueryClientSave({
|
|
|
62
60
|
const persistClient = {
|
|
63
61
|
buster,
|
|
64
62
|
timestamp: Date.now(),
|
|
65
|
-
clientState:
|
|
63
|
+
clientState: dehydrate(queryClient, dehydrateOptions)
|
|
66
64
|
};
|
|
67
65
|
await persister.persistClient(persistClient);
|
|
68
66
|
}
|
|
@@ -96,7 +94,7 @@ function persistQueryClient(props) {
|
|
|
96
94
|
let persistQueryClientUnsubscribe;
|
|
97
95
|
const unsubscribe = () => {
|
|
98
96
|
hasUnsubscribed = true;
|
|
99
|
-
persistQueryClientUnsubscribe
|
|
97
|
+
persistQueryClientUnsubscribe?.();
|
|
100
98
|
};
|
|
101
99
|
|
|
102
100
|
// Attempt restore
|
|
@@ -109,8 +107,5 @@ function persistQueryClient(props) {
|
|
|
109
107
|
return [unsubscribe, restorePromise];
|
|
110
108
|
}
|
|
111
109
|
|
|
112
|
-
|
|
113
|
-
exports.persistQueryClientRestore = persistQueryClientRestore;
|
|
114
|
-
exports.persistQueryClientSave = persistQueryClientSave;
|
|
115
|
-
exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
|
|
110
|
+
export { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
116
111
|
//# sourceMappingURL=persist.js.map
|
package/build/lib/persist.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"persist.js","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\
|
|
1
|
+
{"version":3,"file":"persist.js","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;AAuDA;AACA;AACA;AACA;AACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;AAED,SAASC,oBAAoBA,CAACC,SAA0B,EAAE;AACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;AAChD,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeE,yBAAyBA,CAAC;EAC9CC,WAAW;EACXC,SAAS;AACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;AAC5BC,EAAAA,MAAM,GAAG,EAAE;AACXC,EAAAA,cAAAA;AACkC,CAAC,EAAE;EACrC,IAAI;AACF,IAAA,MAAMC,eAAe,GAAG,MAAM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 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var queryCore = require('@tanstack/query-core');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Checks if emitted event is about cache change and not about observers.
|
|
7
|
+
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
8
|
+
*/
|
|
9
|
+
const cacheableEventTypes = ['added', 'removed', 'updated'];
|
|
10
|
+
function isCacheableEventType(eventType) {
|
|
11
|
+
return cacheableEventTypes.includes(eventType);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Restores persisted data to the QueryCache
|
|
16
|
+
* - data obtained from persister.restoreClient
|
|
17
|
+
* - data is hydrated using hydrateOptions
|
|
18
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
19
|
+
*/
|
|
20
|
+
async function persistQueryClientRestore({
|
|
21
|
+
queryClient,
|
|
22
|
+
persister,
|
|
23
|
+
maxAge = 1000 * 60 * 60 * 24,
|
|
24
|
+
buster = '',
|
|
25
|
+
hydrateOptions
|
|
26
|
+
}) {
|
|
27
|
+
try {
|
|
28
|
+
const persistedClient = await persister.restoreClient();
|
|
29
|
+
if (persistedClient) {
|
|
30
|
+
if (persistedClient.timestamp) {
|
|
31
|
+
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
32
|
+
const busted = persistedClient.buster !== buster;
|
|
33
|
+
if (expired || busted) {
|
|
34
|
+
persister.removeClient();
|
|
35
|
+
} else {
|
|
36
|
+
queryCore.hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
37
|
+
}
|
|
38
|
+
} else {
|
|
39
|
+
persister.removeClient();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} catch (err) {
|
|
43
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
44
|
+
console.error(err);
|
|
45
|
+
console.warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
46
|
+
}
|
|
47
|
+
persister.removeClient();
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Persists data from the QueryCache
|
|
53
|
+
* - data dehydrated using dehydrateOptions
|
|
54
|
+
* - data is persisted using persister.persistClient
|
|
55
|
+
*/
|
|
56
|
+
async function persistQueryClientSave({
|
|
57
|
+
queryClient,
|
|
58
|
+
persister,
|
|
59
|
+
buster = '',
|
|
60
|
+
dehydrateOptions
|
|
61
|
+
}) {
|
|
62
|
+
const persistClient = {
|
|
63
|
+
buster,
|
|
64
|
+
timestamp: Date.now(),
|
|
65
|
+
clientState: queryCore.dehydrate(queryClient, dehydrateOptions)
|
|
66
|
+
};
|
|
67
|
+
await persister.persistClient(persistClient);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
72
|
+
* @returns an unsubscribe function (to discontinue monitoring)
|
|
73
|
+
*/
|
|
74
|
+
function persistQueryClientSubscribe(props) {
|
|
75
|
+
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
|
|
76
|
+
if (isCacheableEventType(event.type)) {
|
|
77
|
+
persistQueryClientSave(props);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(event => {
|
|
81
|
+
if (isCacheableEventType(event.type)) {
|
|
82
|
+
persistQueryClientSave(props);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
return () => {
|
|
86
|
+
unsubscribeQueryCache();
|
|
87
|
+
unusbscribeMutationCache();
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Restores persisted data to QueryCache and persists further changes.
|
|
93
|
+
*/
|
|
94
|
+
function persistQueryClient(props) {
|
|
95
|
+
let hasUnsubscribed = false;
|
|
96
|
+
let persistQueryClientUnsubscribe;
|
|
97
|
+
const unsubscribe = () => {
|
|
98
|
+
hasUnsubscribed = true;
|
|
99
|
+
persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
// Attempt restore
|
|
103
|
+
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
104
|
+
if (!hasUnsubscribed) {
|
|
105
|
+
// Subscribe to changes in the query cache to trigger the save
|
|
106
|
+
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return [unsubscribe, restorePromise];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
exports.persistQueryClient = persistQueryClient;
|
|
113
|
+
exports.persistQueryClientRestore = persistQueryClientRestore;
|
|
114
|
+
exports.persistQueryClientSave = persistQueryClientSave;
|
|
115
|
+
exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
|
|
116
|
+
//# sourceMappingURL=persist.legacy.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist.legacy.cjs","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;;;AAuDA;AACA;AACA;AACA;AACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;AAED,SAASC,oBAAoBA,CAACC,SAA0B,EAAE;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ACtBC,6BAA6B,IAAA,IAAA,GAAA,KAAA,CAAA,GAA7BA,6BAA6B,EAAI,CAAA;GAClC,CAAA;;AAED;EACA,MAAME,cAAc,GAAGxC,yBAAyB,CAAC4B,KAAK,CAAC,CAACa,IAAI,CAAC,MAAM;IACjE,IAAI,CAACJ,eAAe,EAAE;AACpB;AACAC,MAAAA,6BAA6B,GAAGX,2BAA2B,CAACC,KAAK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist.legacy.js","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;AAuDA;AACA;AACA;AACA;AACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;AAED,SAASC,oBAAoBA,CAACC,SAA0B,EAAE;AACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;AAChD,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeE,yBAAyBA,CAAC;EAC9CC,WAAW;EACXC,SAAS;AACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,E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pB;AACAC,MAAAA,6BAA6B,GAAGX,2BAA2B,CAACC,KAAK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;"}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
1
3
|
const removeOldestQuery = ({
|
|
2
4
|
persistedClient
|
|
3
5
|
}) => {
|
|
@@ -23,5 +25,5 @@ const removeOldestQuery = ({
|
|
|
23
25
|
return undefined;
|
|
24
26
|
};
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
//# sourceMappingURL=retryStrategies.
|
|
28
|
+
exports.removeOldestQuery = removeOldestQuery;
|
|
29
|
+
//# sourceMappingURL=retryStrategies.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retryStrategies.cjs","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"],"names":["removeOldestQuery","persistedClient","mutations","clientState","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":";;AAQO,MAAMA,iBAAiC,GAAGA,CAAC;AAAEC,EAAAA,eAAAA;AAAgB,CAAC,KAAK;EACxE,MAAMC,SAAS,GAAG,CAAC,GAAGD,eAAe,CAACE,WAAW,CAACD,SAAS,CAAC,CAAA;EAC5D,MAAME,OAAO,GAAG,CAAC,GAAGH,eAAe,CAACE,WAAW,CAACC,OAAO,CAAC,CAAA;AACxD,EAAA,MAAMC,MAAuB,GAAG;AAC9B,IAAA,GAAGJ,eAAe;AAClBE,IAAAA,WAAW,EAAE;MAAED,SAAS;AAAEE,MAAAA,OAAAA;AAAQ,KAAA;GACnC,CAAA;;AAED;EACA,MAAME,aAAa,GAAG,CAAC,GAAGF,OAAO,CAAC,CAACG,IAAI,CACrC,CAACC,CAAC,EAAEC,CAAC,KAAKD,CAAC,CAACE,KAAK,CAACC,aAAa,GAAGF,CAAC,CAACC,KAAK,CAACC,aAC5C,CAAC,CAAA;;AAED;AACA,EAAA,IAAIL,aAAa,CAACM,MAAM,GAAG,CAAC,EAAE;AAC5B,IAAA,MAAMC,UAAU,GAAGP,aAAa,CAACQ,KAAK,EAAE,CAAA;AACxCT,IAAAA,MAAM,CAACF,WAAW,CAACC,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;AACpE,IAAA,OAAOR,MAAM,CAAA;AACf,GAAA;AAEA,EAAA,OAAOY,SAAS,CAAA;AAClB;;;;"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type { PersistedClient } from '
|
|
2
|
-
export
|
|
1
|
+
import type { PersistedClient } from './persist';
|
|
2
|
+
export type PersistRetryer = (props: {
|
|
3
3
|
persistedClient: PersistedClient;
|
|
4
4
|
error: Error;
|
|
5
5
|
errorCount: number;
|
|
6
6
|
}) => PersistedClient | undefined;
|
|
7
7
|
export declare const removeOldestQuery: PersistRetryer;
|
|
8
|
+
//# sourceMappingURL=retryStrategies.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retryStrategies.d.ts","sourceRoot":"","sources":["../../src/retryStrategies.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,WAAW,CAAA;AAEhD,MAAM,MAAM,cAAc,GAAG,CAAC,KAAK,EAAE;IACnC,eAAe,EAAE,eAAe,CAAA;IAChC,KAAK,EAAE,KAAK,CAAA;IACZ,UAAU,EAAE,MAAM,CAAA;CACnB,KAAK,eAAe,GAAG,SAAS,CAAA;AAEjC,eAAO,MAAM,iBAAiB,EAAE,cAqB/B,CAAA"}
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
1
|
const removeOldestQuery = ({
|
|
4
2
|
persistedClient
|
|
5
3
|
}) => {
|
|
@@ -25,5 +23,5 @@ const removeOldestQuery = ({
|
|
|
25
23
|
return undefined;
|
|
26
24
|
};
|
|
27
25
|
|
|
28
|
-
|
|
26
|
+
export { removeOldestQuery };
|
|
29
27
|
//# sourceMappingURL=retryStrategies.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.js","sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from '
|
|
1
|
+
{"version":3,"file":"retryStrategies.js","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"],"names":["removeOldestQuery","persistedClient","mutations","clientState","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":"AAQO,MAAMA,iBAAiC,GAAGA,CAAC;AAAEC,EAAAA,eAAAA;AAAgB,CAAC,KAAK;EACxE,MAAMC,SAAS,GAAG,CAAC,GAAGD,eAAe,CAACE,WAAW,CAACD,SAAS,CAAC,CAAA;EAC5D,MAAME,OAAO,GAAG,CAAC,GAAGH,eAAe,CAACE,WAAW,CAACC,OAAO,CAAC,CAAA;AACxD,EAAA,MAAMC,MAAuB,GAAG;AAC9B,IAAA,GAAGJ,eAAe;AAClBE,IAAAA,WAAW,EAAE;MAAED,SAAS;AAAEE,MAAAA,OAAAA;AAAQ,KAAA;GACnC,CAAA;;AAED;EACA,MAAME,aAAa,GAAG,CAAC,GAAGF,OAAO,CAAC,CAACG,IAAI,CACrC,CAACC,CAAC,EAAEC,CAAC,KAAKD,CAAC,CAACE,KAAK,CAACC,aAAa,GAAGF,CAAC,CAACC,KAAK,CAACC,aAC5C,CAAC,CAAA;;AAED;AACA,EAAA,IAAIL,aAAa,CAACM,MAAM,GAAG,CAAC,EAAE;AAC5B,IAAA,MAAMC,UAAU,GAAGP,aAAa,CAACQ,KAAK,EAAE,CAAA;AACxCT,IAAAA,MAAM,CAACF,WAAW,CAACC,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;AACpE,IAAA,OAAOR,MAAM,CAAA;AACf,GAAA;AAEA,EAAA,OAAOY,SAAS,CAAA;AAClB;;;;"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const removeOldestQuery = ({
|
|
4
|
+
persistedClient
|
|
5
|
+
}) => {
|
|
6
|
+
const mutations = [...persistedClient.clientState.mutations];
|
|
7
|
+
const queries = [...persistedClient.clientState.queries];
|
|
8
|
+
const client = {
|
|
9
|
+
...persistedClient,
|
|
10
|
+
clientState: {
|
|
11
|
+
mutations,
|
|
12
|
+
queries
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// sort queries by dataUpdatedAt (oldest first)
|
|
17
|
+
const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt);
|
|
18
|
+
|
|
19
|
+
// clean oldest query
|
|
20
|
+
if (sortedQueries.length > 0) {
|
|
21
|
+
const oldestData = sortedQueries.shift();
|
|
22
|
+
client.clientState.queries = queries.filter(q => q !== oldestData);
|
|
23
|
+
return client;
|
|
24
|
+
}
|
|
25
|
+
return undefined;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.removeOldestQuery = removeOldestQuery;
|
|
29
|
+
//# sourceMappingURL=retryStrategies.legacy.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retryStrategies.legacy.cjs","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"],"names":["removeOldestQuery","persistedClient","mutations","clientState","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":";;AAQO,MAAMA,iBAAiC,GAAGA,CAAC;AAAEC,EAAAA,eAAAA;AAAgB,CAAC,KAAK;EACxE,MAAMC,SAAS,GAAG,CAAC,GAAGD,eAAe,CAACE,WAAW,CAACD,SAAS,CAAC,CAAA;EAC5D,MAAME,OAAO,GAAG,CAAC,GAAGH,eAAe,CAACE,WAAW,CAACC,OAAO,CAAC,CAAA;AACxD,EAAA,MAAMC,MAAuB,GAAG;AAC9B,IAAA,GAAGJ,eAAe;AAClBE,IAAAA,WAAW,EAAE;MAAED,SAAS;AAAEE,MAAAA,OAAAA;AAAQ,KAAA;GACnC,CAAA;;AAED;EACA,MAAME,aAAa,GAAG,CAAC,GAAGF,OAAO,CAAC,CAACG,IAAI,CACrC,CAACC,CAAC,EAAEC,CAAC,KAAKD,CAAC,CAACE,KAAK,CAACC,aAAa,GAAGF,CAAC,CAACC,KAAK,CAACC,aAC5C,CAAC,CAAA;;AAED;AACA,EAAA,IAAIL,aAAa,CAACM,MAAM,GAAG,CAAC,EAAE;AAC5B,IAAA,MAAMC,UAAU,GAAGP,aAAa,CAACQ,KAAK,EAAE,CAAA;AACxCT,IAAAA,MAAM,CAACF,WAAW,CAACC,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;AACpE,IAAA,OAAOR,MAAM,CAAA;AACf,GAAA;AAEA,EAAA,OAAOY,SAAS,CAAA;AAClB;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retryStrategies.legacy.js","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"],"names":["removeOldestQuery","persistedClient","mutations","clientState","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":"AAQO,MAAMA,iBAAiC,GAAGA,CAAC;AAAEC,EAAAA,eAAAA;AAAgB,CAAC,KAAK;EACxE,MAAMC,SAAS,GAAG,CAAC,GAAGD,eAAe,CAACE,WAAW,CAACD,SAAS,CAAC,CAAA;EAC5D,MAAME,OAAO,GAAG,CAAC,GAAGH,eAAe,CAACE,WAAW,CAACC,OAAO,CAAC,CAAA;AACxD,EAAA,MAAMC,MAAuB,GAAG;AAC9B,IAAA,GAAGJ,eAAe;AAClBE,IAAAA,WAAW,EAAE;MAAED,SAAS;AAAEE,MAAAA,OAAAA;AAAQ,KAAA;GACnC,CAAA;;AAED;EACA,MAAME,aAAa,GAAG,CAAC,GAAGF,OAAO,CAAC,CAACG,IAAI,CACrC,CAACC,CAAC,EAAEC,CAAC,KAAKD,CAAC,CAACE,KAAK,CAACC,aAAa,GAAGF,CAAC,CAACC,KAAK,CAACC,aAC5C,CAAC,CAAA;;AAED;AACA,EAAA,IAAIL,aAAa,CAACM,MAAM,GAAG,CAAC,EAAE;AAC5B,IAAA,MAAMC,UAAU,GAAGP,aAAa,CAACQ,KAAK,EAAE,CAAA;AACxCT,IAAAA,MAAM,CAACF,WAAW,CAACC,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;AACpE,IAAA,OAAOR,MAAM,CAAA;AACf,GAAA;AAEA,EAAA,OAAOY,SAAS,CAAA;AAClB;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-persist-client-core",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.63",
|
|
4
4
|
"description": "Set of utilities for interacting with persisters, which can save your queryClient for later use",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,32 +10,36 @@
|
|
|
10
10
|
"type": "github",
|
|
11
11
|
"url": "https://github.com/sponsors/tannerlinsley"
|
|
12
12
|
},
|
|
13
|
+
"type": "module",
|
|
13
14
|
"types": "build/lib/index.d.ts",
|
|
14
|
-
"main": "build/lib/index.
|
|
15
|
-
"module": "build/lib/index.
|
|
15
|
+
"main": "build/lib/index.legacy.cjs",
|
|
16
|
+
"module": "build/lib/index.legacy.js",
|
|
16
17
|
"exports": {
|
|
17
18
|
".": {
|
|
18
19
|
"types": "./build/lib/index.d.ts",
|
|
19
|
-
"import": "./build/lib/index.
|
|
20
|
-
"
|
|
20
|
+
"import": "./build/lib/index.js",
|
|
21
|
+
"require": "./build/lib/index.cjs",
|
|
22
|
+
"default": "./build/lib/index.cjs"
|
|
21
23
|
},
|
|
22
24
|
"./package.json": "./package.json"
|
|
23
25
|
},
|
|
24
26
|
"sideEffects": false,
|
|
25
27
|
"files": [
|
|
26
28
|
"build/lib/*",
|
|
27
|
-
"build/umd/*",
|
|
28
29
|
"src"
|
|
29
30
|
],
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@tanstack/query-core": "5.0.0-alpha.
|
|
32
|
+
"@tanstack/query-core": "^5.0.0-alpha.43"
|
|
32
33
|
},
|
|
33
34
|
"scripts": {
|
|
34
|
-
"clean": "rimraf ./build",
|
|
35
|
+
"clean": "rimraf ./build && rimraf ./coverage",
|
|
35
36
|
"test:eslint": "eslint --ext .ts,.tsx ./src",
|
|
36
|
-
"test:types": "tsc",
|
|
37
|
+
"test:types": "tsc --noEmit",
|
|
37
38
|
"test:lib": "vitest run --coverage",
|
|
38
39
|
"test:lib:dev": "pnpm run test:lib --watch",
|
|
39
|
-
"build
|
|
40
|
+
"test:build": "publint --strict",
|
|
41
|
+
"build": "pnpm build:rollup && pnpm build:types",
|
|
42
|
+
"build:rollup": "rollup --config rollup.config.js",
|
|
43
|
+
"build:types": "tsc --emitDeclarationOnly"
|
|
40
44
|
}
|
|
41
45
|
}
|
package/src/__tests__/utils.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import type { QueryClientConfig } from '@tanstack/query-core'
|
|
2
2
|
import { QueryClient } from '@tanstack/query-core'
|
|
3
|
-
import type {
|
|
4
|
-
Persister,
|
|
5
|
-
PersistedClient,
|
|
6
|
-
} from '@tanstack/query-persist-client-core'
|
|
3
|
+
import type { Persister, PersistedClient } from '../persist'
|
|
7
4
|
import { vi } from 'vitest'
|
|
8
5
|
|
|
9
6
|
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
package/src/persist.ts
CHANGED
|
@@ -3,9 +3,9 @@ import type {
|
|
|
3
3
|
DehydratedState,
|
|
4
4
|
DehydrateOptions,
|
|
5
5
|
HydrateOptions,
|
|
6
|
+
NotifyEventType,
|
|
6
7
|
} from '@tanstack/query-core'
|
|
7
8
|
import { dehydrate, hydrate } from '@tanstack/query-core'
|
|
8
|
-
import type { NotifyEventType } from '@tanstack/query-core'
|
|
9
9
|
|
|
10
10
|
export type Promisable<T> = T | PromiseLike<T>
|
|
11
11
|
|
package/src/retryStrategies.ts
CHANGED
package/build/lib/index.esm.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
package/build/lib/index.mjs
DELETED
package/build/lib/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"persist.esm.js","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\nimport type { NotifyEventType } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;AAuDA;AACA;AACA;AACA;AACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;AAED,SAASC,oBAAoB,CAACC,SAA0B,EAAE;AACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;AAChD,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeE,yBAAyB,CAAC;EAC9CC,WAAW;EACXC,SAAS;AACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;AAC5BC,EAAAA,MAAM,GAAG,EAAE;AACXC,EAAAA,cAAAA;AACkC,CAAC,EAAE;EACrC,IAAI;AACF,IAAA,MAAMC,eAAe,GAAG,MAAMJ,SAAS,CAACK,aAAa,EAAE,CAAA;AAEvD,IAAA,IAAID,eAAe,EAAE;MACnB,IAAIA,eAAe,CAACE,SAAS,EAAE;QAC7B,MAAMC,OAAO,GAAGC,IAAI,CAACC,GAAG,EAAE,GAAGL,eAAe,CAACE,SAAS,GAAGL,MAAM,CAAA;AAC/D,QAAA,MAAMS,MAAM,GAAGN,eAAe,CAACF,MAAM,KAAKA,MAAM,CAAA;QAChD,IAAIK,OAAO,IAAIG,MAAM,EAAE;UACrBV,SAAS,CAACW,YAAY,EAAE,CAAA;AAC1B,SA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,CAC/CkC,gBAAgB,EAAE,CAClBJ,SAAS,CAAEC,KAAK,IAAK;AACpB,IAAA,IAAInC,oBAAoB,CAACmC,KAAK,CAACC,IAAI,CAAC,EAAE;MACpCV,sBAAsB,CAACK,KAAK,CAAC,CAAA;AAC/B,KAAA;AACF,GAAC,CAAC,CAAA;AAEJ,EAAA,OAAO,MAAM;AACXC,IAAAA,qBAAqB,EAAE,CAAA;AACvBK,IAAAA,wBAAwB,EAAE,CAAA;GAC3B,CAAA;AACH,CAAA;;AAEA;AACA;AACA;AACO,SAASE,kBAAkB,CAChCR,KAAgC,EACH;EAC7B,IAAIS,eAAe,GAAG,KAAK,CAAA;AAC3B,EAAA,IAAIC,6BAAuD,CAAA;EAC3D,MAAMC,WAAW,GAAG,MAAM;AACxBF,IAAAA,eAAe,GAAG,IAAI,CAAA;IACtBC,6BAA6B,IAAA,IAAA,GAAA,KAAA,CAAA,GAA7BA,6BAA6B,EAAI,CAAA;GAClC,CAAA;;AAED;EACA,MAAME,cAAc,GAAGxC,yBAAyB,CAAC4B,KAAK,CAAC,CAACa,IAAI,CAAC,MAAM;IACjE,IAAI,CAACJ,eAAe,EAAE;AACpB;AACAC,MAAAA,6BAA6B,GAAGX,2BAA2B,CAACC,KAAK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"persist.mjs","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\nimport type { NotifyEventType } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;AAuDA;AACA;AACA;AACA;AACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;AAED,SAASC,oBAAoB,CAACC,SAA0B,EAAE;AACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;AAChD,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeE,yBAAyB,CAAC;EAC9CC,WAAW;EACXC,SAAS;AACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;AAC5BC,EAAAA,MAAM,GAAG,EAAE;AACXC,EAAAA,cAAAA;AACkC,CAAC,EAAE;EACrC,IAAI;AACF,IAAA,MAAMC,eAAe,GAAG,MAAMJ,SAAS,CAACK,aAAa,EAAE,CAAA;AAEvD,IAAA,IAAID,eAAe,EAAE;MACnB,IAAIA,eAAe,CAACE,SAAS,EAAE;QAC7B,MAAMC,OAAO,GAAGC,IAAI,CAACC,GAAG,EAAE,GAAGL,eAAe,CAACE,SAAS,GAAGL,MAAM,CAAA;AAC/D,QAAA,MAAMS,MAAM,GAAGN,eAAe,CAACF,MAAM,KAAKA,MAAM,CAAA;QAChD,IAAIK,OAAO,IAAIG,MAAM,EAAE;UACrBV,SAAS,CAACW,YAAY,EAAE,CAAA;AAC1B,SAAC,MAAM;UACLC,OAAO,CAACb,WAAW,EAAEK,eAAe,CAACS,WAAW,EAAEV,cAAc,CAAC,CAAA;AACnE,SAAA;AACF,OAAC,MAAM;QACLH,SAAS,CAACW,YAAY,EAAE,CAAA;AAC1B,OAAA;AACF,KAAA;GACD,CAAC,OAAOG,GAAG,EA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AAEJ,EAAA,OAAO,MAAM;AACXC,IAAAA,qBAAqB,EAAE,CAAA;AACvBK,IAAAA,wBAAwB,EAAE,CAAA;GAC3B,CAAA;AACH,CAAA;;AAEA;AACA;AACA;AACO,SAASE,kBAAkB,CAChCR,KAAgC,EACH;EAC7B,IAAIS,eAAe,GAAG,KAAK,CAAA;AAC3B,EAAA,IAAIC,6BAAuD,CAAA;EAC3D,MAAMC,WAAW,GAAG,MAAM;AACxBF,IAAAA,eAAe,GAAG,IAAI,CAAA;AACtBC,IAAAA,6BAA6B,IAAI,CAAA;GAClC,CAAA;;AAED;EACA,MAAME,cAAc,GAAGxC,yBAAyB,CAAC4B,KAAK,CAAC,CAACa,IAAI,CAAC,MAAM;IACjE,IAAI,CAACJ,eAAe,EAAE;AACpB;AACAC,MAAAA,6BAA6B,GAAGX,2BAA2B,CAACC,KAAK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.esm.js","sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from '@tanstack/query-persist-client-core'\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"],"names":["removeOldestQuery","persistedClient","mutations","clientState","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":"AAQO,MAAMA,iBAAiC,GAAG,CAAC;AAAEC,EAAAA,eAAAA;AAAgB,CAAC,KAAK;EACxE,MAAMC,SAAS,GAAG,CAAC,GAAGD,eAAe,CAACE,WAAW,CAACD,SAAS,CAAC,CAAA;EAC5D,MAAME,OAAO,GAAG,CAAC,GAAGH,eAAe,CAACE,WAAW,CAACC,OAAO,CAAC,CAAA;AACxD,EAAA,MAAMC,MAAuB,GAAG;AAC9B,IAAA,GAAGJ,eAAe;AAClBE,IAAAA,WAAW,EAAE;MAAED,SAAS;AAAEE,MAAAA,OAAAA;AAAQ,KAAA;GACnC,CAAA;;AAED;EACA,MAAME,aAAa,GAAG,CAAC,GAAGF,OAAO,CAAC,CAACG,IAAI,CACrC,CAACC,CAAC,EAAEC,CAAC,KAAKD,CAAC,CAACE,KAAK,CAACC,aAAa,GAAGF,CAAC,CAACC,KAAK,CAACC,aAAa,CACxD,CAAA;;AAED;AACA,EAAA,IAAIL,aAAa,CAACM,MAAM,GAAG,CAAC,EAAE;AAC5B,IAAA,MAAMC,UAAU,GAAGP,aAAa,CAACQ,KAAK,EAAE,CAAA;AACxCT,IAAAA,MAAM,CAACF,WAAW,CAACC,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;AACpE,IAAA,OAAOR,MAAM,CAAA;AACf,GAAA;AAEA,EAAA,OAAOY,SAAS,CAAA;AAClB;;;;"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.mjs","sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from '@tanstack/query-persist-client-core'\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"],"names":["removeOldestQuery","persistedClient","mutations","clientState","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":"AAQO,MAAMA,iBAAiC,GAAG,CAAC;AAAEC,EAAAA,eAAAA;AAAgB,CAAC,KAAK;EACxE,MAAMC,SAAS,GAAG,CAAC,GAAGD,eAAe,CAACE,WAAW,CAACD,SAAS,CAAC,CAAA;EAC5D,MAAME,OAAO,GAAG,CAAC,GAAGH,eAAe,CAACE,WAAW,CAACC,OAAO,CAAC,CAAA;AACxD,EAAA,MAAMC,MAAuB,GAAG;AAC9B,IAAA,GAAGJ,eAAe;AAClBE,IAAAA,WAAW,EAAE;MAAED,SAAS;AAAEE,MAAAA,OAAAA;AAAQ,KAAA;GACnC,CAAA;;AAED;EACA,MAAME,aAAa,GAAG,CAAC,GAAGF,OAAO,CAAC,CAACG,IAAI,CACrC,CAACC,CAAC,EAAEC,CAAC,KAAKD,CAAC,CAACE,KAAK,CAACC,aAAa,GAAGF,CAAC,CAACC,KAAK,CAACC,aAAa,CACxD,CAAA;;AAED;AACA,EAAA,IAAIL,aAAa,CAACM,MAAM,GAAG,CAAC,EAAE;AAC5B,IAAA,MAAMC,UAAU,GAAGP,aAAa,CAACQ,KAAK,EAAE,CAAA;AACxCT,IAAAA,MAAM,CAACF,WAAW,CAACC,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;AACpE,IAAA,OAAOR,MAAM,CAAA;AACf,GAAA;AAEA,EAAA,OAAOY,SAAS,CAAA;AAClB;;;;"}
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
(function (global, factory) {
|
|
2
|
-
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tanstack/query-core')) :
|
|
3
|
-
typeof define === 'function' && define.amd ? define(['exports', '@tanstack/query-core'], factory) :
|
|
4
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.QueryPersistClientCore = {}, global.QueryCore));
|
|
5
|
-
})(this, (function (exports, queryCore) { 'use strict';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Checks if emitted event is about cache change and not about observers.
|
|
9
|
-
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
10
|
-
*/
|
|
11
|
-
const cacheableEventTypes = ['added', 'removed', 'updated'];
|
|
12
|
-
function isCacheableEventType(eventType) {
|
|
13
|
-
return cacheableEventTypes.includes(eventType);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
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
|
|
21
|
-
*/
|
|
22
|
-
async function persistQueryClientRestore({
|
|
23
|
-
queryClient,
|
|
24
|
-
persister,
|
|
25
|
-
maxAge = 1000 * 60 * 60 * 24,
|
|
26
|
-
buster = '',
|
|
27
|
-
hydrateOptions
|
|
28
|
-
}) {
|
|
29
|
-
try {
|
|
30
|
-
const persistedClient = await persister.restoreClient();
|
|
31
|
-
if (persistedClient) {
|
|
32
|
-
if (persistedClient.timestamp) {
|
|
33
|
-
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
34
|
-
const busted = persistedClient.buster !== buster;
|
|
35
|
-
if (expired || busted) {
|
|
36
|
-
persister.removeClient();
|
|
37
|
-
} else {
|
|
38
|
-
queryCore.hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
39
|
-
}
|
|
40
|
-
} else {
|
|
41
|
-
persister.removeClient();
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
} catch (err) {
|
|
45
|
-
{
|
|
46
|
-
console.error(err);
|
|
47
|
-
console.warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
48
|
-
}
|
|
49
|
-
persister.removeClient();
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Persists data from the QueryCache
|
|
55
|
-
* - data dehydrated using dehydrateOptions
|
|
56
|
-
* - data is persisted using persister.persistClient
|
|
57
|
-
*/
|
|
58
|
-
async function persistQueryClientSave({
|
|
59
|
-
queryClient,
|
|
60
|
-
persister,
|
|
61
|
-
buster = '',
|
|
62
|
-
dehydrateOptions
|
|
63
|
-
}) {
|
|
64
|
-
const persistClient = {
|
|
65
|
-
buster,
|
|
66
|
-
timestamp: Date.now(),
|
|
67
|
-
clientState: queryCore.dehydrate(queryClient, dehydrateOptions)
|
|
68
|
-
};
|
|
69
|
-
await persister.persistClient(persistClient);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
74
|
-
* @returns an unsubscribe function (to discontinue monitoring)
|
|
75
|
-
*/
|
|
76
|
-
function persistQueryClientSubscribe(props) {
|
|
77
|
-
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
|
|
78
|
-
if (isCacheableEventType(event.type)) {
|
|
79
|
-
persistQueryClientSave(props);
|
|
80
|
-
}
|
|
81
|
-
});
|
|
82
|
-
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(event => {
|
|
83
|
-
if (isCacheableEventType(event.type)) {
|
|
84
|
-
persistQueryClientSave(props);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
return () => {
|
|
88
|
-
unsubscribeQueryCache();
|
|
89
|
-
unusbscribeMutationCache();
|
|
90
|
-
};
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Restores persisted data to QueryCache and persists further changes.
|
|
95
|
-
*/
|
|
96
|
-
function persistQueryClient(props) {
|
|
97
|
-
let hasUnsubscribed = false;
|
|
98
|
-
let persistQueryClientUnsubscribe;
|
|
99
|
-
const unsubscribe = () => {
|
|
100
|
-
hasUnsubscribed = true;
|
|
101
|
-
persistQueryClientUnsubscribe?.();
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
// Attempt restore
|
|
105
|
-
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
106
|
-
if (!hasUnsubscribed) {
|
|
107
|
-
// Subscribe to changes in the query cache to trigger the save
|
|
108
|
-
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
return [unsubscribe, restorePromise];
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const removeOldestQuery = ({
|
|
115
|
-
persistedClient
|
|
116
|
-
}) => {
|
|
117
|
-
const mutations = [...persistedClient.clientState.mutations];
|
|
118
|
-
const queries = [...persistedClient.clientState.queries];
|
|
119
|
-
const client = {
|
|
120
|
-
...persistedClient,
|
|
121
|
-
clientState: {
|
|
122
|
-
mutations,
|
|
123
|
-
queries
|
|
124
|
-
}
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
// sort queries by dataUpdatedAt (oldest first)
|
|
128
|
-
const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt);
|
|
129
|
-
|
|
130
|
-
// clean oldest query
|
|
131
|
-
if (sortedQueries.length > 0) {
|
|
132
|
-
const oldestData = sortedQueries.shift();
|
|
133
|
-
client.clientState.queries = queries.filter(q => q !== oldestData);
|
|
134
|
-
return client;
|
|
135
|
-
}
|
|
136
|
-
return undefined;
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
exports.persistQueryClient = persistQueryClient;
|
|
140
|
-
exports.persistQueryClientRestore = persistQueryClientRestore;
|
|
141
|
-
exports.persistQueryClientSave = persistQueryClientSave;
|
|
142
|
-
exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
|
|
143
|
-
exports.removeOldestQuery = removeOldestQuery;
|
|
144
|
-
|
|
145
|
-
}));
|
|
146
|
-
//# sourceMappingURL=index.development.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.development.js","sources":["../../src/persist.ts","../../src/retryStrategies.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\nimport type { NotifyEventType } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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","import type { PersistedClient } from '@tanstack/query-persist-client-core'\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then","removeOldestQuery","mutations","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":";;;;;;EAuDA;EACA;EACA;EACA;EACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;EAED,SAASC,oBAAoB,CAACC,SAA0B,EAAE;EACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;EAChD,CAAA;;EAEA;EACA;EACA;EACA;EACA;EACA;EACO,eAAeE,yBAAyB,CAAC;IAC9CC,WAAW;IACXC,SAAS;EACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;EAC5BC,EAAAA,MAAM,GAAG,EAAE;EACXC,EAAAA,cAAAA;EACkC,CAAC,EAAE;IACrC,IAAI;EACF,IAAA,MAAMC,eAAe,GAAG,MAAMJ,SAAS,CAACK,aAAa,EAAE,CAAA;EAEvD,IAAA,IAAID,eAAe,EAAE;QACnB,IAAIA,eAAe,CAACE,SAAS,EAAE;UAC7B,MAAMC,OAAO,GAAGC,IAAI,CAACC,GAAG,EAAE,GAAGL,eAAe,CAACE,SAAS,GAAGL,MAAM,CAAA;EAC/D,QAAA,MAAMS,MAAM,GAAGN,eAAe,CAACF,MAAM,KAAKA,MAAM,CAAA;UAChD,IAAIK,OAAO,IAAIG,MAAM,EAAE;YACrBV,SAAS,CAACW,YAAY,EAAE,CAAA;EAC1B,SAAC,MAAM;YACLC,iBAAO,CAACb,WAAW,EAAEK,eAAe,CAACS,WAAW,EAAEV,cAAc,CAAC,CAAA;EACnE,SAAA;EACF,OAAC,MAAM;UACLH,SAAS,CAACW,YAAY,EAAE,CAAA;EAC1B,OAAA;EACF,KAAA;KACD,CAAC,OAAOG,GAAG,EAAE;EACZ,IAA2C;EACzCC,MAAAA,OAAO,CAACC,KAAK,CAACF,GAAG,CAAC,CAAA;EAClBC,MAAAA,O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,WAAW,CAAC0B,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;EACpE,IAAA,OAAOR,MAAM,CAAA;EACf,GAAA;EAEA,EAAA,OAAOY,SAAS,CAAA;EAClB;;;;;;;;;;;;"}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@tanstack/query-core")):"function"==typeof define&&define.amd?define(["exports","@tanstack/query-core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).QueryPersistClientCore={},e.QueryCore)}(this,(function(e,t){"use strict";const i=["added","removed","updated"];function n(e){return i.includes(e)}async function r({queryClient:e,persister:i,maxAge:n=864e5,buster:r="",hydrateOptions:s}){try{const a=await i.restoreClient();if(a)if(a.timestamp){const u=Date.now()-a.timestamp>n,o=a.buster!==r;u||o?i.removeClient():t.hydrate(e,a.clientState,s)}else i.removeClient()}catch(e){i.removeClient()}}async function s({queryClient:e,persister:i,buster:n="",dehydrateOptions:r}){const s={buster:n,timestamp:Date.now(),clientState:t.dehydrate(e,r)};await i.persistClient(s)}function a(e){const t=e.queryClient.getQueryCache().subscribe((t=>{n(t.type)&&s(e)})),i=e.queryClient.getMutationCache().subscribe((t=>{n(t.type)&&s(e)}));return()=>{t(),i()}}e.persistQueryClient=function(e){let t,i=!1;return[()=>{i=!0,t?.()},r(e).then((()=>{i||(t=a(e))}))]},e.persistQueryClientRestore=r,e.persistQueryClientSave=s,e.persistQueryClientSubscribe=a,e.removeOldestQuery=({persistedClient:e})=>{const t=[...e.clientState.mutations],i=[...e.clientState.queries],n={...e,clientState:{mutations:t,queries:i}},r=[...i].sort(((e,t)=>e.state.dataUpdatedAt-t.state.dataUpdatedAt));if(r.length>0){const e=r.shift();return n.clientState.queries=i.filter((t=>t!==e)),n}}}));
|
|
2
|
-
//# sourceMappingURL=index.production.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.production.js","sources":["../../src/persist.ts","../../src/retryStrategies.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\nimport type { NotifyEventType } 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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\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 maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\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 */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.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 */\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 persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n 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 persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\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 * @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 (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\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","import type { PersistedClient } from '@tanstack/query-persist-client-core'\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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","async","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClientUnsubscribe","hasUnsubscribed","then","mutations","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q"],"mappings":"mUA2DA,MAAMA,EAA8C,CAClD,QACA,UACA,WAGF,SAASC,EAAqBC,GAC5B,OAAOF,EAAoBG,SAASD,EACtC,CAQOE,eAAeC,GAA0BC,YAC9CA,EAAWC,UACXA,EAASC,OACTA,EAAS,MAAmBC,OAC5BA,EAAS,GAAEC,eACXA,IAEA,IACE,MAAMC,QAAwBJ,EAAUK,gBAExC,G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