@tanstack/query-persist-client-core 5.0.0-alpha.49 → 5.0.0-alpha.50

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.
@@ -96,7 +96,7 @@ function persistQueryClient(props) {
96
96
  let persistQueryClientUnsubscribe;
97
97
  const unsubscribe = () => {
98
98
  hasUnsubscribed = true;
99
- persistQueryClientUnsubscribe == null ? void 0 : persistQueryClientUnsubscribe();
99
+ persistQueryClientUnsubscribe?.();
100
100
  };
101
101
 
102
102
  // Attempt restore
@@ -1 +1 @@
1
- {"version":3,"file":"persist.js","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient(persistClient: PersistedClient): Promisable<void>\n restoreClient(): Promisable<PersistedClient | undefined>\n removeClient(): Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\n /** The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting)\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;;;AAuDA;AACA;AACA;AACA;AACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;AAED,SAASC,oBAAoBA,CAACC,SAA0B,EAAE;AACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;AAChD,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeE,yBAAyBA,CAAC;EAC9CC,WAAW;EACXC,SAAS;AACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;AAC5BC,EAAAA,MAAM,G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AK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;;;;"}
1
+ {"version":3,"file":"persist.js","sources":["../../src/persist.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient(persistClient: PersistedClient): Promisable<void>\n restoreClient(): Promisable<PersistedClient | undefined>\n removeClient(): Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\n /** The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting)\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","process","env","NODE_ENV","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then"],"mappings":";;;;AAuDA;AACA;AACA;AACA;AACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;AAED,SAASC,oBAAoBA,CAACC,SAA0B,EAAE;AACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;AAChD,CAAA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeE,yBAAyBA,CAAC;EAC9CC,WAAW;EACXC,SAAS;AACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;AAC5BC,EAAAA,MAAM,GAAG,EAAE;AACXC,EAAAA,cAAAA;AACkC,CAAC,EAAE;EACrC,IAAI;AACF,IAAA,MAAMC,eAAe,GAAG,MAAMJ,SAAS,CAACK,aAAa,EAAE,CAAA;AAEvD,IAAA,IAAID,eAAe,EAAE;MACnB,IAAIA,eAAe,CAACE,SAAS,EAAE;AAC7B,QAAA,MAAMC,OAAO,GAAGC,IA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C,SAAS,CAAEC,KAAK,IAAK;AACpB,IAAA,IAAInC,oBAAoB,CAACmC,KAAK,CAACC,IAAI,CAAC,EAAE;MACpCV,sBAAsB,CAACK,KAAK,CAAC,CAAA;AAC/B,KAAA;AACF,GAAC,CAAC,CAAA;AAEJ,EAAA,MAAMM,wBAAwB,GAAGN,KAAK,CAAC3B,WAAW,CAC/CkC,gBAAgB,EAAE,CAClBJ,SAAS,CAAEC,KAAK,IAAK;AACpB,IAAA,IAAInC,oBAAoB,CAACmC,KAAK,CAACC,IAAI,CAAC,EAAE;MACpCV,sBAAsB,CAACK,KAAK,CAAC,CAAA;AAC/B,KAAA;AACF,GAAC,CAAC,CAAA;AAEJ,EAAA,OAAO,MAAM;AACXC,IAAAA,qBAAqB,EAAE,CAAA;AACvBK,IAAAA,wBAAwB,EAAE,CAAA;GAC3B,CAAA;AACH,CAAA;;AAEA;AACA;AACA;AACO,SAASE,kBAAkBA,CAChCR,KAAgC,EACH;EAC7B,IAAIS,eAAe,GAAG,KAAK,CAAA;AAC3B,EAAA,IAAIC,6BAAuD,CAAA;EAC3D,MAAMC,WAAW,GAAGA,MAAM;AACxBF,IAAAA,eAAe,GAAG,IAAI,CAAA;AACtBC,IAAAA,6BAA6B,IAAI,CAAA;GAClC,CAAA;;AAED;EACA,MAAME,cAAc,GAAGxC,yBAAyB,CAAC4B,KAAK,CAAC,CAACa,IAAI,CAAC,MAAM;IACjE,IAAI,CAACJ,eAAe,EAAE;AACpB;AACAC,MAAAA,6BAA6B,GAAGX,2BAA2B,CAACC,KAAK,CAAC,CAAA;AACpE,KAAA;AACF,GAAC,CAAC,CAAA;AAEF,EAAA,OAAO,CAACW,WAAW,EAAEC,cAAc,CAAC,CAAA;AACtC;;;;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/query-persist-client-core",
3
- "version": "5.0.0-alpha.49",
3
+ "version": "5.0.0-alpha.50",
4
4
  "description": "Set of utilities for interacting with persisters, which can save your queryClient for later use",
5
5
  "author": "tannerlinsley",
6
6
  "license": "MIT",
@@ -25,7 +25,6 @@
25
25
  "sideEffects": false,
26
26
  "files": [
27
27
  "build/lib/*",
28
- "build/umd/*",
29
28
  "src"
30
29
  ],
31
30
  "dependencies": {
@@ -1,146 +0,0 @@
1
- (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@tanstack/query-core')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@tanstack/query-core'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.QueryPersistClientCore = {}, global.QueryCore));
5
- })(this, (function (exports, queryCore) { 'use strict';
6
-
7
- /**
8
- * Checks if emitted event is about cache change and not about observers.
9
- * Useful for persist, where we only want to trigger save when cache is changed.
10
- */
11
- const cacheableEventTypes = ['added', 'removed', 'updated'];
12
- function isCacheableEventType(eventType) {
13
- return cacheableEventTypes.includes(eventType);
14
- }
15
-
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({
23
- queryClient,
24
- persister,
25
- maxAge = 1000 * 60 * 60 * 24,
26
- buster = '',
27
- hydrateOptions
28
- }) {
29
- try {
30
- const persistedClient = await persister.restoreClient();
31
- if (persistedClient) {
32
- if (persistedClient.timestamp) {
33
- const expired = Date.now() - persistedClient.timestamp > maxAge;
34
- const busted = persistedClient.buster !== buster;
35
- if (expired || busted) {
36
- persister.removeClient();
37
- } else {
38
- queryCore.hydrate(queryClient, persistedClient.clientState, hydrateOptions);
39
- }
40
- } else {
41
- persister.removeClient();
42
- }
43
- }
44
- } catch (err) {
45
- {
46
- console.error(err);
47
- console.warn('Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.');
48
- }
49
- persister.removeClient();
50
- }
51
- }
52
-
53
- /**
54
- * Persists data from the QueryCache
55
- * - data dehydrated using dehydrateOptions
56
- * - data is persisted using persister.persistClient
57
- */
58
- async function persistQueryClientSave({
59
- queryClient,
60
- persister,
61
- buster = '',
62
- dehydrateOptions
63
- }) {
64
- const persistClient = {
65
- buster,
66
- timestamp: Date.now(),
67
- clientState: queryCore.dehydrate(queryClient, dehydrateOptions)
68
- };
69
- await persister.persistClient(persistClient);
70
- }
71
-
72
- /**
73
- * Subscribe to QueryCache and MutationCache updates (for persisting)
74
- * @returns an unsubscribe function (to discontinue monitoring)
75
- */
76
- function persistQueryClientSubscribe(props) {
77
- const unsubscribeQueryCache = props.queryClient.getQueryCache().subscribe(event => {
78
- if (isCacheableEventType(event.type)) {
79
- persistQueryClientSave(props);
80
- }
81
- });
82
- const unusbscribeMutationCache = props.queryClient.getMutationCache().subscribe(event => {
83
- if (isCacheableEventType(event.type)) {
84
- persistQueryClientSave(props);
85
- }
86
- });
87
- return () => {
88
- unsubscribeQueryCache();
89
- unusbscribeMutationCache();
90
- };
91
- }
92
-
93
- /**
94
- * Restores persisted data to QueryCache and persists further changes.
95
- */
96
- function persistQueryClient(props) {
97
- let hasUnsubscribed = false;
98
- let persistQueryClientUnsubscribe;
99
- const unsubscribe = () => {
100
- hasUnsubscribed = true;
101
- persistQueryClientUnsubscribe?.();
102
- };
103
-
104
- // Attempt restore
105
- const restorePromise = persistQueryClientRestore(props).then(() => {
106
- if (!hasUnsubscribed) {
107
- // Subscribe to changes in the query cache to trigger the save
108
- persistQueryClientUnsubscribe = persistQueryClientSubscribe(props);
109
- }
110
- });
111
- return [unsubscribe, restorePromise];
112
- }
113
-
114
- const removeOldestQuery = ({
115
- persistedClient
116
- }) => {
117
- const mutations = [...persistedClient.clientState.mutations];
118
- const queries = [...persistedClient.clientState.queries];
119
- const client = {
120
- ...persistedClient,
121
- clientState: {
122
- mutations,
123
- queries
124
- }
125
- };
126
-
127
- // sort queries by dataUpdatedAt (oldest first)
128
- const sortedQueries = [...queries].sort((a, b) => a.state.dataUpdatedAt - b.state.dataUpdatedAt);
129
-
130
- // clean oldest query
131
- if (sortedQueries.length > 0) {
132
- const oldestData = sortedQueries.shift();
133
- client.clientState.queries = queries.filter(q => q !== oldestData);
134
- return client;
135
- }
136
- return undefined;
137
- };
138
-
139
- exports.persistQueryClient = persistQueryClient;
140
- exports.persistQueryClientRestore = persistQueryClientRestore;
141
- exports.persistQueryClientSave = persistQueryClientSave;
142
- exports.persistQueryClientSubscribe = persistQueryClientSubscribe;
143
- exports.removeOldestQuery = removeOldestQuery;
144
-
145
- }));
146
- //# sourceMappingURL=index.development.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.development.js","sources":["../../src/persist.ts","../../src/retryStrategies.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient(persistClient: PersistedClient): Promisable<void>\n restoreClient(): Promisable<PersistedClient | undefined>\n removeClient(): Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\n /** The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting)\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n","import type { PersistedClient } from './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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","console","error","warn","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClient","hasUnsubscribed","persistQueryClientUnsubscribe","unsubscribe","restorePromise","then","removeOldestQuery","mutations","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q","undefined"],"mappings":";;;;;;EAuDA;EACA;EACA;EACA;EACA,MAAMA,mBAA2C,GAAG,CAClD,OAAO,EACP,SAAS,EACT,SAAS,CACV,CAAA;EAED,SAASC,oBAAoBA,CAACC,SAA0B,EAAE;EACxD,EAAA,OAAOF,mBAAmB,CAACG,QAAQ,CAACD,SAAS,CAAC,CAAA;EAChD,CAAA;;EAEA;EACA;EACA;EACA;EACA;EACA;EACO,eAAeE,yBAAyBA,CAAC;IAC9CC,WAAW;IACXC,SAAS;EACTC,EAAAA,MAAM,GAAG,IAAI,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;EAC5BC,EAAAA,MAAM,GAAG,EAAE;EACXC,EAAAA,cAAAA;EACkC,CAAC,EAAE;IACrC,IAAI;EACF,IAAA,MAAMC,eAAe,GAAG,MAAMJ,SAAS,CAACK,aAAa,EAAE,CAAA;EAEvD,IAAA,IAAID,eAAe,EAAE;QACnB,IAAIA,eAAe,CAACE,SAAS,EAAE;EAC7B,QAAA,MAAMC,OAAO,GAAGC,IAAI,CAACC,GAAG,EAAE,GAAGL,eAAe,CAACE,SAAS,GAAGL,MAAM,CAAA;EAC/D,QAAA,MAAMS,MAAM,GAAGN,eAAe,CAACF,MAAM,KAAKA,MAAM,CAAA;UAChD,IAAIK,OAAO,IAAIG,MAAM,EAAE;YACrBV,SAAS,CAACW,YAAY,EAAE,CAAA;EAC1B,SAAC,MAAM;YACLC,iBAAO,CAACb,WAAW,EAAEK,eAAe,CAACS,WAAW,EAAEV,cAAc,CAAC,CAAA;EACnE,SAAA;EACF,OAAC,MAAM;UACLH,SAAS,CAACW,YAAY,EAAE,CAAA;EAC1B,OAAA;EACF,KAAA;KACD,CAAC,OAAOG,GAAG,EAAE;EACZ,IAA2C;EACzCC,MAAAA,OAA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AAa,CAACQ,KAAK,EAAE,CAAA;EACxCT,IAAAA,MAAM,CAAC3B,WAAW,CAAC0B,OAAO,GAAGA,OAAO,CAACW,MAAM,CAAEC,CAAC,IAAKA,CAAC,KAAKH,UAAU,CAAC,CAAA;EACpE,IAAA,OAAOR,MAAM,CAAA;EACf,GAAA;EAEA,EAAA,OAAOY,SAAS,CAAA;EAClB;;;;;;;;;;;;"}
@@ -1,2 +0,0 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@tanstack/query-core")):"function"==typeof define&&define.amd?define(["exports","@tanstack/query-core"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).QueryPersistClientCore={},e.QueryCore)}(this,(function(e,t){"use strict";const i=["added","removed","updated"];function n(e){return i.includes(e)}async function r({queryClient:e,persister:i,maxAge:n=864e5,buster:r="",hydrateOptions:s}){try{const a=await i.restoreClient();if(a)if(a.timestamp){const u=Date.now()-a.timestamp>n,o=a.buster!==r;u||o?i.removeClient():t.hydrate(e,a.clientState,s)}else i.removeClient()}catch(e){i.removeClient()}}async function s({queryClient:e,persister:i,buster:n="",dehydrateOptions:r}){const s={buster:n,timestamp:Date.now(),clientState:t.dehydrate(e,r)};await i.persistClient(s)}function a(e){const t=e.queryClient.getQueryCache().subscribe((t=>{n(t.type)&&s(e)})),i=e.queryClient.getMutationCache().subscribe((t=>{n(t.type)&&s(e)}));return()=>{t(),i()}}e.persistQueryClient=function(e){let t,i=!1;return[()=>{i=!0,t?.()},r(e).then((()=>{i||(t=a(e))}))]},e.persistQueryClientRestore=r,e.persistQueryClientSave=s,e.persistQueryClientSubscribe=a,e.removeOldestQuery=({persistedClient:e})=>{const t=[...e.clientState.mutations],i=[...e.clientState.queries],n={...e,clientState:{mutations:t,queries:i}},r=[...i].sort(((e,t)=>e.state.dataUpdatedAt-t.state.dataUpdatedAt));if(r.length>0){const e=r.shift();return n.clientState.queries=i.filter((t=>t!==e)),n}}}));
2
- //# sourceMappingURL=index.production.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.production.js","sources":["../../src/persist.ts","../../src/retryStrategies.ts"],"sourcesContent":["import type {\n QueryClient,\n DehydratedState,\n DehydrateOptions,\n HydrateOptions,\n NotifyEventType,\n} from '@tanstack/query-core'\nimport { dehydrate, hydrate } from '@tanstack/query-core'\n\nexport type Promisable<T> = T | PromiseLike<T>\n\nexport interface Persister {\n persistClient(persistClient: PersistedClient): Promisable<void>\n restoreClient(): Promisable<PersistedClient | undefined>\n removeClient(): Promisable<void>\n}\n\nexport interface PersistedClient {\n timestamp: number\n buster: string\n clientState: DehydratedState\n}\n\nexport interface PersistQueryClienRootOptions {\n /** The QueryClient to persist */\n queryClient: QueryClient\n /** The Persister interface for storing and restoring the cache\n * to/from a persisted location */\n persister: Persister\n /** A unique string that can be used to forcefully\n * invalidate existing caches if they do not share the same buster string */\n buster?: string\n}\n\nexport interface PersistedQueryClientRestoreOptions\n extends PersistQueryClienRootOptions {\n /** The max-allowed age of the cache in milliseconds.\n * If a persisted cache is found that is older than this\n * time, it will be discarded */\n maxAge?: number\n /** The options passed to the hydrate function */\n hydrateOptions?: HydrateOptions\n}\n\nexport interface PersistedQueryClientSaveOptions\n extends PersistQueryClienRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends PersistedQueryClientRestoreOptions,\n PersistedQueryClientSaveOptions,\n PersistQueryClienRootOptions {}\n\n/**\n * Checks if emitted event is about cache change and not about observers.\n * Useful for persist, where we only want to trigger save when cache is changed.\n */\nconst cacheableEventTypes: Array<NotifyEventType> = [\n 'added',\n 'removed',\n 'updated',\n]\n\nfunction isCacheableEventType(eventType: NotifyEventType) {\n return cacheableEventTypes.includes(eventType)\n}\n\n/**\n * Restores persisted data to the QueryCache\n * - data obtained from persister.restoreClient\n * - data is hydrated using hydrateOptions\n * If data is expired, busted, empty, or throws, it runs persister.removeClient\n */\nexport async function persistQueryClientRestore({\n queryClient,\n persister,\n maxAge = 1000 * 60 * 60 * 24,\n buster = '',\n hydrateOptions,\n}: PersistedQueryClientRestoreOptions) {\n try {\n const persistedClient = await persister.restoreClient()\n\n if (persistedClient) {\n if (persistedClient.timestamp) {\n const expired = Date.now() - persistedClient.timestamp > maxAge\n const busted = persistedClient.buster !== buster\n if (expired || busted) {\n persister.removeClient()\n } else {\n hydrate(queryClient, persistedClient.clientState, hydrateOptions)\n }\n } else {\n persister.removeClient()\n }\n }\n } catch (err) {\n if (process.env.NODE_ENV !== 'production') {\n console.error(err)\n console.warn(\n 'Encountered an error attempting to restore client cache from persisted location. As a precaution, the persisted cache will be discarded.',\n )\n }\n persister.removeClient()\n }\n}\n\n/**\n * Persists data from the QueryCache\n * - data dehydrated using dehydrateOptions\n * - data is persisted using persister.persistClient\n */\nexport async function persistQueryClientSave({\n queryClient,\n persister,\n buster = '',\n dehydrateOptions,\n}: PersistedQueryClientSaveOptions) {\n const persistClient: PersistedClient = {\n buster,\n timestamp: Date.now(),\n clientState: dehydrate(queryClient, dehydrateOptions),\n }\n\n await persister.persistClient(persistClient)\n}\n\n/**\n * Subscribe to QueryCache and MutationCache updates (for persisting)\n * @returns an unsubscribe function (to discontinue monitoring)\n */\nexport function persistQueryClientSubscribe(\n props: PersistedQueryClientSaveOptions,\n) {\n const unsubscribeQueryCache = props.queryClient\n .getQueryCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n const unusbscribeMutationCache = props.queryClient\n .getMutationCache()\n .subscribe((event) => {\n if (isCacheableEventType(event.type)) {\n persistQueryClientSave(props)\n }\n })\n\n return () => {\n unsubscribeQueryCache()\n unusbscribeMutationCache()\n }\n}\n\n/**\n * Restores persisted data to QueryCache and persists further changes.\n */\nexport function persistQueryClient(\n props: PersistQueryClientOptions,\n): [() => void, Promise<void>] {\n let hasUnsubscribed = false\n let persistQueryClientUnsubscribe: (() => void) | undefined\n const unsubscribe = () => {\n hasUnsubscribed = true\n persistQueryClientUnsubscribe?.()\n }\n\n // Attempt restore\n const restorePromise = persistQueryClientRestore(props).then(() => {\n if (!hasUnsubscribed) {\n // Subscribe to changes in the query cache to trigger the save\n persistQueryClientUnsubscribe = persistQueryClientSubscribe(props)\n }\n })\n\n return [unsubscribe, restorePromise]\n}\n","import type { PersistedClient } from './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"],"names":["cacheableEventTypes","isCacheableEventType","eventType","includes","async","persistQueryClientRestore","queryClient","persister","maxAge","buster","hydrateOptions","persistedClient","restoreClient","timestamp","expired","Date","now","busted","removeClient","hydrate","clientState","err","persistQueryClientSave","dehydrateOptions","persistClient","dehydrate","persistQueryClientSubscribe","props","unsubscribeQueryCache","getQueryCache","subscribe","event","type","unusbscribeMutationCache","getMutationCache","persistQueryClientUnsubscribe","hasUnsubscribed","unsubscribe","then","removeOldestQuery","mutations","queries","client","sortedQueries","sort","a","b","state","dataUpdatedAt","length","oldestData","shift","filter","q"],"mappings":"mUA2DA,MAAMA,EAA8C,CAClD,QACA,UACA,WAGF,SAASC,EAAqBC,GAC5B,OAAOF,EAAoBG,SAASD,EACtC,CAQOE,eAAeC,GAA0BC,YAC9CA,EAAWC,UACXA,EAASC,OACTA,EAAS,MAAmBC,OAC5BA,EAAS,GAAEC,eACXA,IAEA,IACE,MAAMC,QAAwBJ,EAAUK,gBAEx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