@tanstack/query-persist-client-core 5.17.10 → 5.17.11

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.
@@ -1,4 +1,4 @@
1
- export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
1
+ export { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
2
2
  export { PersistRetryer, removeOldestQuery } from './retryStrategies.cjs';
3
3
  export { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.cjs';
4
4
  import '@tanstack/query-core';
@@ -1,4 +1,4 @@
1
- export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
1
+ export { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
2
2
  export { PersistRetryer, removeOldestQuery } from './retryStrategies.js';
3
3
  export { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.js';
4
4
  import '@tanstack/query-core';
@@ -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 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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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":[]}
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\n 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\n extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends 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 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,IAAM,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;AAC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AAA,EACF,CAAC;AAED,SAAO,CAAC,aAAa,cAAc;AACrC;","names":[]}
@@ -11,7 +11,7 @@ interface PersistedClient {
11
11
  buster: string;
12
12
  clientState: DehydratedState;
13
13
  }
14
- interface PersistQueryClienRootOptions {
14
+ interface PersistQueryClientRootOptions {
15
15
  /** The QueryClient to persist */
16
16
  queryClient: QueryClient;
17
17
  /** The Persister interface for storing and restoring the cache
@@ -21,7 +21,7 @@ interface PersistQueryClienRootOptions {
21
21
  * invalidate existing caches if they do not share the same buster string */
22
22
  buster?: string;
23
23
  }
24
- interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOptions {
24
+ interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
25
25
  /** The max-allowed age of the cache in milliseconds.
26
26
  * If a persisted cache is found that is older than this
27
27
  * time, it will be discarded */
@@ -29,11 +29,11 @@ interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOption
29
29
  /** The options passed to the hydrate function */
30
30
  hydrateOptions?: HydrateOptions;
31
31
  }
32
- interface PersistedQueryClientSaveOptions extends PersistQueryClienRootOptions {
32
+ interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
33
33
  /** The options passed to the dehydrate function */
34
34
  dehydrateOptions?: DehydrateOptions;
35
35
  }
36
- interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
36
+ interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {
37
37
  }
38
38
  /**
39
39
  * Restores persisted data to the QueryCache
@@ -58,4 +58,4 @@ declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOpti
58
58
  */
59
59
  declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
60
60
 
61
- export { type PersistQueryClienRootOptions, type PersistQueryClientOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
61
+ export { type PersistQueryClientOptions, type PersistQueryClientRootOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
@@ -11,7 +11,7 @@ interface PersistedClient {
11
11
  buster: string;
12
12
  clientState: DehydratedState;
13
13
  }
14
- interface PersistQueryClienRootOptions {
14
+ interface PersistQueryClientRootOptions {
15
15
  /** The QueryClient to persist */
16
16
  queryClient: QueryClient;
17
17
  /** The Persister interface for storing and restoring the cache
@@ -21,7 +21,7 @@ interface PersistQueryClienRootOptions {
21
21
  * invalidate existing caches if they do not share the same buster string */
22
22
  buster?: string;
23
23
  }
24
- interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOptions {
24
+ interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
25
25
  /** The max-allowed age of the cache in milliseconds.
26
26
  * If a persisted cache is found that is older than this
27
27
  * time, it will be discarded */
@@ -29,11 +29,11 @@ interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOption
29
29
  /** The options passed to the hydrate function */
30
30
  hydrateOptions?: HydrateOptions;
31
31
  }
32
- interface PersistedQueryClientSaveOptions extends PersistQueryClienRootOptions {
32
+ interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
33
33
  /** The options passed to the dehydrate function */
34
34
  dehydrateOptions?: DehydrateOptions;
35
35
  }
36
- interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
36
+ interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {
37
37
  }
38
38
  /**
39
39
  * Restores persisted data to the QueryCache
@@ -58,4 +58,4 @@ declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOpti
58
58
  */
59
59
  declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
60
60
 
61
- export { type PersistQueryClienRootOptions, type PersistQueryClientOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
61
+ export { type PersistQueryClientOptions, type PersistQueryClientRootOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
@@ -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 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,IAAM,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":[]}
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\n 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\n extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends 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 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,IAAM,sBAA8C;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AACF;A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,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":[]}
@@ -1,4 +1,4 @@
1
- export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
1
+ export { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.cjs';
2
2
  export { PersistRetryer, removeOldestQuery } from './retryStrategies.cjs';
3
3
  export { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.cjs';
4
4
  import '@tanstack/query-core';
@@ -1,4 +1,4 @@
1
- export { PersistQueryClienRootOptions, PersistQueryClientOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
1
+ export { PersistQueryClientOptions, PersistQueryClientRootOptions, PersistedClient, PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, Persister, Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe } from './persist.js';
2
2
  export { PersistRetryer, removeOldestQuery } from './retryStrategies.js';
3
3
  export { AsyncStorage, MaybePromise, PERSISTER_KEY_PREFIX, PersistedQuery, StoragePersisterOptions, experimental_createPersister } from './createPersister.js';
4
4
  import '@tanstack/query-core';
@@ -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 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,IAAM,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,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,"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\n 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\n extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends 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 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,IAAM,sBAA8C;AAAA,EAClD;AAAA,EACA;AAAA,EACA;AACF;AAEA,SAAS,qBAAqB,WAA4B;AACxD,SAAO,oBAAoB,SAAS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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":[]}
@@ -11,7 +11,7 @@ interface PersistedClient {
11
11
  buster: string;
12
12
  clientState: DehydratedState;
13
13
  }
14
- interface PersistQueryClienRootOptions {
14
+ interface PersistQueryClientRootOptions {
15
15
  /** The QueryClient to persist */
16
16
  queryClient: QueryClient;
17
17
  /** The Persister interface for storing and restoring the cache
@@ -21,7 +21,7 @@ interface PersistQueryClienRootOptions {
21
21
  * invalidate existing caches if they do not share the same buster string */
22
22
  buster?: string;
23
23
  }
24
- interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOptions {
24
+ interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
25
25
  /** The max-allowed age of the cache in milliseconds.
26
26
  * If a persisted cache is found that is older than this
27
27
  * time, it will be discarded */
@@ -29,11 +29,11 @@ interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOption
29
29
  /** The options passed to the hydrate function */
30
30
  hydrateOptions?: HydrateOptions;
31
31
  }
32
- interface PersistedQueryClientSaveOptions extends PersistQueryClienRootOptions {
32
+ interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
33
33
  /** The options passed to the dehydrate function */
34
34
  dehydrateOptions?: DehydrateOptions;
35
35
  }
36
- interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
36
+ interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {
37
37
  }
38
38
  /**
39
39
  * Restores persisted data to the QueryCache
@@ -58,4 +58,4 @@ declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOpti
58
58
  */
59
59
  declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
60
60
 
61
- export { type PersistQueryClienRootOptions, type PersistQueryClientOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
61
+ export { type PersistQueryClientOptions, type PersistQueryClientRootOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
@@ -11,7 +11,7 @@ interface PersistedClient {
11
11
  buster: string;
12
12
  clientState: DehydratedState;
13
13
  }
14
- interface PersistQueryClienRootOptions {
14
+ interface PersistQueryClientRootOptions {
15
15
  /** The QueryClient to persist */
16
16
  queryClient: QueryClient;
17
17
  /** The Persister interface for storing and restoring the cache
@@ -21,7 +21,7 @@ interface PersistQueryClienRootOptions {
21
21
  * invalidate existing caches if they do not share the same buster string */
22
22
  buster?: string;
23
23
  }
24
- interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOptions {
24
+ interface PersistedQueryClientRestoreOptions extends PersistQueryClientRootOptions {
25
25
  /** The max-allowed age of the cache in milliseconds.
26
26
  * If a persisted cache is found that is older than this
27
27
  * time, it will be discarded */
@@ -29,11 +29,11 @@ interface PersistedQueryClientRestoreOptions extends PersistQueryClienRootOption
29
29
  /** The options passed to the hydrate function */
30
30
  hydrateOptions?: HydrateOptions;
31
31
  }
32
- interface PersistedQueryClientSaveOptions extends PersistQueryClienRootOptions {
32
+ interface PersistedQueryClientSaveOptions extends PersistQueryClientRootOptions {
33
33
  /** The options passed to the dehydrate function */
34
34
  dehydrateOptions?: DehydrateOptions;
35
35
  }
36
- interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClienRootOptions {
36
+ interface PersistQueryClientOptions extends PersistedQueryClientRestoreOptions, PersistedQueryClientSaveOptions, PersistQueryClientRootOptions {
37
37
  }
38
38
  /**
39
39
  * Restores persisted data to the QueryCache
@@ -58,4 +58,4 @@ declare function persistQueryClientSubscribe(props: PersistedQueryClientSaveOpti
58
58
  */
59
59
  declare function persistQueryClient(props: PersistQueryClientOptions): [() => void, Promise<void>];
60
60
 
61
- export { type PersistQueryClienRootOptions, type PersistQueryClientOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
61
+ export { type PersistQueryClientOptions, type PersistQueryClientRootOptions, type PersistedClient, type PersistedQueryClientRestoreOptions, type PersistedQueryClientSaveOptions, type Persister, type Promisable, persistQueryClient, persistQueryClientRestore, persistQueryClientSave, persistQueryClientSubscribe };
@@ -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 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,IAAM,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;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A,IACnE;AAAA,EACF,CAAC;AAED,SAAO,CAAC,aAAa,cAAc;AACrC;","names":[]}
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\n 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\n extends PersistQueryClientRootOptions {\n /** The options passed to the dehydrate function */\n dehydrateOptions?: DehydrateOptions\n}\n\nexport interface PersistQueryClientOptions\n extends 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 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,IAAM,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,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":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/query-persist-client-core",
3
- "version": "5.17.10",
3
+ "version": "5.17.11",
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",
package/src/persist.ts CHANGED
@@ -21,7 +21,7 @@ export interface PersistedClient {
21
21
  clientState: DehydratedState
22
22
  }
23
23
 
24
- export interface PersistQueryClienRootOptions {
24
+ export interface PersistQueryClientRootOptions {
25
25
  /** The QueryClient to persist */
26
26
  queryClient: QueryClient
27
27
  /** The Persister interface for storing and restoring the cache
@@ -33,7 +33,7 @@ export interface PersistQueryClienRootOptions {
33
33
  }
34
34
 
35
35
  export interface PersistedQueryClientRestoreOptions
36
- extends PersistQueryClienRootOptions {
36
+ extends PersistQueryClientRootOptions {
37
37
  /** The max-allowed age of the cache in milliseconds.
38
38
  * If a persisted cache is found that is older than this
39
39
  * time, it will be discarded */
@@ -43,7 +43,7 @@ export interface PersistedQueryClientRestoreOptions
43
43
  }
44
44
 
45
45
  export interface PersistedQueryClientSaveOptions
46
- extends PersistQueryClienRootOptions {
46
+ extends PersistQueryClientRootOptions {
47
47
  /** The options passed to the dehydrate function */
48
48
  dehydrateOptions?: DehydrateOptions
49
49
  }
@@ -51,7 +51,7 @@ export interface PersistedQueryClientSaveOptions
51
51
  export interface PersistQueryClientOptions
52
52
  extends PersistedQueryClientRestoreOptions,
53
53
  PersistedQueryClientSaveOptions,
54
- PersistQueryClienRootOptions {}
54
+ PersistQueryClientRootOptions {}
55
55
 
56
56
  /**
57
57
  * Checks if emitted event is about cache change and not about observers.