@tanstack/query-persist-client-core 4.7.0 → 4.7.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/index.d.ts +2 -58
- package/build/lib/index.esm.js +2 -101
- package/build/lib/index.esm.js.map +1 -1
- package/build/lib/index.js +7 -101
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +2 -101
- package/build/lib/index.mjs.map +1 -1
- package/build/lib/persist.d.ts +58 -0
- package/build/lib/persist.esm.js +102 -0
- package/build/lib/persist.esm.js.map +1 -0
- package/build/lib/persist.js +109 -0
- package/build/lib/persist.js.map +1 -0
- package/build/lib/persist.mjs +102 -0
- package/build/lib/persist.mjs.map +1 -0
- package/build/lib/retryStrategies.d.ts +7 -0
- package/build/lib/retryStrategies.esm.js +25 -0
- package/build/lib/retryStrategies.esm.js.map +1 -0
- package/build/lib/retryStrategies.js +29 -0
- package/build/lib/retryStrategies.js.map +1 -0
- package/build/lib/retryStrategies.mjs +25 -0
- package/build/lib/retryStrategies.mjs.map +1 -0
- package/build/umd/index.development.js +24 -0
- 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/index.ts +2 -164
- package/src/persist.ts +164 -0
- package/src/retryStrategies.ts +30 -0
package/build/lib/index.d.ts
CHANGED
|
@@ -1,58 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export interface Persister {
|
|
4
|
-
persistClient(persistClient: PersistedClient): Promisable<void>;
|
|
5
|
-
restoreClient(): Promisable<PersistedClient | undefined>;
|
|
6
|
-
removeClient(): Promisable<void>;
|
|
7
|
-
}
|
|
8
|
-
export interface PersistedClient {
|
|
9
|
-
timestamp: number;
|
|
10
|
-
buster: string;
|
|
11
|
-
clientState: DehydratedState;
|
|
12
|
-
}
|
|
13
|
-
export interface PersistQueryClienRootOptions {
|
|
14
|
-
/** The QueryClient to persist */
|
|
15
|
-
queryClient: QueryClient;
|
|
16
|
-
/** The Persister interface for storing and restoring the cache
|
|
17
|
-
* to/from a persisted location */
|
|
18
|
-
persister: Persister;
|
|
19
|
-
/** A unique string that can be used to forcefully
|
|
20
|
-
* invalidate existing caches if they do not share the same buster string */
|
|
21
|
-
buster?: string;
|
|
22
|
-
}
|
|
23
|
-
export interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOptions {
|
|
24
|
-
/** The max-allowed age of the cache in milliseconds.
|
|
25
|
-
* If a persisted cache is found that is older than this
|
|
26
|
-
* time, it will be discarded */
|
|
27
|
-
maxAge?: number;
|
|
28
|
-
/** The options passed to the hydrate function */
|
|
29
|
-
hydrateOptions?: HydrateOptions;
|
|
30
|
-
}
|
|
31
|
-
export interface PersistedQueryClientSaveOptions extends PersistQueryClienRootOptions {
|
|
32
|
-
/** The options passed to the dehydrate function */
|
|
33
|
-
dehydrateOptions?: DehydrateOptions;
|
|
34
|
-
}
|
|
35
|
-
export interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Restores persisted data to the QueryCache
|
|
39
|
-
* - data obtained from persister.restoreClient
|
|
40
|
-
* - data is hydrated using hydrateOptions
|
|
41
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
42
|
-
*/
|
|
43
|
-
export declare function persistQueryClientRestore({ queryClient, persister, maxAge, buster, hydrateOptions, }: PersistedQueryClientRestoreOptions): Promise<void>;
|
|
44
|
-
/**
|
|
45
|
-
* Persists data from the QueryCache
|
|
46
|
-
* - data dehydrated using dehydrateOptions
|
|
47
|
-
* - data is persisted using persister.persistClient
|
|
48
|
-
*/
|
|
49
|
-
export declare function persistQueryClientSave({ queryClient, persister, buster, dehydrateOptions, }: PersistedQueryClientSaveOptions): Promise<void>;
|
|
50
|
-
/**
|
|
51
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
52
|
-
* @returns an unsubscribe function (to discontinue monitoring)
|
|
53
|
-
*/
|
|
54
|
-
export declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
55
|
-
/**
|
|
56
|
-
* Restores persisted data to QueryCache and persists further changes.
|
|
57
|
-
*/
|
|
58
|
-
export declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
1
|
+
export * from './persist';
|
|
2
|
+
export * from './retryStrategies';
|
package/build/lib/index.esm.js
CHANGED
|
@@ -1,102 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Restores persisted data to the QueryCache
|
|
5
|
-
* - data obtained from persister.restoreClient
|
|
6
|
-
* - data is hydrated using hydrateOptions
|
|
7
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
8
|
-
*/
|
|
9
|
-
async function persistQueryClientRestore({
|
|
10
|
-
queryClient,
|
|
11
|
-
persister,
|
|
12
|
-
maxAge = 1000 * 60 * 60 * 24,
|
|
13
|
-
buster = '',
|
|
14
|
-
hydrateOptions
|
|
15
|
-
}) {
|
|
16
|
-
try {
|
|
17
|
-
const persistedClient = await persister.restoreClient();
|
|
18
|
-
|
|
19
|
-
if (persistedClient) {
|
|
20
|
-
if (persistedClient.timestamp) {
|
|
21
|
-
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
22
|
-
const busted = persistedClient.buster !== buster;
|
|
23
|
-
|
|
24
|
-
if (expired || busted) {
|
|
25
|
-
persister.removeClient();
|
|
26
|
-
} else {
|
|
27
|
-
hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
28
|
-
}
|
|
29
|
-
} else {
|
|
30
|
-
persister.removeClient();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
} catch (err) {
|
|
34
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
35
|
-
queryClient.getLogger().error(err);
|
|
36
|
-
queryClient.getLogger().warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
persister.removeClient();
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Persists data from the QueryCache
|
|
44
|
-
* - data dehydrated using dehydrateOptions
|
|
45
|
-
* - data is persisted using persister.persistClient
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
|
-
async function persistQueryClientSave({
|
|
49
|
-
queryClient,
|
|
50
|
-
persister,
|
|
51
|
-
buster = '',
|
|
52
|
-
dehydrateOptions
|
|
53
|
-
}) {
|
|
54
|
-
const persistClient = {
|
|
55
|
-
buster,
|
|
56
|
-
timestamp: Date.now(),
|
|
57
|
-
clientState: dehydrate(queryClient, dehydrateOptions)
|
|
58
|
-
};
|
|
59
|
-
await persister.persistClient(persistClient);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
63
|
-
* @returns an unsubscribe function (to discontinue monitoring)
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
function persistQueryClientSubscribe(props) {
|
|
67
|
-
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(() => {
|
|
68
|
-
persistQueryClientSave(props);
|
|
69
|
-
});
|
|
70
|
-
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(() => {
|
|
71
|
-
persistQueryClientSave(props);
|
|
72
|
-
});
|
|
73
|
-
return () => {
|
|
74
|
-
unsubscribeQueryCache();
|
|
75
|
-
unusbscribeMutationCache();
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Restores persisted data to QueryCache and persists further changes.
|
|
80
|
-
*/
|
|
81
|
-
|
|
82
|
-
function persistQueryClient(props) {
|
|
83
|
-
let hasUnsubscribed = false;
|
|
84
|
-
let persistQueryClientUnsubscribe;
|
|
85
|
-
|
|
86
|
-
const unsubscribe = () => {
|
|
87
|
-
hasUnsubscribed = true;
|
|
88
|
-
persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
|
|
89
|
-
}; // Attempt restore
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
93
|
-
if (!hasUnsubscribed) {
|
|
94
|
-
// Subscribe to changes in the query cache to trigger the save
|
|
95
|
-
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
return [unsubscribe, restorePromise];
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
1
|
+
export { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.esm.js';
|
|
2
|
+
export { removeOldestQuery } from './retryStrategies.esm.js';
|
|
102
3
|
//# sourceMappingURL=index.esm.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":[
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
package/build/lib/index.js
CHANGED
|
@@ -2,108 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
5
|
+
var persist = require('./persist.js');
|
|
6
|
+
var retryStrategies = require('./retryStrategies.js');
|
|
6
7
|
|
|
7
|
-
/**
|
|
8
|
-
* Restores persisted data to the QueryCache
|
|
9
|
-
* - data obtained from persister.restoreClient
|
|
10
|
-
* - data is hydrated using hydrateOptions
|
|
11
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
12
|
-
*/
|
|
13
|
-
async function persistQueryClientRestore({
|
|
14
|
-
queryClient,
|
|
15
|
-
persister,
|
|
16
|
-
maxAge = 1000 * 60 * 60 * 24,
|
|
17
|
-
buster = '',
|
|
18
|
-
hydrateOptions
|
|
19
|
-
}) {
|
|
20
|
-
try {
|
|
21
|
-
const persistedClient = await persister.restoreClient();
|
|
22
8
|
|
|
23
|
-
if (persistedClient) {
|
|
24
|
-
if (persistedClient.timestamp) {
|
|
25
|
-
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
26
|
-
const busted = persistedClient.buster !== buster;
|
|
27
9
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
} else {
|
|
34
|
-
persister.removeClient();
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
} catch (err) {
|
|
38
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
39
|
-
queryClient.getLogger().error(err);
|
|
40
|
-
queryClient.getLogger().warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
persister.removeClient();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Persists data from the QueryCache
|
|
48
|
-
* - data dehydrated using dehydrateOptions
|
|
49
|
-
* - data is persisted using persister.persistClient
|
|
50
|
-
*/
|
|
51
|
-
|
|
52
|
-
async function persistQueryClientSave({
|
|
53
|
-
queryClient,
|
|
54
|
-
persister,
|
|
55
|
-
buster = '',
|
|
56
|
-
dehydrateOptions
|
|
57
|
-
}) {
|
|
58
|
-
const persistClient = {
|
|
59
|
-
buster,
|
|
60
|
-
timestamp: Date.now(),
|
|
61
|
-
clientState: queryCore.dehydrate(queryClient, dehydrateOptions)
|
|
62
|
-
};
|
|
63
|
-
await persister.persistClient(persistClient);
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
67
|
-
* @returns an unsubscribe function (to discontinue monitoring)
|
|
68
|
-
*/
|
|
69
|
-
|
|
70
|
-
function persistQueryClientSubscribe(props) {
|
|
71
|
-
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(() => {
|
|
72
|
-
persistQueryClientSave(props);
|
|
73
|
-
});
|
|
74
|
-
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(() => {
|
|
75
|
-
persistQueryClientSave(props);
|
|
76
|
-
});
|
|
77
|
-
return () => {
|
|
78
|
-
unsubscribeQueryCache();
|
|
79
|
-
unusbscribeMutationCache();
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Restores persisted data to QueryCache and persists further changes.
|
|
84
|
-
*/
|
|
85
|
-
|
|
86
|
-
function persistQueryClient(props) {
|
|
87
|
-
let hasUnsubscribed = false;
|
|
88
|
-
let persistQueryClientUnsubscribe;
|
|
89
|
-
|
|
90
|
-
const unsubscribe = () => {
|
|
91
|
-
hasUnsubscribed = true;
|
|
92
|
-
persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
|
|
93
|
-
}; // Attempt restore
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
97
|
-
if (!hasUnsubscribed) {
|
|
98
|
-
// Subscribe to changes in the query cache to trigger the save
|
|
99
|
-
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
return [unsubscribe, restorePromise];
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
exports.persistQueryClient = persistQueryClient;
|
|
106
|
-
exports.persistQueryClientRestore = persistQueryClientRestore;
|
|
107
|
-
exports.persistQueryClientSave = persistQueryClientSave;
|
|
108
|
-
exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
|
|
10
|
+
exports.persistQueryClient = persist.persistQueryClient;
|
|
11
|
+
exports.persistQueryClientRestore = persist.persistQueryClientRestore;
|
|
12
|
+
exports.persistQueryClientSave = persist.persistQueryClientSave;
|
|
13
|
+
exports.persistQueryClientSubscribe = persist.persistQueryClientSubscribe;
|
|
14
|
+
exports.removeOldestQuery = retryStrategies.removeOldestQuery;
|
|
109
15
|
//# sourceMappingURL=index.js.map
|
package/build/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;"}
|
package/build/lib/index.mjs
CHANGED
|
@@ -1,102 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Restores persisted data to the QueryCache
|
|
5
|
-
* - data obtained from persister.restoreClient
|
|
6
|
-
* - data is hydrated using hydrateOptions
|
|
7
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
8
|
-
*/
|
|
9
|
-
async function persistQueryClientRestore({
|
|
10
|
-
queryClient,
|
|
11
|
-
persister,
|
|
12
|
-
maxAge = 1000 * 60 * 60 * 24,
|
|
13
|
-
buster = '',
|
|
14
|
-
hydrateOptions
|
|
15
|
-
}) {
|
|
16
|
-
try {
|
|
17
|
-
const persistedClient = await persister.restoreClient();
|
|
18
|
-
|
|
19
|
-
if (persistedClient) {
|
|
20
|
-
if (persistedClient.timestamp) {
|
|
21
|
-
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
22
|
-
const busted = persistedClient.buster !== buster;
|
|
23
|
-
|
|
24
|
-
if (expired || busted) {
|
|
25
|
-
persister.removeClient();
|
|
26
|
-
} else {
|
|
27
|
-
hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
28
|
-
}
|
|
29
|
-
} else {
|
|
30
|
-
persister.removeClient();
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
} catch (err) {
|
|
34
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
35
|
-
queryClient.getLogger().error(err);
|
|
36
|
-
queryClient.getLogger().warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
persister.removeClient();
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Persists data from the QueryCache
|
|
44
|
-
* - data dehydrated using dehydrateOptions
|
|
45
|
-
* - data is persisted using persister.persistClient
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
|
-
async function persistQueryClientSave({
|
|
49
|
-
queryClient,
|
|
50
|
-
persister,
|
|
51
|
-
buster = '',
|
|
52
|
-
dehydrateOptions
|
|
53
|
-
}) {
|
|
54
|
-
const persistClient = {
|
|
55
|
-
buster,
|
|
56
|
-
timestamp: Date.now(),
|
|
57
|
-
clientState: dehydrate(queryClient, dehydrateOptions)
|
|
58
|
-
};
|
|
59
|
-
await persister.persistClient(persistClient);
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
63
|
-
* @returns an unsubscribe function (to discontinue monitoring)
|
|
64
|
-
*/
|
|
65
|
-
|
|
66
|
-
function persistQueryClientSubscribe(props) {
|
|
67
|
-
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(() => {
|
|
68
|
-
persistQueryClientSave(props);
|
|
69
|
-
});
|
|
70
|
-
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(() => {
|
|
71
|
-
persistQueryClientSave(props);
|
|
72
|
-
});
|
|
73
|
-
return () => {
|
|
74
|
-
unsubscribeQueryCache();
|
|
75
|
-
unusbscribeMutationCache();
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Restores persisted data to QueryCache and persists further changes.
|
|
80
|
-
*/
|
|
81
|
-
|
|
82
|
-
function persistQueryClient(props) {
|
|
83
|
-
let hasUnsubscribed = false;
|
|
84
|
-
let persistQueryClientUnsubscribe;
|
|
85
|
-
|
|
86
|
-
const unsubscribe = () => {
|
|
87
|
-
hasUnsubscribed = true;
|
|
88
|
-
persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
|
|
89
|
-
}; // Attempt restore
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
93
|
-
if (!hasUnsubscribed) {
|
|
94
|
-
// Subscribe to changes in the query cache to trigger the save
|
|
95
|
-
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
return [unsubscribe, restorePromise];
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
export { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
1
|
+
export { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.mjs';
|
|
2
|
+
export { removeOldestQuery } from './retryStrategies.mjs';
|
|
102
3
|
//# sourceMappingURL=index.mjs.map
|
package/build/lib/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":[
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { QueryClient, DehydratedState, DehydrateOptions, HydrateOptions } from '@tanstack/query-core';
|
|
2
|
+
export declare type Promisable<T> = T | PromiseLike<T>;
|
|
3
|
+
export interface Persister {
|
|
4
|
+
persistClient(persistClient: PersistedClient): Promisable<void>;
|
|
5
|
+
restoreClient(): Promisable<PersistedClient | undefined>;
|
|
6
|
+
removeClient(): Promisable<void>;
|
|
7
|
+
}
|
|
8
|
+
export interface PersistedClient {
|
|
9
|
+
timestamp: number;
|
|
10
|
+
buster: string;
|
|
11
|
+
clientState: DehydratedState;
|
|
12
|
+
}
|
|
13
|
+
export interface PersistQueryClienRootOptions {
|
|
14
|
+
/** The QueryClient to persist */
|
|
15
|
+
queryClient: QueryClient;
|
|
16
|
+
/** The Persister interface for storing and restoring the cache
|
|
17
|
+
* to/from a persisted location */
|
|
18
|
+
persister: Persister;
|
|
19
|
+
/** A unique string that can be used to forcefully
|
|
20
|
+
* invalidate existing caches if they do not share the same buster string */
|
|
21
|
+
buster?: string;
|
|
22
|
+
}
|
|
23
|
+
export interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOptions {
|
|
24
|
+
/** The max-allowed age of the cache in milliseconds.
|
|
25
|
+
* If a persisted cache is found that is older than this
|
|
26
|
+
* time, it will be discarded */
|
|
27
|
+
maxAge?: number;
|
|
28
|
+
/** The options passed to the hydrate function */
|
|
29
|
+
hydrateOptions?: HydrateOptions;
|
|
30
|
+
}
|
|
31
|
+
export interface PersistedQueryClientSaveOptions extends PersistQueryClienRootOptions {
|
|
32
|
+
/** The options passed to the dehydrate function */
|
|
33
|
+
dehydrateOptions?: DehydrateOptions;
|
|
34
|
+
}
|
|
35
|
+
export interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Restores persisted data to the QueryCache
|
|
39
|
+
* - data obtained from persister.restoreClient
|
|
40
|
+
* - data is hydrated using hydrateOptions
|
|
41
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
42
|
+
*/
|
|
43
|
+
export declare function persistQueryClientRestore({ queryClient, persister, maxAge, buster, hydrateOptions, }: PersistedQueryClientRestoreOptions): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Persists data from the QueryCache
|
|
46
|
+
* - data dehydrated using dehydrateOptions
|
|
47
|
+
* - data is persisted using persister.persistClient
|
|
48
|
+
*/
|
|
49
|
+
export declare function persistQueryClientSave({ queryClient, persister, buster, dehydrateOptions, }: PersistedQueryClientSaveOptions): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
52
|
+
* @returns an unsubscribe function (to discontinue monitoring)
|
|
53
|
+
*/
|
|
54
|
+
export declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
55
|
+
/**
|
|
56
|
+
* Restores persisted data to QueryCache and persists further changes.
|
|
57
|
+
*/
|
|
58
|
+
export declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { hydrate, dehydrate } from '@tanstack/query-core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Restores persisted data to the QueryCache
|
|
5
|
+
* - data obtained from persister.restoreClient
|
|
6
|
+
* - data is hydrated using hydrateOptions
|
|
7
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
8
|
+
*/
|
|
9
|
+
async function persistQueryClientRestore({
|
|
10
|
+
queryClient,
|
|
11
|
+
persister,
|
|
12
|
+
maxAge = 1000 * 60 * 60 * 24,
|
|
13
|
+
buster = '',
|
|
14
|
+
hydrateOptions
|
|
15
|
+
}) {
|
|
16
|
+
try {
|
|
17
|
+
const persistedClient = await persister.restoreClient();
|
|
18
|
+
|
|
19
|
+
if (persistedClient) {
|
|
20
|
+
if (persistedClient.timestamp) {
|
|
21
|
+
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
22
|
+
const busted = persistedClient.buster !== buster;
|
|
23
|
+
|
|
24
|
+
if (expired || busted) {
|
|
25
|
+
persister.removeClient();
|
|
26
|
+
} else {
|
|
27
|
+
hydrate(queryClient, persistedClient.clientState, hydrateOptions);
|
|
28
|
+
}
|
|
29
|
+
} else {
|
|
30
|
+
persister.removeClient();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
} catch (err) {
|
|
34
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
35
|
+
queryClient.getLogger().error(err);
|
|
36
|
+
queryClient.getLogger().warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
persister.removeClient();
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Persists data from the QueryCache
|
|
44
|
+
* - data dehydrated using dehydrateOptions
|
|
45
|
+
* - data is persisted using persister.persistClient
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
async function persistQueryClientSave({
|
|
49
|
+
queryClient,
|
|
50
|
+
persister,
|
|
51
|
+
buster = '',
|
|
52
|
+
dehydrateOptions
|
|
53
|
+
}) {
|
|
54
|
+
const persistClient = {
|
|
55
|
+
buster,
|
|
56
|
+
timestamp: Date.now(),
|
|
57
|
+
clientState: dehydrate(queryClient, dehydrateOptions)
|
|
58
|
+
};
|
|
59
|
+
await persister.persistClient(persistClient);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
63
|
+
* @returns an unsubscribe function (to discontinue monitoring)
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
function persistQueryClientSubscribe(props) {
|
|
67
|
+
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(() => {
|
|
68
|
+
persistQueryClientSave(props);
|
|
69
|
+
});
|
|
70
|
+
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(() => {
|
|
71
|
+
persistQueryClientSave(props);
|
|
72
|
+
});
|
|
73
|
+
return () => {
|
|
74
|
+
unsubscribeQueryCache();
|
|
75
|
+
unusbscribeMutationCache();
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Restores persisted data to QueryCache and persists further changes.
|
|
80
|
+
*/
|
|
81
|
+
|
|
82
|
+
function persistQueryClient(props) {
|
|
83
|
+
let hasUnsubscribed = false;
|
|
84
|
+
let persistQueryClientUnsubscribe;
|
|
85
|
+
|
|
86
|
+
const unsubscribe = () => {
|
|
87
|
+
hasUnsubscribed = true;
|
|
88
|
+
persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
|
|
89
|
+
}; // Attempt restore
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
93
|
+
if (!hasUnsubscribed) {
|
|
94
|
+
// Subscribe to changes in the query cache to trigger the save
|
|
95
|
+
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
return [unsubscribe, restorePromise];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
102
|
+
//# sourceMappingURL=persist.esm.js.map
|
|
@@ -0,0 +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'\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 * 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(() => {\n persistQueryClientSave(props)\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe(() => {\n persistQueryClientSave(props)\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":["persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","getLogger","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":"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"}
|