@tanstack/query-persist-client-core 4.24.10 → 5.0.0-alpha.1
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__/utils.d.ts +0 -6
- package/build/lib/index.js +0 -2
- package/build/lib/index.js.map +1 -1
- package/build/lib/persist.esm.js +9 -15
- package/build/lib/persist.esm.js.map +1 -1
- package/build/lib/persist.js +9 -17
- package/build/lib/persist.js.map +1 -1
- package/build/lib/persist.mjs +9 -15
- package/build/lib/persist.mjs.map +1 -1
- package/build/lib/retryStrategies.esm.js +6 -4
- package/build/lib/retryStrategies.esm.js.map +1 -1
- package/build/lib/retryStrategies.js +6 -6
- package/build/lib/retryStrategies.js.map +1 -1
- package/build/lib/retryStrategies.mjs +6 -4
- package/build/lib/retryStrategies.mjs.map +1 -1
- package/build/umd/index.development.js +15 -21
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/__tests__/persist.test.ts +0 -1
- package/src/__tests__/utils.ts +1 -8
- package/src/persist.ts +4 -6
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
/// <reference types="jest" />
|
|
2
1
|
import type { QueryClientConfig } from '@tanstack/query-core';
|
|
3
2
|
import { QueryClient } from '@tanstack/query-core';
|
|
4
3
|
import type { Persister } from '@tanstack/query-persist-client-core';
|
|
5
4
|
export declare function createQueryClient(config?: QueryClientConfig): QueryClient;
|
|
6
|
-
export declare const mockLogger: {
|
|
7
|
-
log: jest.Mock<any, any>;
|
|
8
|
-
warn: jest.Mock<any, any>;
|
|
9
|
-
error: jest.Mock<any, any>;
|
|
10
|
-
};
|
|
11
5
|
export declare function sleep(timeout: number): Promise<void>;
|
|
12
6
|
export declare const createMockPersister: () => Persister;
|
|
13
7
|
export declare const createSpyablePersister: () => Persister;
|
package/build/lib/index.js
CHANGED
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":";;;;;;;;;;;"}
|
package/build/lib/persist.esm.js
CHANGED
|
@@ -5,18 +5,16 @@ import { hydrate, dehydrate } from '@tanstack/query-core';
|
|
|
5
5
|
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
6
6
|
*/
|
|
7
7
|
const cacheableEventTypes = ['added', 'removed', 'updated'];
|
|
8
|
-
|
|
9
8
|
function isCacheableEventType(eventType) {
|
|
10
9
|
return cacheableEventTypes.includes(eventType);
|
|
11
10
|
}
|
|
11
|
+
|
|
12
12
|
/**
|
|
13
13
|
* Restores persisted data to the QueryCache
|
|
14
14
|
* - data obtained from persister.restoreClient
|
|
15
15
|
* - data is hydrated using hydrateOptions
|
|
16
16
|
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
18
|
async function persistQueryClientRestore({
|
|
21
19
|
queryClient,
|
|
22
20
|
persister,
|
|
@@ -26,12 +24,10 @@ async function persistQueryClientRestore({
|
|
|
26
24
|
}) {
|
|
27
25
|
try {
|
|
28
26
|
const persistedClient = await persister.restoreClient();
|
|
29
|
-
|
|
30
27
|
if (persistedClient) {
|
|
31
28
|
if (persistedClient.timestamp) {
|
|
32
29
|
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
33
30
|
const busted = persistedClient.buster !== buster;
|
|
34
|
-
|
|
35
31
|
if (expired || busted) {
|
|
36
32
|
persister.removeClient();
|
|
37
33
|
} else {
|
|
@@ -43,19 +39,18 @@ async function persistQueryClientRestore({
|
|
|
43
39
|
}
|
|
44
40
|
} catch (err) {
|
|
45
41
|
if (process.env.NODE_ENV !== 'production') {
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
console.error(err);
|
|
43
|
+
console.warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
48
44
|
}
|
|
49
|
-
|
|
50
45
|
persister.removeClient();
|
|
51
46
|
}
|
|
52
47
|
}
|
|
48
|
+
|
|
53
49
|
/**
|
|
54
50
|
* Persists data from the QueryCache
|
|
55
51
|
* - data dehydrated using dehydrateOptions
|
|
56
52
|
* - data is persisted using persister.persistClient
|
|
57
53
|
*/
|
|
58
|
-
|
|
59
54
|
async function persistQueryClientSave({
|
|
60
55
|
queryClient,
|
|
61
56
|
persister,
|
|
@@ -69,11 +64,11 @@ async function persistQueryClientSave({
|
|
|
69
64
|
};
|
|
70
65
|
await persister.persistClient(persistClient);
|
|
71
66
|
}
|
|
67
|
+
|
|
72
68
|
/**
|
|
73
69
|
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
74
70
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
75
71
|
*/
|
|
76
|
-
|
|
77
72
|
function persistQueryClientSubscribe(props) {
|
|
78
73
|
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
|
|
79
74
|
if (isCacheableEventType(event.type)) {
|
|
@@ -90,20 +85,19 @@ function persistQueryClientSubscribe(props) {
|
|
|
90
85
|
unusbscribeMutationCache();
|
|
91
86
|
};
|
|
92
87
|
}
|
|
88
|
+
|
|
93
89
|
/**
|
|
94
90
|
* Restores persisted data to QueryCache and persists further changes.
|
|
95
91
|
*/
|
|
96
|
-
|
|
97
92
|
function persistQueryClient(props) {
|
|
98
93
|
let hasUnsubscribed = false;
|
|
99
94
|
let persistQueryClientUnsubscribe;
|
|
100
|
-
|
|
101
95
|
const unsubscribe = () => {
|
|
102
96
|
hasUnsubscribed = true;
|
|
103
|
-
persistQueryClientUnsubscribe
|
|
104
|
-
};
|
|
105
|
-
|
|
97
|
+
persistQueryClientUnsubscribe?.();
|
|
98
|
+
};
|
|
106
99
|
|
|
100
|
+
// Attempt restore
|
|
107
101
|
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
108
102
|
if (!hasUnsubscribed) {
|
|
109
103
|
// Subscribe to changes in the query cache to trigger the save
|
|
@@ -1 +1 @@
|
|
|
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
|
|
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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B,CAACC,KAAK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;"}
|
package/build/lib/persist.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var queryCore = require('@tanstack/query-core');
|
|
6
4
|
|
|
7
5
|
/**
|
|
@@ -9,18 +7,16 @@ var queryCore = require('@tanstack/query-core');
|
|
|
9
7
|
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
10
8
|
*/
|
|
11
9
|
const cacheableEventTypes = ['added', 'removed', 'updated'];
|
|
12
|
-
|
|
13
10
|
function isCacheableEventType(eventType) {
|
|
14
11
|
return cacheableEventTypes.includes(eventType);
|
|
15
12
|
}
|
|
13
|
+
|
|
16
14
|
/**
|
|
17
15
|
* Restores persisted data to the QueryCache
|
|
18
16
|
* - data obtained from persister.restoreClient
|
|
19
17
|
* - data is hydrated using hydrateOptions
|
|
20
18
|
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
21
19
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
20
|
async function persistQueryClientRestore({
|
|
25
21
|
queryClient,
|
|
26
22
|
persister,
|
|
@@ -30,12 +26,10 @@ async function persistQueryClientRestore({
|
|
|
30
26
|
}) {
|
|
31
27
|
try {
|
|
32
28
|
const persistedClient = await persister.restoreClient();
|
|
33
|
-
|
|
34
29
|
if (persistedClient) {
|
|
35
30
|
if (persistedClient.timestamp) {
|
|
36
31
|
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
37
32
|
const busted = persistedClient.buster !== buster;
|
|
38
|
-
|
|
39
33
|
if (expired || busted) {
|
|
40
34
|
persister.removeClient();
|
|
41
35
|
} else {
|
|
@@ -47,19 +41,18 @@ async function persistQueryClientRestore({
|
|
|
47
41
|
}
|
|
48
42
|
} catch (err) {
|
|
49
43
|
if (process.env.NODE_ENV !== 'production') {
|
|
50
|
-
|
|
51
|
-
|
|
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.');
|
|
52
46
|
}
|
|
53
|
-
|
|
54
47
|
persister.removeClient();
|
|
55
48
|
}
|
|
56
49
|
}
|
|
50
|
+
|
|
57
51
|
/**
|
|
58
52
|
* Persists data from the QueryCache
|
|
59
53
|
* - data dehydrated using dehydrateOptions
|
|
60
54
|
* - data is persisted using persister.persistClient
|
|
61
55
|
*/
|
|
62
|
-
|
|
63
56
|
async function persistQueryClientSave({
|
|
64
57
|
queryClient,
|
|
65
58
|
persister,
|
|
@@ -73,11 +66,11 @@ async function persistQueryClientSave({
|
|
|
73
66
|
};
|
|
74
67
|
await persister.persistClient(persistClient);
|
|
75
68
|
}
|
|
69
|
+
|
|
76
70
|
/**
|
|
77
71
|
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
78
72
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
79
73
|
*/
|
|
80
|
-
|
|
81
74
|
function persistQueryClientSubscribe(props) {
|
|
82
75
|
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
|
|
83
76
|
if (isCacheableEventType(event.type)) {
|
|
@@ -94,20 +87,19 @@ function persistQueryClientSubscribe(props) {
|
|
|
94
87
|
unusbscribeMutationCache();
|
|
95
88
|
};
|
|
96
89
|
}
|
|
90
|
+
|
|
97
91
|
/**
|
|
98
92
|
* Restores persisted data to QueryCache and persists further changes.
|
|
99
93
|
*/
|
|
100
|
-
|
|
101
94
|
function persistQueryClient(props) {
|
|
102
95
|
let hasUnsubscribed = false;
|
|
103
96
|
let persistQueryClientUnsubscribe;
|
|
104
|
-
|
|
105
97
|
const unsubscribe = () => {
|
|
106
98
|
hasUnsubscribed = true;
|
|
107
|
-
persistQueryClientUnsubscribe
|
|
108
|
-
};
|
|
109
|
-
|
|
99
|
+
persistQueryClientUnsubscribe?.();
|
|
100
|
+
};
|
|
110
101
|
|
|
102
|
+
// Attempt restore
|
|
111
103
|
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
112
104
|
if (!hasUnsubscribed) {
|
|
113
105
|
// Subscribe to changes in the query cache to trigger the save
|
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'\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
|
|
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'\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,mB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AA;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.mjs
CHANGED
|
@@ -5,18 +5,16 @@ import { hydrate, dehydrate } from '@tanstack/query-core';
|
|
|
5
5
|
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
6
6
|
*/
|
|
7
7
|
const cacheableEventTypes = ['added', 'removed', 'updated'];
|
|
8
|
-
|
|
9
8
|
function isCacheableEventType(eventType) {
|
|
10
9
|
return cacheableEventTypes.includes(eventType);
|
|
11
10
|
}
|
|
11
|
+
|
|
12
12
|
/**
|
|
13
13
|
* Restores persisted data to the QueryCache
|
|
14
14
|
* - data obtained from persister.restoreClient
|
|
15
15
|
* - data is hydrated using hydrateOptions
|
|
16
16
|
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
17
17
|
*/
|
|
18
|
-
|
|
19
|
-
|
|
20
18
|
async function persistQueryClientRestore({
|
|
21
19
|
queryClient,
|
|
22
20
|
persister,
|
|
@@ -26,12 +24,10 @@ async function persistQueryClientRestore({
|
|
|
26
24
|
}) {
|
|
27
25
|
try {
|
|
28
26
|
const persistedClient = await persister.restoreClient();
|
|
29
|
-
|
|
30
27
|
if (persistedClient) {
|
|
31
28
|
if (persistedClient.timestamp) {
|
|
32
29
|
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
33
30
|
const busted = persistedClient.buster !== buster;
|
|
34
|
-
|
|
35
31
|
if (expired || busted) {
|
|
36
32
|
persister.removeClient();
|
|
37
33
|
} else {
|
|
@@ -43,19 +39,18 @@ async function persistQueryClientRestore({
|
|
|
43
39
|
}
|
|
44
40
|
} catch (err) {
|
|
45
41
|
if (process.env.NODE_ENV !== 'production') {
|
|
46
|
-
|
|
47
|
-
|
|
42
|
+
console.error(err);
|
|
43
|
+
console.warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
48
44
|
}
|
|
49
|
-
|
|
50
45
|
persister.removeClient();
|
|
51
46
|
}
|
|
52
47
|
}
|
|
48
|
+
|
|
53
49
|
/**
|
|
54
50
|
* Persists data from the QueryCache
|
|
55
51
|
* - data dehydrated using dehydrateOptions
|
|
56
52
|
* - data is persisted using persister.persistClient
|
|
57
53
|
*/
|
|
58
|
-
|
|
59
54
|
async function persistQueryClientSave({
|
|
60
55
|
queryClient,
|
|
61
56
|
persister,
|
|
@@ -69,11 +64,11 @@ async function persistQueryClientSave({
|
|
|
69
64
|
};
|
|
70
65
|
await persister.persistClient(persistClient);
|
|
71
66
|
}
|
|
67
|
+
|
|
72
68
|
/**
|
|
73
69
|
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
74
70
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
75
71
|
*/
|
|
76
|
-
|
|
77
72
|
function persistQueryClientSubscribe(props) {
|
|
78
73
|
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
|
|
79
74
|
if (isCacheableEventType(event.type)) {
|
|
@@ -90,20 +85,19 @@ function persistQueryClientSubscribe(props) {
|
|
|
90
85
|
unusbscribeMutationCache();
|
|
91
86
|
};
|
|
92
87
|
}
|
|
88
|
+
|
|
93
89
|
/**
|
|
94
90
|
* Restores persisted data to QueryCache and persists further changes.
|
|
95
91
|
*/
|
|
96
|
-
|
|
97
92
|
function persistQueryClient(props) {
|
|
98
93
|
let hasUnsubscribed = false;
|
|
99
94
|
let persistQueryClientUnsubscribe;
|
|
100
|
-
|
|
101
95
|
const unsubscribe = () => {
|
|
102
96
|
hasUnsubscribed = true;
|
|
103
|
-
persistQueryClientUnsubscribe
|
|
104
|
-
};
|
|
105
|
-
|
|
97
|
+
persistQueryClientUnsubscribe?.();
|
|
98
|
+
};
|
|
106
99
|
|
|
100
|
+
// Attempt restore
|
|
107
101
|
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
108
102
|
if (!hasUnsubscribed) {
|
|
109
103
|
// Subscribe to changes in the query cache to trigger the save
|
|
@@ -1 +1 @@
|
|
|
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
|
|
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,CA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AAA;;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;;;;"}
|
|
@@ -3,21 +3,23 @@ const removeOldestQuery = ({
|
|
|
3
3
|
}) => {
|
|
4
4
|
const mutations = [...persistedClient.clientState.mutations];
|
|
5
5
|
const queries = [...persistedClient.clientState.queries];
|
|
6
|
-
const client = {
|
|
6
|
+
const client = {
|
|
7
|
+
...persistedClient,
|
|
7
8
|
clientState: {
|
|
8
9
|
mutations,
|
|
9
10
|
queries
|
|
10
11
|
}
|
|
11
|
-
};
|
|
12
|
+
};
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
// sort queries by dataUpdatedAt (oldest first)
|
|
15
|
+
const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt);
|
|
14
16
|
|
|
17
|
+
// clean oldest query
|
|
15
18
|
if (sortedQueries.length > 0) {
|
|
16
19
|
const oldestData = sortedQueries.shift();
|
|
17
20
|
client.clientState.queries = queries.filter(q => q !== oldestData);
|
|
18
21
|
return client;
|
|
19
22
|
}
|
|
20
|
-
|
|
21
23
|
return undefined;
|
|
22
24
|
};
|
|
23
25
|
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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,27 +1,27 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
const removeOldestQuery = ({
|
|
6
4
|
persistedClient
|
|
7
5
|
}) => {
|
|
8
6
|
const mutations = [...persistedClient.clientState.mutations];
|
|
9
7
|
const queries = [...persistedClient.clientState.queries];
|
|
10
|
-
const client = {
|
|
8
|
+
const client = {
|
|
9
|
+
...persistedClient,
|
|
11
10
|
clientState: {
|
|
12
11
|
mutations,
|
|
13
12
|
queries
|
|
14
13
|
}
|
|
15
|
-
};
|
|
14
|
+
};
|
|
16
15
|
|
|
17
|
-
|
|
16
|
+
// sort queries by dataUpdatedAt (oldest first)
|
|
17
|
+
const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt);
|
|
18
18
|
|
|
19
|
+
// clean oldest query
|
|
19
20
|
if (sortedQueries.length > 0) {
|
|
20
21
|
const oldestData = sortedQueries.shift();
|
|
21
22
|
client.clientState.queries = queries.filter(q => q !== oldestData);
|
|
22
23
|
return client;
|
|
23
24
|
}
|
|
24
|
-
|
|
25
25
|
return undefined;
|
|
26
26
|
};
|
|
27
27
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"retryStrategies.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":"
|
|
1
|
+
{"version":3,"file":"retryStrategies.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;;;;"}
|
|
@@ -3,21 +3,23 @@ const removeOldestQuery = ({
|
|
|
3
3
|
}) => {
|
|
4
4
|
const mutations = [...persistedClient.clientState.mutations];
|
|
5
5
|
const queries = [...persistedClient.clientState.queries];
|
|
6
|
-
const client = {
|
|
6
|
+
const client = {
|
|
7
|
+
...persistedClient,
|
|
7
8
|
clientState: {
|
|
8
9
|
mutations,
|
|
9
10
|
queries
|
|
10
11
|
}
|
|
11
|
-
};
|
|
12
|
+
};
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
// sort queries by dataUpdatedAt (oldest first)
|
|
15
|
+
const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt);
|
|
14
16
|
|
|
17
|
+
// clean oldest query
|
|
15
18
|
if (sortedQueries.length > 0) {
|
|
16
19
|
const oldestData = sortedQueries.shift();
|
|
17
20
|
client.clientState.queries = queries.filter(q => q !== oldestData);
|
|
18
21
|
return client;
|
|
19
22
|
}
|
|
20
|
-
|
|
21
23
|
return undefined;
|
|
22
24
|
};
|
|
23
25
|
|
|
@@ -1 +1 @@
|
|
|
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;
|
|
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;;;;"}
|
|
@@ -9,18 +9,16 @@
|
|
|
9
9
|
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
10
10
|
*/
|
|
11
11
|
const cacheableEventTypes = ['added', 'removed', 'updated'];
|
|
12
|
-
|
|
13
12
|
function isCacheableEventType(eventType) {
|
|
14
13
|
return cacheableEventTypes.includes(eventType);
|
|
15
14
|
}
|
|
15
|
+
|
|
16
16
|
/**
|
|
17
17
|
* Restores persisted data to the QueryCache
|
|
18
18
|
* - data obtained from persister.restoreClient
|
|
19
19
|
* - data is hydrated using hydrateOptions
|
|
20
20
|
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
21
21
|
*/
|
|
22
|
-
|
|
23
|
-
|
|
24
22
|
async function persistQueryClientRestore({
|
|
25
23
|
queryClient,
|
|
26
24
|
persister,
|
|
@@ -30,12 +28,10 @@
|
|
|
30
28
|
}) {
|
|
31
29
|
try {
|
|
32
30
|
const persistedClient = await persister.restoreClient();
|
|
33
|
-
|
|
34
31
|
if (persistedClient) {
|
|
35
32
|
if (persistedClient.timestamp) {
|
|
36
33
|
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
37
34
|
const busted = persistedClient.buster !== buster;
|
|
38
|
-
|
|
39
35
|
if (expired || busted) {
|
|
40
36
|
persister.removeClient();
|
|
41
37
|
} else {
|
|
@@ -47,19 +43,18 @@
|
|
|
47
43
|
}
|
|
48
44
|
} catch (err) {
|
|
49
45
|
{
|
|
50
|
-
|
|
51
|
-
|
|
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.');
|
|
52
48
|
}
|
|
53
|
-
|
|
54
49
|
persister.removeClient();
|
|
55
50
|
}
|
|
56
51
|
}
|
|
52
|
+
|
|
57
53
|
/**
|
|
58
54
|
* Persists data from the QueryCache
|
|
59
55
|
* - data dehydrated using dehydrateOptions
|
|
60
56
|
* - data is persisted using persister.persistClient
|
|
61
57
|
*/
|
|
62
|
-
|
|
63
58
|
async function persistQueryClientSave({
|
|
64
59
|
queryClient,
|
|
65
60
|
persister,
|
|
@@ -73,11 +68,11 @@
|
|
|
73
68
|
};
|
|
74
69
|
await persister.persistClient(persistClient);
|
|
75
70
|
}
|
|
71
|
+
|
|
76
72
|
/**
|
|
77
73
|
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
78
74
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
79
75
|
*/
|
|
80
|
-
|
|
81
76
|
function persistQueryClientSubscribe(props) {
|
|
82
77
|
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
|
|
83
78
|
if (isCacheableEventType(event.type)) {
|
|
@@ -94,20 +89,19 @@
|
|
|
94
89
|
unusbscribeMutationCache();
|
|
95
90
|
};
|
|
96
91
|
}
|
|
92
|
+
|
|
97
93
|
/**
|
|
98
94
|
* Restores persisted data to QueryCache and persists further changes.
|
|
99
95
|
*/
|
|
100
|
-
|
|
101
96
|
function persistQueryClient(props) {
|
|
102
97
|
let hasUnsubscribed = false;
|
|
103
98
|
let persistQueryClientUnsubscribe;
|
|
104
|
-
|
|
105
99
|
const unsubscribe = () => {
|
|
106
100
|
hasUnsubscribed = true;
|
|
107
|
-
persistQueryClientUnsubscribe
|
|
108
|
-
};
|
|
109
|
-
|
|
101
|
+
persistQueryClientUnsubscribe?.();
|
|
102
|
+
};
|
|
110
103
|
|
|
104
|
+
// Attempt restore
|
|
111
105
|
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
112
106
|
if (!hasUnsubscribed) {
|
|
113
107
|
// Subscribe to changes in the query cache to trigger the save
|
|
@@ -122,21 +116,23 @@
|
|
|
122
116
|
}) => {
|
|
123
117
|
const mutations = [...persistedClient.clientState.mutations];
|
|
124
118
|
const queries = [...persistedClient.clientState.queries];
|
|
125
|
-
const client = {
|
|
119
|
+
const client = {
|
|
120
|
+
...persistedClient,
|
|
126
121
|
clientState: {
|
|
127
122
|
mutations,
|
|
128
123
|
queries
|
|
129
124
|
}
|
|
130
|
-
};
|
|
125
|
+
};
|
|
131
126
|
|
|
132
|
-
|
|
127
|
+
// sort queries by dataUpdatedAt (oldest first)
|
|
128
|
+
const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt);
|
|
133
129
|
|
|
130
|
+
// clean oldest query
|
|
134
131
|
if (sortedQueries.length > 0) {
|
|
135
132
|
const oldestData = sortedQueries.shift();
|
|
136
133
|
client.clientState.queries = queries.filter(q => q !== oldestData);
|
|
137
134
|
return client;
|
|
138
135
|
}
|
|
139
|
-
|
|
140
136
|
return undefined;
|
|
141
137
|
};
|
|
142
138
|
|
|
@@ -146,7 +142,5 @@
|
|
|
146
142
|
exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
|
|
147
143
|
exports.removeOldestQuery = removeOldestQuery;
|
|
148
144
|
|
|
149
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
150
|
-
|
|
151
145
|
}));
|
|
152
146
|
//# sourceMappingURL=index.development.js.map
|
|
@@ -1 +1 @@
|
|
|
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 queryClient.getLogger().error(err)\n queryClient\n .getLogger()\n .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","getLogger","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,OADkD,EAElD,SAFkD,EAGlD,SAHkD,CAApD,CAAA;;EAMA,SAASC,oBAAT,CAA8BC,SAA9B,EAA0D;EACxD,EAAA,OAAOF,mBAAmB,CAACG,QAApB,CAA6BD,SAA7B,CAAP,CAAA;EACD,CAAA;EAED;EACA;EACA;EACA;EACA;EACA;;;EACO,eAAeE,yBAAf,CAAyC;IAC9CC,WA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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;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,CAACJ,eAAe,EAAE;EACpB;EACAC,MAAAA,6BAA6B,GAAGX,2BAA2B,CAACC,KAAK,CAAC,CAAA;EACpE,KAAA;EACF,GAAC,CAAC,CAAA;EAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;EACtC;;AC5KO,QAAME,iBAAiC,GAAG,CAAC;EAAEjC,EAAAA,eAAAA;EAAgB,CAAC,KAAK;IACxE,MAAMkC,SAAS,GAAG,CAAC,GAAGlC,eAAe,CAACS,WAAW,CAACyB,SAAS,CAAC,CAAA;IAC5D,MAAMC,OAAO,GAAG,CAAC,GAAGnC,eAAe,CAACS,WAAW,CAAC0B,OAAO,CAAC,CAAA;EACxD,EAAA,MAAMC,MAAuB,GAAG;EAC9B,IAAA,GAAGpC,eAAe;EAClBS,IAAAA,WAAW,EAAE;QAAEyB,SAAS;EAAEC,MAAAA,OAAAA;EAAQ,KAAA;KACnC,CAAA;;EAED;IACA,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;;EAED;EACA,EAAA,IAAIL,aAAa,CAACM,MAAM,GAAG,CAAC,EAAE;EAC5B,IAAA,MAAMC,UAAU,GAAGP,aAAa,CAACQ,KAAK,EAAE,CAAA;EACxCT,IAAAA,MAAM,CAAC3B,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 +1,2 @@
|
|
|
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
|
|
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
2
|
//# sourceMappingURL=index.production.js.map
|
|
@@ -1 +1 @@
|
|
|
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
|
|
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,GAS/BE,eAAeC,GAA0BC,YAC9CA,EAAWC,UACXA,EAASC,OACTA,EAAS,MAAmBC,OAC5BA,EAAS,GAAEC,eACXA,IAEA,IACE,MAAMC,QAAwBJ,EAAUK,gBAExC,GAAID,EACF,GAAIA,EAAgBE,UAAW,CAC7B,MAAMC,EAAUC,KAAKC,MAAQL,EAAgBE,UAAYL,EACnDS,EAASN,EAAgBF,SAAWA,EACtCK,GAAWG,EACbV,EAAUW,eAEVC,EAAAA,QAAQb,EAAaK,EAAgBS,YAAaV,QAGpDH,EAAUW,eAGd,MAAOG,GAOPd,EAAUW,gBASPd,eAAekB,GAAuBhB,YAC3CA,EAAWC,UACXA,EAASE,OACTA,EAAS,GAAEc,iBACXA,IAEA,MAAMC,EAAiC,CACrCf,SACAI,UAAWE,KAAKC,MAChBI,YAAaK,EAAAA,UAAUnB,EAAaiB,UAGhChB,EAAUiB,cAAcA,GAOzB,SAASE,EACdC,GAEA,MAAMC,EAAwBD,EAAMrB,YACjCuB,gBACAC,WAAWC,IACN9B,EAAqB8B,EAAMC,OAC7BV,EAAuBK,MAIvBM,EAA2BN,EAAMrB,YACpC4B,mBACAJ,WAAWC,IACN9B,EAAqB8B,EAAMC,OAC7BV,EAAuBK,MAI7B,MAAO,KACLC,IACAK,0BAOG,SACLN,GAEA,IACIQ,EADAC,GAAkB,EAetB,MAAO,CAba,KAClBA,GAAkB,EAClBD,OAIqB9B,EAA0BsB,GAAOU,MAAK,KACtDD,IAEHD,EAAgCT,EAA4BC,sHCvKjB,EAAGhB,sBAClD,MAAM2B,EAAY,IAAI3B,EAAgBS,YAAYkB,WAC5CC,EAAU,IAAI5B,EAAgBS,YAAYmB,SAC1CC,EAA0B,IAC3B7B,EACHS,YAAa,CAAEkB,YAAWC,YAItBE,EAAgB,IAAIF,GAASG,MACjC,CAACC,EAAGC,IAAMD,EAAEE,MAAMC,cAAgBF,EAAEC,MAAMC,gBAI5C,GAAIL,EAAcM,OAAS,EAAG,CAC5B,MAAMC,EAAaP,EAAcQ,QAEjC,OADAT,EAAOpB,YAAYmB,QAAUA,EAAQW,QAAQC,GAAMA,IAAMH,IAClDR"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-persist-client-core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
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",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"src"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@tanstack/query-core": "
|
|
31
|
+
"@tanstack/query-core": "5.0.0-alpha.1"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"clean": "rimraf ./build",
|
package/src/__tests__/utils.ts
CHANGED
|
@@ -6,14 +6,7 @@ import type {
|
|
|
6
6
|
} from '@tanstack/query-persist-client-core'
|
|
7
7
|
|
|
8
8
|
export function createQueryClient(config?: QueryClientConfig): QueryClient {
|
|
9
|
-
|
|
10
|
-
return new QueryClient({ logger: mockLogger, ...config })
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const mockLogger = {
|
|
14
|
-
log: jest.fn(),
|
|
15
|
-
warn: jest.fn(),
|
|
16
|
-
error: jest.fn(),
|
|
9
|
+
return new QueryClient(config)
|
|
17
10
|
}
|
|
18
11
|
|
|
19
12
|
export function sleep(timeout: number): Promise<void> {
|
package/src/persist.ts
CHANGED
|
@@ -98,12 +98,10 @@ export async function persistQueryClientRestore({
|
|
|
98
98
|
}
|
|
99
99
|
} catch (err) {
|
|
100
100
|
if (process.env.NODE_ENV !== 'production') {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
.
|
|
104
|
-
|
|
105
|
-
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',
|
|
106
|
-
)
|
|
101
|
+
console.error(err)
|
|
102
|
+
console.warn(
|
|
103
|
+
'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',
|
|
104
|
+
)
|
|
107
105
|
}
|
|
108
106
|
persister.removeClient()
|
|
109
107
|
}
|