@tanstack/query-persist-client-core 5.0.0-alpha.85 → 5.0.0-alpha.87
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/index.cjs +25 -0
- package/build/legacy/index.cjs.map +1 -0
- package/build/legacy/index.d.cts +3 -0
- package/build/legacy/index.d.ts +3 -0
- package/build/legacy/index.js +3 -0
- package/build/legacy/index.js.map +1 -0
- package/build/legacy/persist.cjs +118 -0
- package/build/legacy/persist.cjs.map +1 -0
- package/build/legacy/persist.d.cts +61 -0
- package/build/{lib → legacy}/persist.d.ts +15 -13
- package/build/{lib/persist.legacy.js → legacy/persist.js} +22 -42
- package/build/legacy/persist.js.map +1 -0
- package/build/legacy/retryStrategies.cjs +45 -0
- package/build/legacy/retryStrategies.cjs.map +1 -0
- package/build/legacy/retryStrategies.d.cts +11 -0
- package/build/legacy/retryStrategies.d.ts +11 -0
- package/build/legacy/retryStrategies.js +21 -0
- package/build/legacy/retryStrategies.js.map +1 -0
- package/build/modern/index.cjs +25 -0
- package/build/modern/index.cjs.map +1 -0
- package/build/modern/index.d.cts +3 -0
- package/build/modern/index.d.ts +3 -0
- package/build/modern/index.js +3 -0
- package/build/modern/index.js.map +1 -0
- package/build/modern/persist.cjs +118 -0
- package/build/modern/persist.cjs.map +1 -0
- package/build/modern/persist.d.cts +61 -0
- package/build/modern/persist.d.ts +61 -0
- package/build/{lib → modern}/persist.js +22 -42
- package/build/modern/persist.js.map +1 -0
- package/build/modern/retryStrategies.cjs +45 -0
- package/build/modern/retryStrategies.cjs.map +1 -0
- package/build/modern/retryStrategies.d.cts +11 -0
- package/build/modern/retryStrategies.d.ts +11 -0
- package/build/modern/retryStrategies.js +21 -0
- package/build/modern/retryStrategies.js.map +1 -0
- package/package.json +16 -14
- package/build/lib/__tests__/persist.test.d.ts +0 -2
- package/build/lib/__tests__/persist.test.d.ts.map +0 -1
- package/build/lib/__tests__/utils.d.ts +0 -8
- package/build/lib/__tests__/utils.d.ts.map +0 -1
- package/build/lib/index.cjs +0 -13
- package/build/lib/index.cjs.map +0 -1
- package/build/lib/index.d.ts +0 -3
- package/build/lib/index.d.ts.map +0 -1
- package/build/lib/index.js +0 -3
- package/build/lib/index.js.map +0 -1
- package/build/lib/index.legacy.cjs +0 -13
- package/build/lib/index.legacy.cjs.map +0 -1
- package/build/lib/index.legacy.js +0 -3
- package/build/lib/index.legacy.js.map +0 -1
- package/build/lib/persist.cjs +0 -116
- package/build/lib/persist.cjs.map +0 -1
- package/build/lib/persist.d.ts.map +0 -1
- package/build/lib/persist.js.map +0 -1
- package/build/lib/persist.legacy.cjs +0 -116
- package/build/lib/persist.legacy.cjs.map +0 -1
- package/build/lib/persist.legacy.js.map +0 -1
- package/build/lib/retryStrategies.cjs +0 -29
- package/build/lib/retryStrategies.cjs.map +0 -1
- package/build/lib/retryStrategies.d.ts +0 -8
- package/build/lib/retryStrategies.d.ts.map +0 -1
- package/build/lib/retryStrategies.js +0 -27
- package/build/lib/retryStrategies.js.map +0 -1
- package/build/lib/retryStrategies.legacy.cjs +0 -29
- package/build/lib/retryStrategies.legacy.cjs.map +0 -1
- package/build/lib/retryStrategies.legacy.js +0 -27
- package/build/lib/retryStrategies.legacy.js.map +0 -1
|
@@ -0,0 +1,25 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var src_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(src_exports);
|
|
18
|
+
__reExport(src_exports, require("./persist"), module.exports);
|
|
19
|
+
__reExport(src_exports, require("./retryStrategies"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./persist"),
|
|
23
|
+
...require("./retryStrategies")
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAEA,wBAAc,sBAFd;AAGA,wBAAc,8BAHd;","names":[]}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
|
|
2
|
+
export { PersistRetryer, removeOldestQuery } from './retryStrategies.cjs';
|
|
3
|
+
import '@tanstack/query-core';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
|
|
2
|
+
export { PersistRetryer, removeOldestQuery } from './retryStrategies.js';
|
|
3
|
+
import '@tanstack/query-core';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":"AAEA,cAAc;AACd,cAAc;","names":[]}
|
|
@@ -0,0 +1,118 @@
|
|
|
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
|
+
var persist_exports = {};
|
|
20
|
+
__export(persist_exports, {
|
|
21
|
+
persistQueryClient: () => persistQueryClient,
|
|
22
|
+
persistQueryClientRestore: () => persistQueryClientRestore,
|
|
23
|
+
persistQueryClientSave: () => persistQueryClientSave,
|
|
24
|
+
persistQueryClientSubscribe: () => persistQueryClientSubscribe
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(persist_exports);
|
|
27
|
+
var import_query_core = require("@tanstack/query-core");
|
|
28
|
+
const cacheableEventTypes = [
|
|
29
|
+
"added",
|
|
30
|
+
"removed",
|
|
31
|
+
"updated"
|
|
32
|
+
];
|
|
33
|
+
function isCacheableEventType(eventType) {
|
|
34
|
+
return cacheableEventTypes.includes(eventType);
|
|
35
|
+
}
|
|
36
|
+
async function persistQueryClientRestore({
|
|
37
|
+
queryClient,
|
|
38
|
+
persister,
|
|
39
|
+
maxAge = 1e3 * 60 * 60 * 24,
|
|
40
|
+
buster = "",
|
|
41
|
+
hydrateOptions
|
|
42
|
+
}) {
|
|
43
|
+
try {
|
|
44
|
+
const persistedClient = await persister.restoreClient();
|
|
45
|
+
if (persistedClient) {
|
|
46
|
+
if (persistedClient.timestamp) {
|
|
47
|
+
const expired = Date.now() - persistedClient.timestamp > maxAge;
|
|
48
|
+
const busted = persistedClient.buster !== buster;
|
|
49
|
+
if (expired || busted) {
|
|
50
|
+
persister.removeClient();
|
|
51
|
+
} else {
|
|
52
|
+
(0, import_query_core.hydrate)(queryClient, persistedClient.clientState, hydrateOptions);
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
persister.removeClient();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} catch (err) {
|
|
59
|
+
if (process.env.NODE_ENV !== "production") {
|
|
60
|
+
console.error(err);
|
|
61
|
+
console.warn(
|
|
62
|
+
"Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded."
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
persister.removeClient();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function persistQueryClientSave({
|
|
69
|
+
queryClient,
|
|
70
|
+
persister,
|
|
71
|
+
buster = "",
|
|
72
|
+
dehydrateOptions
|
|
73
|
+
}) {
|
|
74
|
+
const persistClient = {
|
|
75
|
+
buster,
|
|
76
|
+
timestamp: Date.now(),
|
|
77
|
+
clientState: (0, import_query_core.dehydrate)(queryClient, dehydrateOptions)
|
|
78
|
+
};
|
|
79
|
+
await persister.persistClient(persistClient);
|
|
80
|
+
}
|
|
81
|
+
function persistQueryClientSubscribe(props) {
|
|
82
|
+
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe((event) => {
|
|
83
|
+
if (isCacheableEventType(event.type)) {
|
|
84
|
+
persistQueryClientSave(props);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe((event) => {
|
|
88
|
+
if (isCacheableEventType(event.type)) {
|
|
89
|
+
persistQueryClientSave(props);
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
return () => {
|
|
93
|
+
unsubscribeQueryCache();
|
|
94
|
+
unusbscribeMutationCache();
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function persistQueryClient(props) {
|
|
98
|
+
let hasUnsubscribed = false;
|
|
99
|
+
let persistQueryClientUnsubscribe;
|
|
100
|
+
const unsubscribe = () => {
|
|
101
|
+
hasUnsubscribed = true;
|
|
102
|
+
persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
|
|
103
|
+
};
|
|
104
|
+
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
105
|
+
if (!hasUnsubscribed) {
|
|
106
|
+
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
return [unsubscribe, restorePromise];
|
|
110
|
+
}
|
|
111
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
112
|
+
0 && (module.exports = {
|
|
113
|
+
persistQueryClient,
|
|
114
|
+
persistQueryClientRestore,
|
|
115
|
+
persistQueryClientSave,
|
|
116
|
+
persistQueryClientSubscribe
|
|
117
|
+
});
|
|
118
|
+
//# sourceMappingURL=persist.cjs.map
|
|
@@ -0,0 +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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\n /** The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting)\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAmC;AA2DnC,MAAM,sBAA8C;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AACF;AAEA,SAAS,qBAAqB,WAA4B;AACxD,SAAO,oBAAoB,SAAS,SAAS;AAC/C;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,oBAAU,aAAa;AAAA,QACzB,OAAO;AACL,yCAAQ,aAAa,gBAAgB,aAAa,cAAc;AAAA,QAClE;AAAA,MACF,OAAO;AACL,kBAAU,aAAa;AAAA,MACzB;AAAA,IACF;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,cAAQ,MAAM,GAAG;AACjB,cAAQ;AAAA,QACN;AAAA,MACF;AAAA,IACF;AACA,cAAU,aAAa;AAAA,EACzB;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,qBAAqB,MAAM,IAAI,GAAG;AACpC,6BAAuB,KAAK;AAAA,IAC9B;AAAA,EACF,CAAC;AAEH,QAAM,2BAA2B,MAAM,YACpC,iBAAiB,EACjB,UAAU,CAAC,UAAU;AACpB,QAAI,qBAAqB,MAAM,IAAI,GAAG;AACpC,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;AAAA,EACF;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":[]}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { DehydratedState, QueryClient, HydrateOptions, DehydrateOptions } from '@tanstack/query-core';
|
|
2
|
+
|
|
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 PersistQueryClienRootOptions {
|
|
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 PersistQueryClienRootOptions {
|
|
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 PersistQueryClienRootOptions {
|
|
33
|
+
/** The options passed to the dehydrate function */
|
|
34
|
+
dehydrateOptions?: DehydrateOptions;
|
|
35
|
+
}
|
|
36
|
+
interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Restores persisted data to the QueryCache
|
|
40
|
+
* - data obtained from persister.restoreClient
|
|
41
|
+
* - data is hydrated using hydrateOptions
|
|
42
|
+
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
43
|
+
*/
|
|
44
|
+
declare function persistQueryClientRestore({ queryClient, persister, maxAge, buster, hydrateOptions, }: PersistedQueryClientRestoreOptions): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Persists data from the QueryCache
|
|
47
|
+
* - data dehydrated using dehydrateOptions
|
|
48
|
+
* - data is persisted using persister.persistClient
|
|
49
|
+
*/
|
|
50
|
+
declare function persistQueryClientSave({ queryClient, persister, buster, dehydrateOptions, }: PersistedQueryClientSaveOptions): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
53
|
+
* @returns an unsubscribe function (to discontinue monitoring)
|
|
54
|
+
*/
|
|
55
|
+
declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
56
|
+
/**
|
|
57
|
+
* Restores persisted data to QueryCache and persists further changes.
|
|
58
|
+
*/
|
|
59
|
+
declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
60
|
+
|
|
61
|
+
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
@@ -1,16 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { DehydratedState, QueryClient, HydrateOptions, DehydrateOptions } from '@tanstack/query-core';
|
|
2
|
+
|
|
3
|
+
type Promisable<T> = T | PromiseLike<T>;
|
|
4
|
+
interface Persister {
|
|
4
5
|
persistClient(persistClient: PersistedClient): Promisable<void>;
|
|
5
6
|
restoreClient(): Promisable<PersistedClient | undefined>;
|
|
6
7
|
removeClient(): Promisable<void>;
|
|
7
8
|
}
|
|
8
|
-
|
|
9
|
+
interface PersistedClient {
|
|
9
10
|
timestamp: number;
|
|
10
11
|
buster: string;
|
|
11
12
|
clientState: DehydratedState;
|
|
12
13
|
}
|
|
13
|
-
|
|
14
|
+
interface PersistQueryClienRootOptions {
|
|
14
15
|
/** The QueryClient to persist */
|
|
15
16
|
queryClient: QueryClient;
|
|
16
17
|
/** The Persister interface for storing and restoring the cache
|
|
@@ -20,7 +21,7 @@ export interface PersistQueryClienRootOptions {
|
|
|
20
21
|
* invalidate existing caches if they do not share the same buster string */
|
|
21
22
|
buster?: string;
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
+
interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOptions {
|
|
24
25
|
/** The max-allowed age of the cache in milliseconds.
|
|
25
26
|
* If a persisted cache is found that is older than this
|
|
26
27
|
* time, it will be discarded */
|
|
@@ -28,11 +29,11 @@ export interface PersistedQueryClientRestoreOptions extends PersistQueryClienRoo
|
|
|
28
29
|
/** The options passed to the hydrate function */
|
|
29
30
|
hydrateOptions?: HydrateOptions;
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
+
interface PersistedQueryClientSaveOptions extends PersistQueryClienRootOptions {
|
|
32
33
|
/** The options passed to the dehydrate function */
|
|
33
34
|
dehydrateOptions?: DehydrateOptions;
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
+
interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
|
|
36
37
|
}
|
|
37
38
|
/**
|
|
38
39
|
* Restores persisted data to the QueryCache
|
|
@@ -40,20 +41,21 @@ export interface PersistQueryClientOptions extends PersistedQueryClientRestoreOp
|
|
|
40
41
|
* - data is hydrated using hydrateOptions
|
|
41
42
|
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
42
43
|
*/
|
|
43
|
-
|
|
44
|
+
declare function persistQueryClientRestore({ queryClient, persister, maxAge, buster, hydrateOptions, }: PersistedQueryClientRestoreOptions): Promise<void>;
|
|
44
45
|
/**
|
|
45
46
|
* Persists data from the QueryCache
|
|
46
47
|
* - data dehydrated using dehydrateOptions
|
|
47
48
|
* - data is persisted using persister.persistClient
|
|
48
49
|
*/
|
|
49
|
-
|
|
50
|
+
declare function persistQueryClientSave({ queryClient, persister, buster, dehydrateOptions, }: PersistedQueryClientSaveOptions): Promise<void>;
|
|
50
51
|
/**
|
|
51
52
|
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
52
53
|
* @returns an unsubscribe function (to discontinue monitoring)
|
|
53
54
|
*/
|
|
54
|
-
|
|
55
|
+
declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOptions): () => void;
|
|
55
56
|
/**
|
|
56
57
|
* Restores persisted data to QueryCache and persists further changes.
|
|
57
58
|
*/
|
|
58
|
-
|
|
59
|
-
|
|
59
|
+
declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
|
|
60
|
+
|
|
61
|
+
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
|
|
@@ -1,25 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const cacheableEventTypes = ['added', 'removed', 'updated'];
|
|
1
|
+
import { dehydrate, hydrate } from "@tanstack/query-core";
|
|
2
|
+
const cacheableEventTypes = [
|
|
3
|
+
"added",
|
|
4
|
+
"removed",
|
|
5
|
+
"updated"
|
|
6
|
+
];
|
|
8
7
|
function isCacheableEventType(eventType) {
|
|
9
8
|
return cacheableEventTypes.includes(eventType);
|
|
10
9
|
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Restores persisted data to the QueryCache
|
|
14
|
-
* - data obtained from persister.restoreClient
|
|
15
|
-
* - data is hydrated using hydrateOptions
|
|
16
|
-
* If data is expired, busted, empty, or throws, it runs persister.removeClient
|
|
17
|
-
*/
|
|
18
10
|
async function persistQueryClientRestore({
|
|
19
11
|
queryClient,
|
|
20
12
|
persister,
|
|
21
|
-
maxAge =
|
|
22
|
-
buster =
|
|
13
|
+
maxAge = 1e3 * 60 * 60 * 24,
|
|
14
|
+
buster = "",
|
|
23
15
|
hydrateOptions
|
|
24
16
|
}) {
|
|
25
17
|
try {
|
|
@@ -38,23 +30,19 @@ async function persistQueryClientRestore({
|
|
|
38
30
|
}
|
|
39
31
|
}
|
|
40
32
|
} catch (err) {
|
|
41
|
-
if (process.env.NODE_ENV !==
|
|
33
|
+
if (process.env.NODE_ENV !== "production") {
|
|
42
34
|
console.error(err);
|
|
43
|
-
console.warn(
|
|
35
|
+
console.warn(
|
|
36
|
+
"Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded."
|
|
37
|
+
);
|
|
44
38
|
}
|
|
45
39
|
persister.removeClient();
|
|
46
40
|
}
|
|
47
41
|
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Persists data from the QueryCache
|
|
51
|
-
* - data dehydrated using dehydrateOptions
|
|
52
|
-
* - data is persisted using persister.persistClient
|
|
53
|
-
*/
|
|
54
42
|
async function persistQueryClientSave({
|
|
55
43
|
queryClient,
|
|
56
44
|
persister,
|
|
57
|
-
buster =
|
|
45
|
+
buster = "",
|
|
58
46
|
dehydrateOptions
|
|
59
47
|
}) {
|
|
60
48
|
const persistClient = {
|
|
@@ -64,18 +52,13 @@ async function persistQueryClientSave({
|
|
|
64
52
|
};
|
|
65
53
|
await persister.persistClient(persistClient);
|
|
66
54
|
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Subscribe to QueryCache and MutationCache updates (for persisting)
|
|
70
|
-
* @returns an unsubscribe function (to discontinue monitoring)
|
|
71
|
-
*/
|
|
72
55
|
function persistQueryClientSubscribe(props) {
|
|
73
|
-
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
|
|
56
|
+
const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe((event) => {
|
|
74
57
|
if (isCacheableEventType(event.type)) {
|
|
75
58
|
persistQueryClientSave(props);
|
|
76
59
|
}
|
|
77
60
|
});
|
|
78
|
-
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(event => {
|
|
61
|
+
const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe((event) => {
|
|
79
62
|
if (isCacheableEventType(event.type)) {
|
|
80
63
|
persistQueryClientSave(props);
|
|
81
64
|
}
|
|
@@ -85,10 +68,6 @@ function persistQueryClientSubscribe(props) {
|
|
|
85
68
|
unusbscribeMutationCache();
|
|
86
69
|
};
|
|
87
70
|
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Restores persisted data to QueryCache and persists further changes.
|
|
91
|
-
*/
|
|
92
71
|
function persistQueryClient(props) {
|
|
93
72
|
let hasUnsubscribed = false;
|
|
94
73
|
let persistQueryClientUnsubscribe;
|
|
@@ -96,16 +75,17 @@ function persistQueryClient(props) {
|
|
|
96
75
|
hasUnsubscribed = true;
|
|
97
76
|
persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
|
|
98
77
|
};
|
|
99
|
-
|
|
100
|
-
// Attempt restore
|
|
101
78
|
const restorePromise = persistQueryClientRestore(props).then(() => {
|
|
102
79
|
if (!hasUnsubscribed) {
|
|
103
|
-
// Subscribe to changes in the query cache to trigger the save
|
|
104
80
|
persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
|
|
105
81
|
}
|
|
106
82
|
});
|
|
107
83
|
return [unsubscribe, restorePromise];
|
|
108
84
|
}
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
85
|
+
export {
|
|
86
|
+
persistQueryClient,
|
|
87
|
+
persistQueryClientRestore,
|
|
88
|
+
persistQueryClientSave,
|
|
89
|
+
persistQueryClientSubscribe
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=persist.js.map
|
|
@@ -0,0 +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 PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\n /** The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting)\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"mappings":"AAAA,SAAS,WAAW,eAAe;AA2DnC,MAAM,sBAA8C;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AACF;AAEA,SAAS,qBAAqB,WAA4B;AACxD,SAAO,oBAAoB,SAAS,SAAS;AAC/C;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,oBAAU,aAAa;AAAA,QACzB,OAAO;AACL,kBAAQ,aAAa,gBAAgB,aAAa,cAAc;AAAA,QAClE;AAAA,MACF,OAAO;AACL,kBAAU,aAAa;AAAA,MACzB;AAAA,IACF;AAAA,EACF,SAAS,KAAK;AACZ,QAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,cAAQ,MAAM,GAAG;AACjB,cAAQ;AAAA,QACN;AAAA,MACF;AAAA,IACF;AACA,cAAU,aAAa;AAAA,EACzB;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,aAAa,UAAU,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,qBAAqB,MAAM,IAAI,GAAG;AACpC,6BAAuB,KAAK;AAAA,IAC9B;AAAA,EACF,CAAC;AAEH,QAAM,2BAA2B,MAAM,YACpC,iBAAiB,EACjB,UAAU,CAAC,UAAU;AACpB,QAAI,qBAAqB,MAAM,IAAI,GAAG;AACpC,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;AAAA,EACF;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":[]}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
var retryStrategies_exports = {};
|
|
20
|
+
__export(retryStrategies_exports, {
|
|
21
|
+
removeOldestQuery: () => removeOldestQuery
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(retryStrategies_exports);
|
|
24
|
+
const removeOldestQuery = ({ persistedClient }) => {
|
|
25
|
+
const mutations = [...persistedClient.clientState.mutations];
|
|
26
|
+
const queries = [...persistedClient.clientState.queries];
|
|
27
|
+
const client = {
|
|
28
|
+
...persistedClient,
|
|
29
|
+
clientState: { mutations, queries }
|
|
30
|
+
};
|
|
31
|
+
const sortedQueries = [...queries].sort(
|
|
32
|
+
(a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt
|
|
33
|
+
);
|
|
34
|
+
if (sortedQueries.length > 0) {
|
|
35
|
+
const oldestData = sortedQueries.shift();
|
|
36
|
+
client.clientState.queries = queries.filter((q) => q !== oldestData);
|
|
37
|
+
return client;
|
|
38
|
+
}
|
|
39
|
+
return void 0;
|
|
40
|
+
};
|
|
41
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
42
|
+
0 && (module.exports = {
|
|
43
|
+
removeOldestQuery
|
|
44
|
+
});
|
|
45
|
+
//# sourceMappingURL=retryStrategies.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from './persist'\n\nexport type PersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => PersistedClient | undefined\n\nexport const removeOldestQuery: PersistRetryer = ({ persistedClient }) => {\n const mutations = [...persistedClient.clientState.mutations]\n const queries = [...persistedClient.clientState.queries]\n const client: PersistedClient = {\n ...persistedClient,\n clientState: { mutations, queries },\n }\n\n // sort queries by dataUpdatedAt (oldest first)\n const sortedQueries = [...queries].sort(\n (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt,\n )\n\n // clean oldest query\n if (sortedQueries.length > 0) {\n const oldestData = sortedQueries.shift()\n client.clientState.queries = queries.filter((q) => q !== oldestData)\n return client\n }\n\n return undefined\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQO,MAAM,oBAAoC,CAAC,EAAE,gBAAgB,MAAM;AACxE,QAAM,YAAY,CAAC,GAAG,gBAAgB,YAAY,SAAS;AAC3D,QAAM,UAAU,CAAC,GAAG,gBAAgB,YAAY,OAAO;AACvD,QAAM,SAA0B;AAAA,IAC9B,GAAG;AAAA,IACH,aAAa,EAAE,WAAW,QAAQ;AAAA,EACpC;AAGA,QAAM,gBAAgB,CAAC,GAAG,OAAO,EAAE;AAAA,IACjC,CAAC,GAAG,MAAM,EAAE,MAAM,gBAAgB,EAAE,MAAM;AAAA,EAC5C;AAGA,MAAI,cAAc,SAAS,GAAG;AAC5B,UAAM,aAAa,cAAc,MAAM;AACvC,WAAO,YAAY,UAAU,QAAQ,OAAO,CAAC,MAAM,MAAM,UAAU;AACnE,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PersistedClient } from './persist.cjs';
|
|
2
|
+
import '@tanstack/query-core';
|
|
3
|
+
|
|
4
|
+
type PersistRetryer = (props: {
|
|
5
|
+
persistedClient: PersistedClient;
|
|
6
|
+
error: Error;
|
|
7
|
+
errorCount: number;
|
|
8
|
+
}) => PersistedClient | undefined;
|
|
9
|
+
declare const removeOldestQuery: PersistRetryer;
|
|
10
|
+
|
|
11
|
+
export { PersistRetryer, removeOldestQuery };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PersistedClient } from './persist.js';
|
|
2
|
+
import '@tanstack/query-core';
|
|
3
|
+
|
|
4
|
+
type PersistRetryer = (props: {
|
|
5
|
+
persistedClient: PersistedClient;
|
|
6
|
+
error: Error;
|
|
7
|
+
errorCount: number;
|
|
8
|
+
}) => PersistedClient | undefined;
|
|
9
|
+
declare const removeOldestQuery: PersistRetryer;
|
|
10
|
+
|
|
11
|
+
export { PersistRetryer, removeOldestQuery };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const removeOldestQuery = ({ persistedClient }) => {
|
|
2
|
+
const mutations = [...persistedClient.clientState.mutations];
|
|
3
|
+
const queries = [...persistedClient.clientState.queries];
|
|
4
|
+
const client = {
|
|
5
|
+
...persistedClient,
|
|
6
|
+
clientState: { mutations, queries }
|
|
7
|
+
};
|
|
8
|
+
const sortedQueries = [...queries].sort(
|
|
9
|
+
(a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt
|
|
10
|
+
);
|
|
11
|
+
if (sortedQueries.length > 0) {
|
|
12
|
+
const oldestData = sortedQueries.shift();
|
|
13
|
+
client.clientState.queries = queries.filter((q) => q !== oldestData);
|
|
14
|
+
return client;
|
|
15
|
+
}
|
|
16
|
+
return void 0;
|
|
17
|
+
};
|
|
18
|
+
export {
|
|
19
|
+
removeOldestQuery
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=retryStrategies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/retryStrategies.ts"],"sourcesContent":["import type { PersistedClient } from './persist'\n\nexport type PersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => PersistedClient | undefined\n\nexport const removeOldestQuery: PersistRetryer = ({ persistedClient }) => {\n const mutations = [...persistedClient.clientState.mutations]\n const queries = [...persistedClient.clientState.queries]\n const client: PersistedClient = {\n ...persistedClient,\n clientState: { mutations, queries },\n }\n\n // sort queries by dataUpdatedAt (oldest first)\n const sortedQueries = [...queries].sort(\n (a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt,\n )\n\n // clean oldest query\n if (sortedQueries.length > 0) {\n const oldestData = sortedQueries.shift()\n client.clientState.queries = queries.filter((q) => q !== oldestData)\n return client\n }\n\n return undefined\n}\n"],"mappings":"AAQO,MAAM,oBAAoC,CAAC,EAAE,gBAAgB,MAAM;AACxE,QAAM,YAAY,CAAC,GAAG,gBAAgB,YAAY,SAAS;AAC3D,QAAM,UAAU,CAAC,GAAG,gBAAgB,YAAY,OAAO;AACvD,QAAM,SAA0B;AAAA,IAC9B,GAAG;AAAA,IACH,aAAa,EAAE,WAAW,QAAQ;AAAA,EACpC;AAGA,QAAM,gBAAgB,CAAC,GAAG,OAAO,EAAE;AAAA,IACjC,CAAC,GAAG,MAAM,EAAE,MAAM,gBAAgB,EAAE,MAAM;AAAA,EAC5C;AAGA,MAAI,cAAc,SAAS,GAAG;AAC5B,UAAM,aAAa,cAAc,MAAM;AACvC,WAAO,YAAY,UAAU,QAAQ,OAAO,CAAC,MAAM,MAAM,UAAU;AACnE,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -0,0 +1,25 @@
|
|
|
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 __copyProps = (to, from, except, desc) => {
|
|
7
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
+
for (let key of __getOwnPropNames(from))
|
|
9
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
+
}
|
|
12
|
+
return to;
|
|
13
|
+
};
|
|
14
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
+
var src_exports = {};
|
|
17
|
+
module.exports = __toCommonJS(src_exports);
|
|
18
|
+
__reExport(src_exports, require("./persist"), module.exports);
|
|
19
|
+
__reExport(src_exports, require("./retryStrategies"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./persist"),
|
|
23
|
+
...require("./retryStrategies")
|
|
24
|
+
});
|
|
25
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":";;;;;;;;;;;;;;;AAAA;AAAA;AAEA,wBAAc,sBAFd;AAGA,wBAAc,8BAHd;","names":[]}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
|
|
2
|
+
export { PersistRetryer, removeOldestQuery } from './retryStrategies.cjs';
|
|
3
|
+
import '@tanstack/query-core';
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
|
|
2
|
+
export { PersistRetryer, removeOldestQuery } from './retryStrategies.js';
|
|
3
|
+
import '@tanstack/query-core';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/* istanbul ignore file */\n\nexport * from './persist'\nexport * from './retryStrategies'\n"],"mappings":"AAEA,cAAc;AACd,cAAc;","names":[]}
|