@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.
Files changed (65) hide show
  1. package/build/legacy/createPersister.cjs +172 -256
  2. package/build/legacy/createPersister.cjs.map +1 -1
  3. package/build/legacy/createPersister.d.cts +88 -6
  4. package/build/legacy/createPersister.d.cts.map +1 -0
  5. package/build/legacy/createPersister.d.ts +88 -6
  6. package/build/legacy/createPersister.d.ts.map +1 -0
  7. package/build/legacy/createPersister.js +170 -235
  8. package/build/legacy/createPersister.js.map +1 -1
  9. package/build/legacy/index.cjs +14 -27
  10. package/build/legacy/index.cjs.map +1 -1
  11. package/build/legacy/index.d.cts +4 -19
  12. package/build/legacy/index.d.ts +4 -19
  13. package/build/legacy/index.js +8 -4
  14. package/build/legacy/index.js.map +1 -1
  15. package/build/legacy/persist.cjs +82 -108
  16. package/build/legacy/persist.cjs.map +1 -1
  17. package/build/legacy/persist.d.cts +61 -11
  18. package/build/legacy/persist.d.cts.map +1 -0
  19. package/build/legacy/persist.d.ts +61 -11
  20. package/build/legacy/persist.d.ts.map +1 -0
  21. package/build/legacy/persist.js +77 -79
  22. package/build/legacy/persist.js.map +1 -1
  23. package/build/legacy/retryStrategies.cjs +20 -44
  24. package/build/legacy/retryStrategies.cjs.map +1 -1
  25. package/build/legacy/retryStrategies.d.cts +11 -2
  26. package/build/legacy/retryStrategies.d.cts.map +1 -0
  27. package/build/legacy/retryStrategies.d.ts +11 -2
  28. package/build/legacy/retryStrategies.d.ts.map +1 -0
  29. package/build/legacy/retryStrategies.js +20 -20
  30. package/build/legacy/retryStrategies.js.map +1 -1
  31. package/build/modern/createPersister.cjs +172 -256
  32. package/build/modern/createPersister.cjs.map +1 -1
  33. package/build/modern/createPersister.d.cts +88 -6
  34. package/build/modern/createPersister.d.cts.map +1 -0
  35. package/build/modern/createPersister.d.ts +88 -6
  36. package/build/modern/createPersister.d.ts.map +1 -0
  37. package/build/modern/createPersister.js +170 -235
  38. package/build/modern/createPersister.js.map +1 -1
  39. package/build/modern/index.cjs +14 -27
  40. package/build/modern/index.cjs.map +1 -1
  41. package/build/modern/index.d.cts +4 -19
  42. package/build/modern/index.d.ts +4 -19
  43. package/build/modern/index.js +8 -4
  44. package/build/modern/index.js.map +1 -1
  45. package/build/modern/persist.cjs +82 -108
  46. package/build/modern/persist.cjs.map +1 -1
  47. package/build/modern/persist.d.cts +61 -11
  48. package/build/modern/persist.d.cts.map +1 -0
  49. package/build/modern/persist.d.ts +61 -11
  50. package/build/modern/persist.d.ts.map +1 -0
  51. package/build/modern/persist.js +77 -79
  52. package/build/modern/persist.js.map +1 -1
  53. package/build/modern/retryStrategies.cjs +20 -44
  54. package/build/modern/retryStrategies.cjs.map +1 -1
  55. package/build/modern/retryStrategies.d.cts +11 -2
  56. package/build/modern/retryStrategies.d.cts.map +1 -0
  57. package/build/modern/retryStrategies.d.ts +11 -2
  58. package/build/modern/retryStrategies.d.ts.map +1 -0
  59. package/build/modern/retryStrategies.js +20 -20
  60. package/build/modern/retryStrategies.js.map +1 -1
  61. package/package.json +4 -6
  62. package/build/legacy/_tsup-dts-rollup.d.cts +0 -214
  63. package/build/legacy/_tsup-dts-rollup.d.ts +0 -214
  64. package/build/modern/_tsup-dts-rollup.d.cts +0 -214
  65. package/build/modern/_tsup-dts-rollup.d.ts +0 -214
@@ -1,19 +1,4 @@
1
- export { persistQueryClientRestore } from './_tsup-dts-rollup.cjs';
2
- export { persistQueryClientSave } from './_tsup-dts-rollup.cjs';
3
- export { persistQueryClientSubscribe } from './_tsup-dts-rollup.cjs';
4
- export { persistQueryClient } from './_tsup-dts-rollup.cjs';
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 };
@@ -1,19 +1,4 @@
1
- export { persistQueryClientRestore } from './_tsup-dts-rollup.js';
2
- export { persistQueryClientSave } from './_tsup-dts-rollup.js';
3
- export { persistQueryClientSubscribe } from './_tsup-dts-rollup.js';
4
- export { persistQueryClient } from './_tsup-dts-rollup.js';
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 };
@@ -1,5 +1,9 @@
1
- // src/index.ts
2
- export * from "./persist.js";
3
- export * from "./retryStrategies.js";
4
- export * from "./createPersister.js";
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":";AAEA,cAAc;AACd,cAAc;AACd,cAAc;","names":[]}
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":""}
@@ -1,117 +1,91 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
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
- return cacheEventTypes.includes(eventType);
14
+ return cacheEventTypes.includes(eventType);
33
15
  }
