@tanstack/query-persist-client-core 5.101.4 → 5.102.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/legacy/createPersister.cjs +172 -256
- package/build/legacy/createPersister.cjs.map +1 -1
- package/build/legacy/createPersister.d.cts +88 -6
- package/build/legacy/createPersister.d.cts.map +1 -0
- package/build/legacy/createPersister.d.ts +88 -6
- package/build/legacy/createPersister.d.ts.map +1 -0
- package/build/legacy/createPersister.js +170 -235
- package/build/legacy/createPersister.js.map +1 -1
- package/build/legacy/index.cjs +14 -27
- package/build/legacy/index.cjs.map +1 -1
- package/build/legacy/index.d.cts +4 -19
- package/build/legacy/index.d.ts +4 -19
- package/build/legacy/index.js +8 -4
- package/build/legacy/index.js.map +1 -1
- package/build/legacy/persist.cjs +82 -108
- package/build/legacy/persist.cjs.map +1 -1
- package/build/legacy/persist.d.cts +61 -11
- package/build/legacy/persist.d.cts.map +1 -0
- package/build/legacy/persist.d.ts +61 -11
- package/build/legacy/persist.d.ts.map +1 -0
- package/build/legacy/persist.js +77 -79
- package/build/legacy/persist.js.map +1 -1
- package/build/legacy/retryStrategies.cjs +20 -44
- package/build/legacy/retryStrategies.cjs.map +1 -1
- package/build/legacy/retryStrategies.d.cts +11 -2
- package/build/legacy/retryStrategies.d.cts.map +1 -0
- package/build/legacy/retryStrategies.d.ts +11 -2
- package/build/legacy/retryStrategies.d.ts.map +1 -0
- package/build/legacy/retryStrategies.js +20 -20
- package/build/legacy/retryStrategies.js.map +1 -1
- package/build/modern/createPersister.cjs +172 -256
- package/build/modern/createPersister.cjs.map +1 -1
- package/build/modern/createPersister.d.cts +88 -6
- package/build/modern/createPersister.d.cts.map +1 -0
- package/build/modern/createPersister.d.ts +88 -6
- package/build/modern/createPersister.d.ts.map +1 -0
- package/build/modern/createPersister.js +170 -235
- package/build/modern/createPersister.js.map +1 -1
- package/build/modern/index.cjs +14 -27
- package/build/modern/index.cjs.map +1 -1
- package/build/modern/index.d.cts +4 -19
- package/build/modern/index.d.ts +4 -19
- package/build/modern/index.js +8 -4
- package/build/modern/index.js.map +1 -1
- package/build/modern/persist.cjs +82 -108
- package/build/modern/persist.cjs.map +1 -1
- package/build/modern/persist.d.cts +61 -11
- package/build/modern/persist.d.cts.map +1 -0
- package/build/modern/persist.d.ts +61 -11
- package/build/modern/persist.d.ts.map +1 -0
- package/build/modern/persist.js +77 -79
- package/build/modern/persist.js.map +1 -1
- package/build/modern/retryStrategies.cjs +20 -44
- package/build/modern/retryStrategies.cjs.map +1 -1
- package/build/modern/retryStrategies.d.cts +11 -2
- package/build/modern/retryStrategies.d.cts.map +1 -0
- package/build/modern/retryStrategies.d.ts +11 -2
- package/build/modern/retryStrategies.d.ts.map +1 -0
- package/build/modern/retryStrategies.js +20 -20
- package/build/modern/retryStrategies.js.map +1 -1
- package/package.json +4 -6
- package/build/legacy/_tsup-dts-rollup.d.cts +0 -214
- package/build/legacy/_tsup-dts-rollup.d.ts +0 -214
- package/build/modern/_tsup-dts-rollup.d.cts +0 -214
- package/build/modern/_tsup-dts-rollup.d.ts +0 -214
package/build/legacy/index.d.cts
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export { persistQueryClient
|
|
5
|
-
export { Promisable } from './_tsup-dts-rollup.cjs';
|
|
6
|
-
export { Persister } from './_tsup-dts-rollup.cjs';
|
|
7
|
-
export { PersistedClient } from './_tsup-dts-rollup.cjs';
|
|
8
|
-
export { PersistQueryClientRootOptions } from './_tsup-dts-rollup.cjs';
|
|
9
|
-
export { PersistedQueryClientRestoreOptions } from './_tsup-dts-rollup.cjs';
|
|
10
|
-
export { PersistedQueryClientSaveOptions } from './_tsup-dts-rollup.cjs';
|
|
11
|
-
export { PersistQueryClientOptions } from './_tsup-dts-rollup.cjs';
|
|
12
|
-
export { PersistRetryer } from './_tsup-dts-rollup.cjs';
|
|
13
|
-
export { removeOldestQuery } from './_tsup-dts-rollup.cjs';
|
|
14
|
-
export { experimental_createQueryPersister_alias_1 as experimental_createQueryPersister } from './_tsup-dts-rollup.cjs';
|
|
15
|
-
export { PersistedQuery_alias_1 as PersistedQuery } from './_tsup-dts-rollup.cjs';
|
|
16
|
-
export { MaybePromise_alias_1 as MaybePromise } from './_tsup-dts-rollup.cjs';
|
|
17
|
-
export { AsyncStorage_alias_1 as AsyncStorage } from './_tsup-dts-rollup.cjs';
|
|
18
|
-
export { StoragePersisterOptions_alias_1 as StoragePersisterOptions } from './_tsup-dts-rollup.cjs';
|
|
19
|
-
export { PERSISTER_KEY_PREFIX_alias_1 as PERSISTER_KEY_PREFIX } from './_tsup-dts-rollup.cjs';
|
|
1
|
+
import { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createQueryPersister } from "./createPersister.cjs";
|
|
2
|
+
import { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from "./persist.cjs";
|
|
3
|
+
import { PersistRetryer, removeOldestQuery } from "./retryStrategies.cjs";
|
|
4
|
+
export { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistQueryClientOptions, PersistQueryClientRootOptions, PersistRetryer, PersistedClient, PersistedQuery, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, StoragePersisterOptions, experimental_createQueryPersister, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe, removeOldestQuery };
|
package/build/legacy/index.d.ts
CHANGED
|
@@ -1,19 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export { persistQueryClient
|
|
5
|
-
export { Promisable } from './_tsup-dts-rollup.js';
|
|
6
|
-
export { Persister } from './_tsup-dts-rollup.js';
|
|
7
|
-
export { PersistedClient } from './_tsup-dts-rollup.js';
|
|
8
|
-
export { PersistQueryClientRootOptions } from './_tsup-dts-rollup.js';
|
|
9
|
-
export { PersistedQueryClientRestoreOptions } from './_tsup-dts-rollup.js';
|
|
10
|
-
export { PersistedQueryClientSaveOptions } from './_tsup-dts-rollup.js';
|
|
11
|
-
export { PersistQueryClientOptions } from './_tsup-dts-rollup.js';
|
|
12
|
-
export { PersistRetryer } from './_tsup-dts-rollup.js';
|
|
13
|
-
export { removeOldestQuery } from './_tsup-dts-rollup.js';
|
|
14
|
-
export { experimental_createQueryPersister_alias_1 as experimental_createQueryPersister } from './_tsup-dts-rollup.js';
|
|
15
|
-
export { PersistedQuery_alias_1 as PersistedQuery } from './_tsup-dts-rollup.js';
|
|
16
|
-
export { MaybePromise_alias_1 as MaybePromise } from './_tsup-dts-rollup.js';
|
|
17
|
-
export { AsyncStorage_alias_1 as AsyncStorage } from './_tsup-dts-rollup.js';
|
|
18
|
-
export { StoragePersisterOptions_alias_1 as StoragePersisterOptions } from './_tsup-dts-rollup.js';
|
|
19
|
-
export { PERSISTER_KEY_PREFIX_alias_1 as PERSISTER_KEY_PREFIX } from './_tsup-dts-rollup.js';
|
|
1
|
+
import { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createQueryPersister } from "./createPersister.js";
|
|
2
|
+
import { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from "./persist.js";
|
|
3
|
+
import { PersistRetryer, removeOldestQuery } from "./retryStrategies.js";
|
|
4
|
+
export { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistQueryClientOptions, PersistQueryClientRootOptions, PersistRetryer, PersistedClient, PersistedQuery, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, StoragePersisterOptions, experimental_createQueryPersister, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe, removeOldestQuery };
|
package/build/legacy/index.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { PERSISTER_KEY_PREFIX, experimental_createQueryPersister } from "./createPersister.js";
|
|
2
|
+
import { persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from "./persist.js";
|
|
3
|
+
import { removeOldestQuery } from "./retryStrategies.js";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
/* istanbul ignore file */
|
|
6
|
+
//#endregion
|
|
7
|
+
export { PERSISTER_KEY_PREFIX, experimental_createQueryPersister, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe, removeOldestQuery };
|
|
8
|
+
|
|
5
9
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\nexport * from './createPersister'\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\nexport * from './createPersister'\n"],"mappings":""}
|
package/build/legacy/persist.cjs
CHANGED
|
@@ -1,117 +1,91 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/persist.ts
|
|
21
|
-
var persist_exports = {};
|
|
22
|
-
__export(persist_exports, {
|
|
23
|
-
persistQueryClient: () => persistQueryClient,
|
|
24
|
-
persistQueryClientRestore: () => persistQueryClientRestore,
|
|
25
|
-
persistQueryClientSave: () => persistQueryClientSave,
|
|
26
|
-
persistQueryClientSubscribe: () => persistQueryClientSubscribe
|
|
27
|
-
});
|
|
28
|
-
module.exports = __toCommonJS(persist_exports);
|
|
29
|
-
var import_query_core = require("@tanstack/query-core");
|
|
30
|
-
var cacheEventTypes = ["added", "removed", "updated"];
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _tanstack_query_core = require("@tanstack/query-core");
|
|
3
|
+
//#region src/persist.ts
|
|
4
|
+
/**
|
|
5
|
+
* Checks if emitted event is about cache change and not about observers.
|
|
6
|
+
* Useful for persist, where we only want to trigger save when cache is changed.
|
|
7
|
+
*/
|
|
8
|
+
const cacheEventTypes = [
|
|
9
|
+
"added",
|
|
10
|
+
"removed",
|
|
11
|
+
"updated"
|
|
12
|
+
];
|
|
31
13
|
function isCacheEventType(eventType) {
|
|
32
|
-
|
|
14
|
+
return cacheEventTypes.includes(eventType);
|
|
33
15
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
persister
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
console.warn(
|
|
60
|
-
"Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded."
|
|
61
|
-
);
|
|
62
|
-
}
|
|
63
|
-
await persister.removeClient();
|
|
64
|
-
throw err;
|
|
65
|
-
}
|
|
16
|
+
/**
|
|
17
|
+
* Restores persisted data to the QueryCache
|
|
18
|
+
* - data obtained from persister.restoreClient
|
|
19
|
+
* - data is hydrated using hydrateOptions
|
|
20
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
21
|
+
*/
|
|
22
|
+
async function persistQueryClientRestore({ queryClient, persister, maxAge = 864e5, buster = "", hydrateOptions }) {
|
|
23
|
+
try {
|
|
24
|
+
const persistedClient = await persister.restoreClient();
|
|
25
|
+
if (persistedClient) {
|
|
26
|
+
if (persistedClient.timestamp) {
|
|
27
|
+
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
28
|
+
const busted = persistedClient.buster !== buster;
|
|
29
|
+
if (expired || busted) return persister.removeClient();
|
|
30
|
+
else (0, _tanstack_query_core.hydrate)(queryClient, persistedClient.clientState, hydrateOptions);
|
|
31
|
+
} else return persister.removeClient();
|
|
32
|
+
}
|
|
33
|
+
} catch (err) {
|
|
34
|
+
if (process.env.NODE_ENV !== "production") {
|
|
35
|
+
console.error(err);
|
|
36
|
+
console.warn("Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.");
|
|
37
|
+
}
|
|
38
|
+
await persister.removeClient();
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
66
41
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
}) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Persists data from the QueryCache
|
|
44
|
+
* - data dehydrated using dehydrateOptions
|
|
45
|
+
* - data is persisted using persister.persistClient
|
|
46
|
+
*/
|
|
47
|
+
async function persistQueryClientSave({ queryClient, persister, buster = "", dehydrateOptions }) {
|
|
48
|
+
const persistClient = {
|
|
49
|
+
buster,
|
|
50
|
+
timestamp: Date.now(),
|
|
51
|
+
clientState: (0, _tanstack_query_core.dehydrate)(queryClient, dehydrateOptions)
|
|
52
|
+
};
|
|
53
|
+
await persister.persistClient(persistClient);
|
|
79
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
57
|
+
* @returns an unsubscribe function (to discontinue monitoring)
|
|
58
|
+
*/
|
|
80
59
|
function persistQueryClientSubscribe(props) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return () => {
|
|
92
|
-
unsubscribeQueryCache();
|
|
93
|
-
unsubscribeMutationCache();
|
|
94
|
-
};
|
|
60
|
+
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe((event) => {
|
|
61
|
+
if (isCacheEventType(event.type)) persistQueryClientSave(props);
|
|
62
|
+
});
|
|
63
|
+
const unsubscribeMutationCache = props.queryClient.getMutationCache().subscribe((event) => {
|
|
64
|
+
if (isCacheEventType(event.type)) persistQueryClientSave(props);
|
|
65
|
+
});
|
|
66
|
+
return () => {
|
|
67
|
+
unsubscribeQueryCache();
|
|
68
|
+
unsubscribeMutationCache();
|
|
69
|
+
};
|
|
95
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Restores persisted data to QueryCache and persists further changes.
|
|
73
|
+
*/
|
|
96
74
|
function persistQueryClient(props) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
return [unsubscribe, restorePromise];
|
|
75
|
+
let hasUnsubscribed = false;
|
|
76
|
+
let persistQueryClientUnsubscribe;
|
|
77
|
+
const unsubscribe = () => {
|
|
78
|
+
hasUnsubscribed = true;
|
|
79
|
+
persistQueryClientUnsubscribe === null || persistQueryClientUnsubscribe === void 0 || persistQueryClientUnsubscribe();
|
|
80
|
+
};
|
|
81
|
+
return [unsubscribe, persistQueryClientRestore(props).then(() => {
|
|
82
|
+
if (!hasUnsubscribed) persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
83
|
+
})];
|
|
109
84
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
});
|
|
85
|
+
//#endregion
|
|
86
|
+
exports.persistQueryClient = persistQueryClient;
|
|
87
|
+
exports.persistQueryClientRestore = persistQueryClientRestore;
|
|
88
|
+
exports.persistQueryClientSave = persistQueryClientSave;
|
|
89
|
+
exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
|
|
90
|
+
|
|
117
91
|
//# sourceMappingURL=persist.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/persist.ts"],"sourcesContent":["import { dehydrate, hydrate } from '@tanstack/query-core'\nimport type {\n DehydrateOptions,\n DehydratedState,\n HydrateOptions,\n NotifyEventType,\n QueryClient,\n} from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient: (persistClient: PersistedClient) => Promisable<void>\n restoreClient: () => Promisable<PersistedClient | undefined>\n removeClient: () => Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClientRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** 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 extends PersistQueryClientRootOptions {\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 extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends\n PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClientRootOptions {}\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 cacheEventTypes: Array<NotifyEventType> = ['added', 'removed', 'updated']\n\nfunction isCacheEventType(eventType: NotifyEventType) {\n return cacheEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n return persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n return persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n\n await persister.removeClient()\n\n throw err\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\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 (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unsubscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unsubscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"persist.cjs","names":["dehydrate"],"sources":["../../src/persist.ts"],"sourcesContent":["import { dehydrate, hydrate } from '@tanstack/query-core'\nimport type {\n DehydrateOptions,\n DehydratedState,\n HydrateOptions,\n NotifyEventType,\n QueryClient,\n} from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient: (persistClient: PersistedClient) => Promisable<void>\n restoreClient: () => Promisable<PersistedClient | undefined>\n removeClient: () => Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClientRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** 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 extends PersistQueryClientRootOptions {\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 extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends\n PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClientRootOptions {}\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 cacheEventTypes: Array<NotifyEventType> = ['added', 'removed', 'updated']\n\nfunction isCacheEventType(eventType: NotifyEventType) {\n return cacheEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n return persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n return persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n\n await persister.removeClient()\n\n throw err\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\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 (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unsubscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unsubscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"mappings":";;;;;;;AA0DA,MAAM,kBAA0C;CAAC;CAAS;CAAW;AAAS;AAE9E,SAAS,iBAAiB,WAA4B;CACpD,OAAO,gBAAgB,SAAS,SAAS;AAC3C;;;;;;;AAQA,eAAsB,0BAA0B,EAC9C,aACA,WACA,SAAS,OACT,SAAS,IACT,kBACqC;CACrC,IAAI;EACF,MAAM,kBAAkB,MAAM,UAAU,cAAc;EAEtD,IAAI,iBAAiB;GACnB,IAAI,gBAAgB,WAAW;IAC7B,MAAM,UAAU,KAAK,IAAI,IAAI,gBAAgB,YAAY;IACzD,MAAM,SAAS,gBAAgB,WAAW;IAC1C,IAAI,WAAW,QACb,OAAO,UAAU,aAAa;SAE9B,CAAA,GAAA,qBAAA,QAAA,CAAQ,aAAa,gBAAgB,aAAa,cAAc;GAEpE,OACE,OAAO,UAAU,aAAa;EAElC;CACF,SAAS,KAAK;EACZ,IAAI,QAAQ,IAAI,aAAa,cAAc;GACzC,QAAQ,MAAM,GAAG;GACjB,QAAQ,KACN,0IACF;EACF;EAEA,MAAM,UAAU,aAAa;EAE7B,MAAM;CACR;AACF;;;;;;AAOA,eAAsB,uBAAuB,EAC3C,aACA,WACA,SAAS,IACT,oBACkC;CAClC,MAAM,gBAAiC;EACrC;EACA,WAAW,KAAK,IAAI;EACpB,cAAA,GAAaA,qBAAAA,UAAAA,CAAU,aAAa,gBAAgB;CACtD;CAEA,MAAM,UAAU,cAAc,aAAa;AAC7C;;;;;AAMA,SAAgB,4BACd,OACA;CACA,MAAM,wBAAwB,MAAM,YACjC,cAAc,CAAC,CACf,WAAW,UAAU;EACpB,IAAI,iBAAiB,MAAM,IAAI,GAC7B,uBAAuB,KAAK;CAEhC,CAAC;CAEH,MAAM,2BAA2B,MAAM,YACpC,iBAAiB,CAAC,CAClB,WAAW,UAAU;EACpB,IAAI,iBAAiB,MAAM,IAAI,GAC7B,uBAAuB,KAAK;CAEhC,CAAC;CAEH,aAAa;EACX,sBAAsB;EACtB,yBAAyB;CAC3B;AACF;;;;AAKA,SAAgB,mBACd,OAC6B;CAC7B,IAAI,kBAAkB;CACtB,IAAI;CACJ,MAAM,oBAAoB;EACxB,kBAAkB;EAClB,kCAAA,QAAA,kCAAA,KAAA,KAAA,8BAAgC;CAClC;CAUA,OAAO,CAAC,aAPe,0BAA0B,KAAK,CAAC,CAAC,WAAW;EACjE,IAAI,CAAC,iBAEH,gCAAgC,4BAA4B,KAAK;CAErE,CAEkC,CAAC;AACrC"}
|
|
@@ -1,11 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { DehydrateOptions, DehydratedState, HydrateOptions, QueryClient } from "@tanstack/query-core";
|
|
2
|
+
//#region src/persist.d.ts
|
|
3
|
+
type Promisable<T> = T | PromiseLike<T>;
|
|
4
|
+
interface Persister {
|
|
5
|
+
persistClient: (persistClient: PersistedClient) => Promisable<void>;
|
|
6
|
+
restoreClient: () => Promisable<PersistedClient | undefined>;
|
|
7
|
+
removeClient: () => Promisable<void>;
|
|
8
|
+
}
|
|
9
|
+
interface PersistedClient {
|
|
10
|
+
timestamp: number;
|
|
11
|
+
buster: string;
|
|
12
|
+
clientState: DehydratedState;
|
|
13
|
+
}
|
|
14
|
+
interface PersistQueryClientRootOptions {
|
|
15
|
+
/** The QueryClient to persist */
|
|
16
|
+
queryClient: QueryClient;
|
|
17
|
+
/** The Persister interface for storing and restoring the cache
|
|
18
|
+
* to/from a persisted location */
|
|
19
|
+
persister: Persister;
|
|
20
|
+
/** A unique string that can be used to forcefully
|
|
21
|
+
* invalidate existing caches if they do not share the same buster string */
|
|
22
|
+
buster?: string;
|
|
23
|
+
}
|
|
24
|
+
interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
|
|
25
|
+
/** The max-allowed age of the cache in milliseconds.
|
|
26
|
+
* If a persisted cache is found that is older than this
|
|
27
|
+
* time, it will be discarded */
|
|
28
|
+
maxAge?: number;
|
|
29
|
+
/** The options passed to the hydrate function */
|
|
30
|
+
hydrateOptions?: HydrateOptions;
|
|
31
|
+
}
|
|
32
|
+
interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
|
|
33
|
+
/** The options passed to the dehydrate function */
|
|
34
|
+
dehydrateOptions?: DehydrateOptions;
|
|
35
|
+
}
|
|
36
|
+
interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {}
|
|
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
|
+
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
|
+
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
|
+
declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
55
|
+
/**
|
|
56
|
+
* Restores persisted data to QueryCache and persists further changes.
|
|
57
|
+
*/
|
|
58
|
+
declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
59
|
+
//#endregion
|
|
60
|
+
export { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
61
|
+
//# sourceMappingURL=persist.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist.d.cts","names":[],"sources":["../../src/persist.ts"],"mappings":";;KASY,WAAW,KAAK,IAAI,YAAY;UAE3B;EACf,gBAAgB,eAAe,oBAAoB;EACnD,qBAAqB,WAAW;EAChC,oBAAoB;;UAGL;EACf;EACA;EACA,aAAa;;UAGE;;EAEf,aAAa;;;EAGb,WAAW;;;EAGX;;UAGe,2CAA2C;;;;EAI1D;;EAEA,iBAAiB;;UAGF,wCAAwC;;EAEvD,mBAAmB;;UAGJ,kCAEb,oCACA,iCACA;;;;;;;iBAkBkB,4BACpB,aACA,WACA,QACA,QACA,kBACC,qCAAkC;;;;;;iBAoCf,yBACpB,aACA,WACA,QACA,oBACC,kCAA+B;;;;;iBAclB,4BACd,OAAO;;;;iBA2BO,mBACd,OAAO,yCACO"}
|
|
@@ -1,11 +1,61 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
import { DehydrateOptions, DehydratedState, HydrateOptions, QueryClient } from "@tanstack/query-core";
|
|
2
|
+
//#region src/persist.d.ts
|
|
3
|
+
type Promisable<T> = T | PromiseLike<T>;
|
|
4
|
+
interface Persister {
|
|
5
|
+
persistClient: (persistClient: PersistedClient) => Promisable<void>;
|
|
6
|
+
restoreClient: () => Promisable<PersistedClient | undefined>;
|
|
7
|
+
removeClient: () => Promisable<void>;
|
|
8
|
+
}
|
|
9
|
+
interface PersistedClient {
|
|
10
|
+
timestamp: number;
|
|
11
|
+
buster: string;
|
|
12
|
+
clientState: DehydratedState;
|
|
13
|
+
}
|
|
14
|
+
interface PersistQueryClientRootOptions {
|
|
15
|
+
/** The QueryClient to persist */
|
|
16
|
+
queryClient: QueryClient;
|
|
17
|
+
/** The Persister interface for storing and restoring the cache
|
|
18
|
+
* to/from a persisted location */
|
|
19
|
+
persister: Persister;
|
|
20
|
+
/** A unique string that can be used to forcefully
|
|
21
|
+
* invalidate existing caches if they do not share the same buster string */
|
|
22
|
+
buster?: string;
|
|
23
|
+
}
|
|
24
|
+
interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
|
|
25
|
+
/** The max-allowed age of the cache in milliseconds.
|
|
26
|
+
* If a persisted cache is found that is older than this
|
|
27
|
+
* time, it will be discarded */
|
|
28
|
+
maxAge?: number;
|
|
29
|
+
/** The options passed to the hydrate function */
|
|
30
|
+
hydrateOptions?: HydrateOptions;
|
|
31
|
+
}
|
|
32
|
+
interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
|
|
33
|
+
/** The options passed to the dehydrate function */
|
|
34
|
+
dehydrateOptions?: DehydrateOptions;
|
|
35
|
+
}
|
|
36
|
+
interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {}
|
|
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
|
+
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
|
+
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
|
+
declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
55
|
+
/**
|
|
56
|
+
* Restores persisted data to QueryCache and persists further changes.
|
|
57
|
+
*/
|
|
58
|
+
declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
59
|
+
//#endregion
|
|
60
|
+
export { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
61
|
+
//# sourceMappingURL=persist.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"persist.d.ts","names":[],"sources":["../../src/persist.ts"],"mappings":";;KASY,WAAW,KAAK,IAAI,YAAY;UAE3B;EACf,gBAAgB,eAAe,oBAAoB;EACnD,qBAAqB,WAAW;EAChC,oBAAoB;;UAGL;EACf;EACA;EACA,aAAa;;UAGE;;EAEf,aAAa;;;EAGb,WAAW;;;EAGX;;UAGe,2CAA2C;;;;EAI1D;;EAEA,iBAAiB;;UAGF,wCAAwC;;EAEvD,mBAAmB;;UAGJ,kCAEb,oCACA,iCACA;;;;;;;iBAkBkB,4BACpB,aACA,WACA,QACA,QACA,kBACC,qCAAkC;;;;;;iBAoCf,yBACpB,aACA,WACA,QACA,oBACC,kCAA+B;;;;;iBAclB,4BACd,OAAO;;;;iBA2BO,mBACd,OAAO,yCACO"}
|