34
- async function persistQueryClientRestore({
35
- queryClient,
36
- persister,
37
- maxAge = 1e3 * 60 * 60 * 24,
38
- buster = "",
39
- hydrateOptions
40
- }) {
41
- try {
42
- const persistedClient = await persister.restoreClient();
43
- if (persistedClient) {
44
- if (persistedClient.timestamp) {
45
- const expired = Date.now() - persistedClient.timestamp > maxAge;
46
- const busted = persistedClient.buster !== buster;
47
- if (expired || busted) {
48
- return persister.removeClient();
49
- } else {
50
- (0, import_query_core.hydrate)(queryClient, persistedClient.clientState, hydrateOptions);
51
- }
52
- } else {
53
- return persister.removeClient();
54
- }
55
- }
56
- } catch (err) {
57
- if (process.env.NODE_ENV !== "production") {
58
- console.error(err);
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
- async function persistQueryClientSave({
68
- queryClient,
69
- persister,
70
- buster = "",
71
- dehydrateOptions
72
- }) {
73
- const persistClient = {
74
- buster,
75
- timestamp: Date.now(),
76
- clientState: (0, import_query_core.dehydrate)(queryClient, dehydrateOptions)
77
- };
78
- await persister.persistClient(persistClient);
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
- const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe((event) => {
82
- if (isCacheEventType(event.type)) {
83
- persistQueryClientSave(props);
84
- }
85
- });
86
- const unsubscribeMutationCache = props.queryClient.getMutationCache().subscribe((event) => {
87
- if (isCacheEventType(event.type)) {
88
- persistQueryClientSave(props);
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
- let hasUnsubscribed = false;
98
- let persistQueryClientUnsubscribe;
99
- const unsubscribe = () => {
100
- hasUnsubscribed = true;
101
- persistQueryClientUnsubscribe?.();
102
- };
103
- const restorePromise = persistQueryClientRestore(props).then(() => {
104
- if (!hasUnsubscribed) {
105
- persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
106
- }
107
- });
108
- return [unsubscribe, restorePromise];
75
+ let hasUnsubscribed = false;
76
+ let persistQueryClientUnsubscribe;
77
+ const unsubscribe = () => {
78
+ hasUnsubscribed = true;
79
+ persistQueryClientUnsubscribe?.();
80
+ };
81
+ return [unsubscribe, persistQueryClientRestore(props).then(() => {
82
+ if (!hasUnsubscribed) persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
83
+ })];
109
84
  }
110
- // Annotate the CommonJS export names for ESM import in node:
111
- 0 && (module.exports = {
112
- persistQueryClient,
113
- persistQueryClientRestore,
114
- persistQueryClientSave,
115
- persistQueryClientSubscribe
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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAmC;AA0DnC,IAAM,kBAA0C,CAAC,SAAS,WAAW,SAAS;AAE9E,SAAS,iBAAiB,WAA4B;AACpD,SAAO,gBAAgB,SAAS,SAAS;AAC3C;AAQA,eAAsB,0BAA0B;AAAA,EAC9C;AAAA,EACA;AAAA,EACA,SAAS,MAAO,KAAK,KAAK;AAAA,EAC1B,SAAS;AAAA,EACT;AACF,GAAuC;AACrC,MAAI;AACF,UAAM,kBAAkB,MAAM,UAAU,cAAc;AAEtD,QAAI,iBAAiB;AACnB,UAAI,gBAAgB,WAAW;AAC7B,cAAM,UAAU,KAAK,IAAI,IAAI,gBAAgB,YAAY;AACzD,cAAM,SAAS,gBAAgB,WAAW;AAC1C,YAAI,WAAW,QAAQ;AACrB,iBAAO,UAAU,aAAa;AAAA,QAChC,OAAO;AACL,yCAAQ,aAAa,gBAAgB,aAAa,cAAc;AAAA,QAClE;AAAA,MACF,OAAO;AACL,eAAO,UAAU,aAAa;AAAA,MAChC;AAAA,IACF;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,cAAQ,MAAM,GAAG;AACjB,cAAQ;AAAA,QACN;AAAA,MACF;AAAA,IACF;AAEA,UAAM,UAAU,aAAa;AAE7B,UAAM;AAAA,EACR;AACF;AAOA,eAAsB,uBAAuB;AAAA,EAC3C;AAAA,EACA;AAAA,EACA,SAAS;AAAA,EACT;AACF,GAAoC;AAClC,QAAM,gBAAiC;AAAA,IACrC;AAAA,IACA,WAAW,KAAK,IAAI;AAAA,IACpB,iBAAa,6BAAU,aAAa,gBAAgB;AAAA,EACtD;AAEA,QAAM,UAAU,cAAc,aAAa;AAC7C;AAMO,SAAS,4BACd,OACA;AACA,QAAM,wBAAwB,MAAM,YACjC,cAAc,EACd,UAAU,CAAC,UAAU;AACpB,QAAI,iBAAiB,MAAM,IAAI,GAAG;AAChC,6BAAuB,KAAK;AAAA,IAC9B;AAAA,EACF,CAAC;AAEH,QAAM,2BAA2B,MAAM,YACpC,iBAAiB,EACjB,UAAU,CAAC,UAAU;AACpB,QAAI,iBAAiB,MAAM,IAAI,GAAG;AAChC,6BAAuB,KAAK;AAAA,IAC9B;AAAA,EACF,CAAC;AAEH,SAAO,MAAM;AACX,0BAAsB;AACtB,6BAAyB;AAAA,EAC3B;AACF;AAKO,SAAS,mBACd,OAC6B;AAC7B,MAAI,kBAAkB;AACtB,MAAI;AACJ,QAAM,cAAc,MAAM;AACxB,sBAAkB;AAClB,oCAAgC;AAAA,EAClC;AAGA,QAAM,iBAAiB,0BAA0B,KAAK,EAAE,KAAK,MAAM;AACjE,QAAI,CAAC,iBAAiB;AAEpB,sCAAgC,4BAA4B,KAAK;AAAA,IACnE;AAAA,EACF,CAAC;AAED,SAAO,CAAC,aAAa,cAAc;AACrC;","names":[]}
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,gCAAgC;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
- export { persistQueryClientRestore_alias_1 as persistQueryClientRestore } from './_tsup-dts-rollup.cjs';
2
- export { persistQueryClientSave_alias_1 as persistQueryClientSave } from './_tsup-dts-rollup.cjs';
3
- export { persistQueryClientSubscribe_alias_1 as persistQueryClientSubscribe } from './_tsup-dts-rollup.cjs';
4
- export { persistQueryClient_alias_1 as persistQueryClient } from './_tsup-dts-rollup.cjs';
5
- export { Promisable_alias_1 as Promisable } from './_tsup-dts-rollup.cjs';
6
- export { Persister_alias_1 as Persister } from './_tsup-dts-rollup.cjs';
7
- export { PersistedClient_alias_1 as PersistedClient } from './_tsup-dts-rollup.cjs';
8
- export { PersistQueryClientRootOptions_alias_1 as PersistQueryClientRootOptions } from './_tsup-dts-rollup.cjs';
9
- export { PersistedQueryClientRestoreOptions_alias_1 as PersistedQueryClientRestoreOptions } from './_tsup-dts-rollup.cjs';
10
- export { PersistedQueryClientSaveOptions_alias_1 as PersistedQueryClientSaveOptions } from './_tsup-dts-rollup.cjs';
11
- export { PersistQueryClientOptions_alias_1 as PersistQueryClientOptions } from './_tsup-dts-rollup.cjs';
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
- export { persistQueryClientRestore_alias_1 as persistQueryClientRestore } from './_tsup-dts-rollup.js';
2
- export { persistQueryClientSave_alias_1 as persistQueryClientSave } from './_tsup-dts-rollup.js';
3
- export { persistQueryClientSubscribe_alias_1 as persistQueryClientSubscribe } from './_tsup-dts-rollup.js';
4
- export { persistQueryClient_alias_1 as persistQueryClient } from './_tsup-dts-rollup.js';
5
- export { Promisable_alias_1 as Promisable } from './_tsup-dts-rollup.js';
6
- export { Persister_alias_1 as Persister } from './_tsup-dts-rollup.js';
7
- export { PersistedClient_alias_1 as PersistedClient } from './_tsup-dts-rollup.js';
8
- export { PersistQueryClientRootOptions_alias_1 as PersistQueryClientRootOptions } from './_tsup-dts-rollup.js';
9
- export { PersistedQueryClientRestoreOptions_alias_1 as PersistedQueryClientRestoreOptions } from './_tsup-dts-rollup.js';
10
- export { PersistedQueryClientSaveOptions_alias_1 as PersistedQueryClientSaveOptions } from './_tsup-dts-rollup.js';
11
- export { PersistQueryClientOptions_alias_1 as PersistQueryClientOptions } from './_tsup-dts-rollup.js';
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"}