@tanstack/query-async-storage-persister 5.0.0-alpha.86 → 5.0.0-alpha.88

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/build/legacy/asyncThrottle.cjs +72 -0
  2. package/build/legacy/asyncThrottle.cjs.map +1 -0
  3. package/build/legacy/asyncThrottle.d.cts +7 -0
  4. package/build/legacy/asyncThrottle.d.ts +7 -0
  5. package/build/{lib → legacy}/asyncThrottle.js +9 -11
  6. package/build/legacy/asyncThrottle.js.map +1 -0
  7. package/build/legacy/index.cjs +84 -0
  8. package/build/legacy/index.cjs.map +1 -0
  9. package/build/legacy/index.d.cts +38 -0
  10. package/build/{lib → legacy}/index.d.ts +6 -5
  11. package/build/legacy/index.js +60 -0
  12. package/build/legacy/index.js.map +1 -0
  13. package/build/modern/asyncThrottle.cjs +72 -0
  14. package/build/modern/asyncThrottle.cjs.map +1 -0
  15. package/build/modern/asyncThrottle.d.cts +7 -0
  16. package/build/modern/asyncThrottle.d.ts +7 -0
  17. package/build/{lib/asyncThrottle.legacy.js → modern/asyncThrottle.js} +9 -11
  18. package/build/modern/asyncThrottle.js.map +1 -0
  19. package/build/modern/index.cjs +84 -0
  20. package/build/modern/index.cjs.map +1 -0
  21. package/build/modern/index.d.cts +38 -0
  22. package/build/modern/index.d.ts +38 -0
  23. package/build/modern/index.js +60 -0
  24. package/build/modern/index.js.map +1 -0
  25. package/package.json +17 -15
  26. package/build/lib/__tests__/asyncThrottle.test.d.ts +0 -2
  27. package/build/lib/__tests__/asyncThrottle.test.d.ts.map +0 -1
  28. package/build/lib/__tests__/utils.d.ts +0 -2
  29. package/build/lib/__tests__/utils.d.ts.map +0 -1
  30. package/build/lib/asyncThrottle.cjs +0 -52
  31. package/build/lib/asyncThrottle.cjs.map +0 -1
  32. package/build/lib/asyncThrottle.d.ts +0 -6
  33. package/build/lib/asyncThrottle.d.ts.map +0 -1
  34. package/build/lib/asyncThrottle.js.map +0 -1
  35. package/build/lib/asyncThrottle.legacy.cjs +0 -52
  36. package/build/lib/asyncThrottle.legacy.cjs.map +0 -1
  37. package/build/lib/asyncThrottle.legacy.js.map +0 -1
  38. package/build/lib/index.cjs +0 -62
  39. package/build/lib/index.cjs.map +0 -1
  40. package/build/lib/index.d.ts.map +0 -1
  41. package/build/lib/index.js +0 -60
  42. package/build/lib/index.js.map +0 -1
  43. package/build/lib/index.legacy.cjs +0 -62
  44. package/build/lib/index.legacy.cjs.map +0 -1
  45. package/build/lib/index.legacy.js +0 -60
  46. package/build/lib/index.legacy.js.map +0 -1
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var asyncThrottle_exports = {};
20
+ __export(asyncThrottle_exports, {
21
+ asyncThrottle: () => asyncThrottle
22
+ });
23
+ module.exports = __toCommonJS(asyncThrottle_exports);
24
+ const noop = () => {
25
+ };
26
+ function asyncThrottle(func, { interval = 1e3, onError = noop } = {}) {
27
+ if (typeof func !== "function")
28
+ throw new Error("argument is not function.");
29
+ let running = false;
30
+ let lastTime = 0;
31
+ let timeout;
32
+ let currentArgs = null;
33
+ const execFunc = async () => {
34
+ if (currentArgs) {
35
+ const args = currentArgs;
36
+ currentArgs = null;
37
+ try {
38
+ running = true;
39
+ await func(...args);
40
+ } catch (error) {
41
+ onError(error);
42
+ } finally {
43
+ lastTime = Date.now();
44
+ running = false;
45
+ }
46
+ }
47
+ };
48
+ const delayFunc = async () => {
49
+ clearTimeout(timeout);
50
+ timeout = setTimeout(() => {
51
+ if (running) {
52
+ delayFunc();
53
+ } else {
54
+ execFunc();
55
+ }
56
+ }, interval);
57
+ };
58
+ return (...args) => {
59
+ currentArgs = args;
60
+ const tooSoon = Date.now() - lastTime < interval;
61
+ if (running || tooSoon) {
62
+ delayFunc();
63
+ } else {
64
+ execFunc();
65
+ }
66
+ };
67
+ }
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ asyncThrottle
71
+ });
72
+ //# sourceMappingURL=asyncThrottle.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["export interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nconst noop = () => {\n /* do nothing */\n}\n\nexport function asyncThrottle<Args extends readonly unknown[]>(\n func: (...args: Args) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: Args | null = null\n\n const execFunc = async () => {\n if (currentArgs) {\n const args = currentArgs\n currentArgs = null\n try {\n running = true\n await func(...args)\n } catch (error) {\n onError(error)\n } finally {\n lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.\n running = false\n }\n }\n }\n\n const delayFunc = async () => {\n clearTimeout(timeout)\n timeout = setTimeout(() => {\n if (running) {\n delayFunc() // Will come here when 'func' execution time is greater than the interval.\n } else {\n execFunc()\n }\n }, interval)\n }\n\n return (...args: Args) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,MAAM,OAAO,MAAM;AAEnB;AAEO,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,KAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA2B;AAE/B,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAe;AACxB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,7 @@
1
+ interface AsyncThrottleOptions {
2
+ interval?: number;
3
+ onError?: (error: unknown) => void;
4
+ }
5
+ declare function asyncThrottle<Args extends readonly unknown[]>(func: (...args: Args) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: Args) => void;
6
+
7
+ export { AsyncThrottleOptions, asyncThrottle };
@@ -0,0 +1,7 @@
1
+ interface AsyncThrottleOptions {
2
+ interval?: number;
3
+ onError?: (error: unknown) => void;
4
+ }
5
+ declare function asyncThrottle<Args extends readonly unknown[]>(func: (...args: Args) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: Args) => void;
6
+
7
+ export { AsyncThrottleOptions, asyncThrottle };
@@ -1,11 +1,8 @@
1
1
  const noop = () => {
2
- /* do nothing */
3
2
  };
4
- function asyncThrottle(func, {
5
- interval = 1000,
6
- onError = noop
7
- } = {}) {
8
- if (typeof func !== 'function') throw new Error('argument is not function.');
3
+ function asyncThrottle(func, { interval = 1e3, onError = noop } = {}) {
4
+ if (typeof func !== "function")
5
+ throw new Error("argument is not function.");
9
6
  let running = false;
10
7
  let lastTime = 0;
11
8
  let timeout;
@@ -20,7 +17,7 @@ function asyncThrottle(func, {
20
17
  } catch (error) {
21
18
  onError(error);
22
19
  } finally {
23
- lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
20
+ lastTime = Date.now();
24
21
  running = false;
25
22
  }
26
23
  }
@@ -29,7 +26,7 @@ function asyncThrottle(func, {
29
26
  clearTimeout(timeout);
30
27
  timeout = setTimeout(() => {
31
28
  if (running) {
32
- delayFunc(); // Will come here when 'func' execution time is greater than the interval.
29
+ delayFunc();
33
30
  } else {
34
31
  execFunc();
35
32
  }
@@ -45,6 +42,7 @@ function asyncThrottle(func, {
45
42
  }
46
43
  };
47
44
  }
48
-
49
- export { asyncThrottle };
50
- //# sourceMappingURL=asyncThrottle.js.map
45
+ export {
46
+ asyncThrottle
47
+ };
48
+ //# sourceMappingURL=asyncThrottle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["export interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nconst noop = () => {\n /* do nothing */\n}\n\nexport function asyncThrottle<Args extends readonly unknown[]>(\n func: (...args: Args) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: Args | null = null\n\n const execFunc = async () => {\n if (currentArgs) {\n const args = currentArgs\n currentArgs = null\n try {\n running = true\n await func(...args)\n } catch (error) {\n onError(error)\n } finally {\n lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.\n running = false\n }\n }\n }\n\n const delayFunc = async () => {\n clearTimeout(timeout)\n timeout = setTimeout(() => {\n if (running) {\n delayFunc() // Will come here when 'func' execution time is greater than the interval.\n } else {\n execFunc()\n }\n }, interval)\n }\n\n return (...args: Args) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":"AAKA,MAAM,OAAO,MAAM;AAEnB;AAEO,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,KAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA2B;AAE/B,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAe;AACxB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var src_exports = {};
20
+ __export(src_exports, {
21
+ createAsyncStoragePersister: () => createAsyncStoragePersister
22
+ });
23
+ module.exports = __toCommonJS(src_exports);
24
+ var import_asyncThrottle = require("./asyncThrottle");
25
+ const createAsyncStoragePersister = ({
26
+ storage,
27
+ key = `REACT_QUERY_OFFLINE_CACHE`,
28
+ throttleTime = 1e3,
29
+ serialize = JSON.stringify,
30
+ deserialize = JSON.parse,
31
+ retry
32
+ }) => {
33
+ if (storage) {
34
+ const trySave = async (persistedClient) => {
35
+ try {
36
+ await storage.setItem(key, serialize(persistedClient));
37
+ return;
38
+ } catch (error) {
39
+ return error;
40
+ }
41
+ };
42
+ return {
43
+ persistClient: (0, import_asyncThrottle.asyncThrottle)(
44
+ async (persistedClient) => {
45
+ let client = persistedClient;
46
+ let error = await trySave(client);
47
+ let errorCount = 0;
48
+ while (error && client) {
49
+ errorCount++;
50
+ client = await (retry == null ? void 0 : retry({
51
+ persistedClient: client,
52
+ error,
53
+ errorCount
54
+ }));
55
+ if (client) {
56
+ error = await trySave(client);
57
+ }
58
+ }
59
+ },
60
+ { interval: throttleTime }
61
+ ),
62
+ restoreClient: async () => {
63
+ const cacheString = await storage.getItem(key);
64
+ if (!cacheString) {
65
+ return;
66
+ }
67
+ return deserialize(cacheString);
68
+ },
69
+ removeClient: () => storage.removeItem(key)
70
+ };
71
+ }
72
+ return {
73
+ persistClient: noop,
74
+ restoreClient: () => Promise.resolve(void 0),
75
+ removeClient: noop
76
+ };
77
+ };
78
+ function noop() {
79
+ }
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ createAsyncStoragePersister
83
+ });
84
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { asyncThrottle } from './asyncThrottle'\nimport type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\n\ninterface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport type AsyncPersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => Promisable<PersistedClient | undefined>\n\ninterface CreateAsyncStoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`. Note that window.localStorage can be\n * `null` in Android WebViews depending on how they are configured.\n */\n storage: AsyncStorage | undefined | null\n /** The key to use when storing the cache */\n key?: string\n /** To avoid spamming,\n * pass a time in ms to throttle saving the cache to disk */\n throttleTime?: number\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (client: PersistedClient) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedClient\n\n retry?: AsyncPersistRetryer\n}\n\nexport const createAsyncStoragePersister = ({\n storage,\n key = `REACT_QUERY_OFFLINE_CACHE`,\n throttleTime = 1000,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n retry,\n}: CreateAsyncStoragePersisterOptions): Persister => {\n if (storage) {\n const trySave = async (\n persistedClient: PersistedClient,\n ): Promise<Error | undefined> => {\n try {\n await storage.setItem(key, serialize(persistedClient))\n return\n } catch (error) {\n return error as Error\n }\n }\n\n return {\n persistClient: asyncThrottle(\n async (persistedClient) => {\n let client: PersistedClient | undefined = persistedClient\n let error = await trySave(client)\n let errorCount = 0\n while (error && client) {\n errorCount++\n client = await retry?.({\n persistedClient: client,\n error,\n errorCount,\n })\n\n if (client) {\n error = await trySave(client)\n }\n }\n },\n { interval: throttleTime },\n ),\n restoreClient: async () => {\n const cacheString = await storage.getItem(key)\n\n if (!cacheString) {\n return\n }\n\n return deserialize(cacheString)\n },\n removeClient: () => storage.removeItem(key),\n }\n }\n\n return {\n persistClient: noop,\n restoreClient: () => Promise.resolve(undefined),\n removeClient: noop,\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nfunction noop() {}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA8B;AA4CvB,MAAM,8BAA8B,CAAC;AAAA,EAC1C;AAAA,EACA,MAAM;AAAA,EACN,eAAe;AAAA,EACf,YAAY,KAAK;AAAA,EACjB,cAAc,KAAK;AAAA,EACnB;AACF,MAAqD;AACnD,MAAI,SAAS;AACX,UAAM,UAAU,OACd,oBAC+B;AAC/B,UAAI;AACF,cAAM,QAAQ,QAAQ,KAAK,UAAU,eAAe,CAAC;AACrD;AAAA,MACF,SAAS,OAAO;AACd,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,MACL,mBAAe;AAAA,QACb,OAAO,oBAAoB;AACzB,cAAI,SAAsC;AAC1C,cAAI,QAAQ,MAAM,QAAQ,MAAM;AAChC,cAAI,aAAa;AACjB,iBAAO,SAAS,QAAQ;AACtB;AACA,qBAAS,OAAM,+BAAQ;AAAA,cACrB,iBAAiB;AAAA,cACjB;AAAA,cACA;AAAA,YACF;AAEA,gBAAI,QAAQ;AACV,sBAAQ,MAAM,QAAQ,MAAM;AAAA,YAC9B;AAAA,UACF;AAAA,QACF;AAAA,QACA,EAAE,UAAU,aAAa;AAAA,MAC3B;AAAA,MACA,eAAe,YAAY;AACzB,cAAM,cAAc,MAAM,QAAQ,QAAQ,GAAG;AAE7C,YAAI,CAAC,aAAa;AAChB;AAAA,QACF;AAEA,eAAO,YAAY,WAAW;AAAA,MAChC;AAAA,MACA,cAAc,MAAM,QAAQ,WAAW,GAAG;AAAA,IAC5C;AAAA,EACF;AAEA,SAAO;AAAA,IACL,eAAe;AAAA,IACf,eAAe,MAAM,QAAQ,QAAQ,MAAS;AAAA,IAC9C,cAAc;AAAA,EAChB;AACF;AAGA,SAAS,OAAO;AAAC;","names":[]}
@@ -0,0 +1,38 @@
1
+ import { PersistedClient, Promisable, Persister } from '@tanstack/query-persist-client-core';
2
+
3
+ interface AsyncStorage {
4
+ getItem: (key: string) => Promise<string | null>;
5
+ setItem: (key: string, value: string) => Promise<unknown>;
6
+ removeItem: (key: string) => Promise<void>;
7
+ }
8
+ type AsyncPersistRetryer = (props: {
9
+ persistedClient: PersistedClient;
10
+ error: Error;
11
+ errorCount: number;
12
+ }) => Promisable<PersistedClient | undefined>;
13
+ interface CreateAsyncStoragePersisterOptions {
14
+ /** The storage client used for setting and retrieving items from cache.
15
+ * For SSR pass in `undefined`. Note that window.localStorage can be
16
+ * `null` in Android WebViews depending on how they are configured.
17
+ */
18
+ storage: AsyncStorage | undefined | null;
19
+ /** The key to use when storing the cache */
20
+ key?: string;
21
+ /** To avoid spamming,
22
+ * pass a time in ms to throttle saving the cache to disk */
23
+ throttleTime?: number;
24
+ /**
25
+ * How to serialize the data to storage.
26
+ * @default `JSON.stringify`
27
+ */
28
+ serialize?: (client: PersistedClient) => string;
29
+ /**
30
+ * How to deserialize the data from storage.
31
+ * @default `JSON.parse`
32
+ */
33
+ deserialize?: (cachedString: string) => PersistedClient;
34
+ retry?: AsyncPersistRetryer;
35
+ }
36
+ declare const createAsyncStoragePersister: ({ storage, key, throttleTime, serialize, deserialize, retry, }: CreateAsyncStoragePersisterOptions) => Persister;
37
+
38
+ export { AsyncPersistRetryer, createAsyncStoragePersister };
@@ -1,10 +1,11 @@
1
- import type { PersistedClient, Persister, Promisable } from '@tanstack/query-persist-client-core';
1
+ import { PersistedClient, Promisable, Persister } from '@tanstack/query-persist-client-core';
2
+
2
3
  interface AsyncStorage {
3
4
  getItem: (key: string) => Promise<string | null>;
4
5
  setItem: (key: string, value: string) => Promise<unknown>;
5
6
  removeItem: (key: string) => Promise<void>;
6
7
  }
7
- export type AsyncPersistRetryer = (props: {
8
+ type AsyncPersistRetryer = (props: {
8
9
  persistedClient: PersistedClient;
9
10
  error: Error;
10
11
  errorCount: number;
@@ -32,6 +33,6 @@ interface CreateAsyncStoragePersisterOptions {
32
33
  deserialize?: (cachedString: string) => PersistedClient;
33
34
  retry?: AsyncPersistRetryer;
34
35
  }
35
- export declare const createAsyncStoragePersister: ({ storage, key, throttleTime, serialize, deserialize, retry, }: CreateAsyncStoragePersisterOptions) => Persister;
36
- export {};
37
- //# sourceMappingURL=index.d.ts.map
36
+ declare const createAsyncStoragePersister: ({ storage, key, throttleTime, serialize, deserialize, retry, }: CreateAsyncStoragePersisterOptions) => Persister;
37
+
38
+ export { AsyncPersistRetryer, createAsyncStoragePersister };
@@ -0,0 +1,60 @@
1
+ import { asyncThrottle } from "./asyncThrottle";
2
+ const createAsyncStoragePersister = ({
3
+ storage,
4
+ key = `REACT_QUERY_OFFLINE_CACHE`,
5
+ throttleTime = 1e3,
6
+ serialize = JSON.stringify,
7
+ deserialize = JSON.parse,
8
+ retry
9
+ }) => {
10
+ if (storage) {
11
+ const trySave = async (persistedClient) => {
12
+ try {
13
+ await storage.setItem(key, serialize(persistedClient));
14
+ return;
15
+ } catch (error) {
16
+ return error;
17
+ }
18
+ };
19
+ return {
20
+ persistClient: asyncThrottle(
21
+ async (persistedClient) => {
22
+ let client = persistedClient;
23
+ let error = await trySave(client);
24
+ let errorCount = 0;
25
+ while (error && client) {
26
+ errorCount++;
27
+ client = await (retry == null ? void 0 : retry({
28
+ persistedClient: client,
29
+ error,
30
+ errorCount
31
+ }));
32
+ if (client) {
33
+ error = await trySave(client);
34
+ }
35
+ }
36
+ },
37
+ { interval: throttleTime }
38
+ ),
39
+ restoreClient: async () => {
40
+ const cacheString = await storage.getItem(key);
41
+ if (!cacheString) {
42
+ return;
43
+ }
44
+ return deserialize(cacheString);
45
+ },
46
+ removeClient: () => storage.removeItem(key)
47
+ };
48
+ }
49
+ return {
50
+ persistClient: noop,
51
+ restoreClient: () => Promise.resolve(void 0),
52
+ removeClient: noop
53
+ };
54
+ };
55
+ function noop() {
56
+ }
57
+ export {
58
+ createAsyncStoragePersister
59
+ };
60
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { asyncThrottle } from './asyncThrottle'\nimport type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\n\ninterface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport type AsyncPersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => Promisable<PersistedClient | undefined>\n\ninterface CreateAsyncStoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`. Note that window.localStorage can be\n * `null` in Android WebViews depending on how they are configured.\n */\n storage: AsyncStorage | undefined | null\n /** The key to use when storing the cache */\n key?: string\n /** To avoid spamming,\n * pass a time in ms to throttle saving the cache to disk */\n throttleTime?: number\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (client: PersistedClient) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedClient\n\n retry?: AsyncPersistRetryer\n}\n\nexport const createAsyncStoragePersister = ({\n storage,\n key = `REACT_QUERY_OFFLINE_CACHE`,\n throttleTime = 1000,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n retry,\n}: CreateAsyncStoragePersisterOptions): Persister => {\n if (storage) {\n const trySave = async (\n persistedClient: PersistedClient,\n ): Promise<Error | undefined> => {\n try {\n await storage.setItem(key, serialize(persistedClient))\n return\n } catch (error) {\n return error as Error\n }\n }\n\n return {\n persistClient: asyncThrottle(\n async (persistedClient) => {\n let client: PersistedClient | undefined = persistedClient\n let error = await trySave(client)\n let errorCount = 0\n while (error && client) {\n errorCount++\n client = await retry?.({\n persistedClient: client,\n error,\n errorCount,\n })\n\n if (client) {\n error = await trySave(client)\n }\n }\n },\n { interval: throttleTime },\n ),\n restoreClient: async () => {\n const cacheString = await storage.getItem(key)\n\n if (!cacheString) {\n return\n }\n\n return deserialize(cacheString)\n },\n removeClient: () => storage.removeItem(key),\n }\n }\n\n return {\n persistClient: noop,\n restoreClient: () => Promise.resolve(undefined),\n removeClient: noop,\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nfunction noop() {}\n"],"mappings":"AAAA,SAAS,qBAAqB;AA4CvB,MAAM,8BAA8B,CAAC;AAAA,EAC1C;AAAA,EACA,MAAM;AAAA,EACN,eAAe;AAAA,EACf,YAAY,KAAK;AAAA,EACjB,cAAc,KAAK;AAAA,EACnB;AACF,MAAqD;AACnD,MAAI,SAAS;AACX,UAAM,UAAU,OACd,oBAC+B;AAC/B,UAAI;AACF,cAAM,QAAQ,QAAQ,KAAK,UAAU,eAAe,CAAC;AACrD;AAAA,MACF,SAAS,OAAO;AACd,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,MACL,eAAe;AAAA,QACb,OAAO,oBAAoB;AACzB,cAAI,SAAsC;AAC1C,cAAI,QAAQ,MAAM,QAAQ,MAAM;AAChC,cAAI,aAAa;AACjB,iBAAO,SAAS,QAAQ;AACtB;AACA,qBAAS,OAAM,+BAAQ;AAAA,cACrB,iBAAiB;AAAA,cACjB;AAAA,cACA;AAAA,YACF;AAEA,gBAAI,QAAQ;AACV,sBAAQ,MAAM,QAAQ,MAAM;AAAA,YAC9B;AAAA,UACF;AAAA,QACF;AAAA,QACA,EAAE,UAAU,aAAa;AAAA,MAC3B;AAAA,MACA,eAAe,YAAY;AACzB,cAAM,cAAc,MAAM,QAAQ,QAAQ,GAAG;AAE7C,YAAI,CAAC,aAAa;AAChB;AAAA,QACF;AAEA,eAAO,YAAY,WAAW;AAAA,MAChC;AAAA,MACA,cAAc,MAAM,QAAQ,WAAW,GAAG;AAAA,IAC5C;AAAA,EACF;AAEA,SAAO;AAAA,IACL,eAAe;AAAA,IACf,eAAe,MAAM,QAAQ,QAAQ,MAAS;AAAA,IAC9C,cAAc;AAAA,EAChB;AACF;AAGA,SAAS,OAAO;AAAC;","names":[]}
@@ -0,0 +1,72 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var asyncThrottle_exports = {};
20
+ __export(asyncThrottle_exports, {
21
+ asyncThrottle: () => asyncThrottle
22
+ });
23
+ module.exports = __toCommonJS(asyncThrottle_exports);
24
+ const noop = () => {
25
+ };
26
+ function asyncThrottle(func, { interval = 1e3, onError = noop } = {}) {
27
+ if (typeof func !== "function")
28
+ throw new Error("argument is not function.");
29
+ let running = false;
30
+ let lastTime = 0;
31
+ let timeout;
32
+ let currentArgs = null;
33
+ const execFunc = async () => {
34
+ if (currentArgs) {
35
+ const args = currentArgs;
36
+ currentArgs = null;
37
+ try {
38
+ running = true;
39
+ await func(...args);
40
+ } catch (error) {
41
+ onError(error);
42
+ } finally {
43
+ lastTime = Date.now();
44
+ running = false;
45
+ }
46
+ }
47
+ };
48
+ const delayFunc = async () => {
49
+ clearTimeout(timeout);
50
+ timeout = setTimeout(() => {
51
+ if (running) {
52
+ delayFunc();
53
+ } else {
54
+ execFunc();
55
+ }
56
+ }, interval);
57
+ };
58
+ return (...args) => {
59
+ currentArgs = args;
60
+ const tooSoon = Date.now() - lastTime < interval;
61
+ if (running || tooSoon) {
62
+ delayFunc();
63
+ } else {
64
+ execFunc();
65
+ }
66
+ };
67
+ }
68
+ // Annotate the CommonJS export names for ESM import in node:
69
+ 0 && (module.exports = {
70
+ asyncThrottle
71
+ });
72
+ //# sourceMappingURL=asyncThrottle.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["export interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nconst noop = () => {\n /* do nothing */\n}\n\nexport function asyncThrottle<Args extends readonly unknown[]>(\n func: (...args: Args) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: Args | null = null\n\n const execFunc = async () => {\n if (currentArgs) {\n const args = currentArgs\n currentArgs = null\n try {\n running = true\n await func(...args)\n } catch (error) {\n onError(error)\n } finally {\n lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.\n running = false\n }\n }\n }\n\n const delayFunc = async () => {\n clearTimeout(timeout)\n timeout = setTimeout(() => {\n if (running) {\n delayFunc() // Will come here when 'func' execution time is greater than the interval.\n } else {\n execFunc()\n }\n }, interval)\n }\n\n return (...args: Args) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,MAAM,OAAO,MAAM;AAEnB;AAEO,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,KAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA2B;AAE/B,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAe;AACxB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,7 @@
1
+ interface AsyncThrottleOptions {
2
+ interval?: number;
3
+ onError?: (error: unknown) => void;
4
+ }
5
+ declare function asyncThrottle<Args extends readonly unknown[]>(func: (...args: Args) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: Args) => void;
6
+
7
+ export { AsyncThrottleOptions, asyncThrottle };
@@ -0,0 +1,7 @@
1
+ interface AsyncThrottleOptions {
2
+ interval?: number;
3
+ onError?: (error: unknown) => void;
4
+ }
5
+ declare function asyncThrottle<Args extends readonly unknown[]>(func: (...args: Args) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: Args) => void;
6
+
7
+ export { AsyncThrottleOptions, asyncThrottle };
@@ -1,11 +1,8 @@
1
1
  const noop = () => {
2
- /* do nothing */
3
2
  };
4
- function asyncThrottle(func, {
5
- interval = 1000,
6
- onError = noop
7
- } = {}) {
8
- if (typeof func !== 'function') throw new Error('argument is not function.');
3
+ function asyncThrottle(func, { interval = 1e3, onError = noop } = {}) {
4
+ if (typeof func !== "function")
5
+ throw new Error("argument is not function.");
9
6
  let running = false;
10
7
  let lastTime = 0;
11
8
  let timeout;
@@ -20,7 +17,7 @@ function asyncThrottle(func, {
20
17
  } catch (error) {
21
18
  onError(error);
22
19
  } finally {
23
- lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
20
+ lastTime = Date.now();
24
21
  running = false;
25
22
  }
26
23
  }
@@ -29,7 +26,7 @@ function asyncThrottle(func, {
29
26
  clearTimeout(timeout);
30
27
  timeout = setTimeout(() => {
31
28
  if (running) {
32
- delayFunc(); // Will come here when 'func' execution time is greater than the interval.
29
+ delayFunc();
33
30
  } else {
34
31
  execFunc();
35
32
  }
@@ -45,6 +42,7 @@ function asyncThrottle(func, {
45
42
  }
46
43
  };
47
44
  }
48
-
49
- export { asyncThrottle };
50
- //# sourceMappingURL=asyncThrottle.legacy.js.map
45
+ export {
46
+ asyncThrottle
47
+ };
48
+ //# sourceMappingURL=asyncThrottle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["export interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nconst noop = () => {\n /* do nothing */\n}\n\nexport function asyncThrottle<Args extends readonly unknown[]>(\n func: (...args: Args) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: Args | null = null\n\n const execFunc = async () => {\n if (currentArgs) {\n const args = currentArgs\n currentArgs = null\n try {\n running = true\n await func(...args)\n } catch (error) {\n onError(error)\n } finally {\n lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.\n running = false\n }\n }\n }\n\n const delayFunc = async () => {\n clearTimeout(timeout)\n timeout = setTimeout(() => {\n if (running) {\n delayFunc() // Will come here when 'func' execution time is greater than the interval.\n } else {\n execFunc()\n }\n }, interval)\n }\n\n return (...args: Args) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":"AAKA,MAAM,OAAO,MAAM;AAEnB;AAEO,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,KAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA2B;AAE/B,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAe;AACxB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var src_exports = {};
20
+ __export(src_exports, {
21
+ createAsyncStoragePersister: () => createAsyncStoragePersister
22
+ });
23
+ module.exports = __toCommonJS(src_exports);
24
+ var import_asyncThrottle = require("./asyncThrottle");
25
+ const createAsyncStoragePersister = ({
26
+ storage,
27
+ key = `REACT_QUERY_OFFLINE_CACHE`,
28
+ throttleTime = 1e3,
29
+ serialize = JSON.stringify,
30
+ deserialize = JSON.parse,
31
+ retry
32
+ }) => {
33
+ if (storage) {
34
+ const trySave = async (persistedClient) => {
35
+ try {
36
+ await storage.setItem(key, serialize(persistedClient));
37
+ return;
38
+ } catch (error) {
39
+ return error;
40
+ }
41
+ };
42
+ return {
43
+ persistClient: (0, import_asyncThrottle.asyncThrottle)(
44
+ async (persistedClient) => {
45
+ let client = persistedClient;
46
+ let error = await trySave(client);
47
+ let errorCount = 0;
48
+ while (error && client) {
49
+ errorCount++;
50
+ client = await retry?.({
51
+ persistedClient: client,
52
+ error,
53
+ errorCount
54
+ });
55
+ if (client) {
56
+ error = await trySave(client);
57
+ }
58
+ }
59
+ },
60
+ { interval: throttleTime }
61
+ ),
62
+ restoreClient: async () => {
63
+ const cacheString = await storage.getItem(key);
64
+ if (!cacheString) {
65
+ return;
66
+ }
67
+ return deserialize(cacheString);
68
+ },
69
+ removeClient: () => storage.removeItem(key)
70
+ };
71
+ }
72
+ return {
73
+ persistClient: noop,
74
+ restoreClient: () => Promise.resolve(void 0),
75
+ removeClient: noop
76
+ };
77
+ };
78
+ function noop() {
79
+ }
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ createAsyncStoragePersister
83
+ });
84
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import { asyncThrottle } from './asyncThrottle'\nimport type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\n\ninterface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<unknown>\n removeItem: (key: string) => Promise<void>\n}\n\nexport type AsyncPersistRetryer = (props: {\n persistedClient: PersistedClient\n error: Error\n errorCount: number\n}) => Promisable<PersistedClient | undefined>\n\ninterface CreateAsyncStoragePersisterOptions {\n /** The storage client used for setting and retrieving items from cache.\n * For SSR pass in `undefined`. Note that window.localStorage can be\n * `null` in Android WebViews depending on how they are configured.\n */\n storage: AsyncStorage | undefined | null\n /** The key to use when storing the cache */\n key?: string\n /** To avoid spamming,\n * pass a time in ms to throttle saving the cache to disk */\n throttleTime?: number\n /**\n * How to serialize the data to storage.\n * @default `JSON.stringify`\n */\n serialize?: (client: PersistedClient) => string\n /**\n * How to deserialize the data from storage.\n * @default `JSON.parse`\n */\n deserialize?: (cachedString: string) => PersistedClient\n\n retry?: AsyncPersistRetryer\n}\n\nexport const createAsyncStoragePersister = ({\n storage,\n key = `REACT_QUERY_OFFLINE_CACHE`,\n throttleTime = 1000,\n serialize = JSON.stringify,\n deserialize = JSON.parse,\n retry,\n}: CreateAsyncStoragePersisterOptions): Persister => {\n if (storage) {\n const trySave = async (\n persistedClient: PersistedClient,\n ): Promise<Error | undefined> => {\n try {\n await storage.setItem(key, serialize(persistedClient))\n return\n } catch (error) {\n return error as Error\n }\n }\n\n return {\n persistClient: asyncThrottle(\n async (persistedClient) => {\n let client: PersistedClient | undefined = persistedClient\n let error = await trySave(client)\n let errorCount = 0\n while (error && client) {\n errorCount++\n client = await retry?.({\n persistedClient: client,\n error,\n errorCount,\n })\n\n if (client) {\n error = await trySave(client)\n }\n }\n },\n { interval: throttleTime },\n ),\n restoreClient: async () => {\n const cacheString = await storage.getItem(key)\n\n if (!cacheString) {\n return\n }\n\n return deserialize(cacheString)\n },\n removeClient: () => storage.removeItem(key),\n }\n }\n\n return {\n persistClient: noop,\n restoreClient: () => Promise.resolve(undefined),\n removeClient: noop,\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nfunction noop() {}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,2BAA8B;AA4CvB,MAAM,8BAA8B,CAAC;AAAA,EAC1C;AAAA,EACA,MAAM;AAAA,EACN,eAAe;AAAA,EACf,YAAY,KAAK;AAAA,EACjB,cAAc,KAAK;AAAA,EACnB;AACF,MAAqD;AACnD,MAAI,SAAS;AACX,UAAM,UAAU,OACd,oBAC+B;AAC/B,UAAI;AACF,cAAM,QAAQ,QAAQ,KAAK,UAAU,eAAe,CAAC;AACrD;AAAA,MACF,SAAS,OAAO;AACd,eAAO;AAAA,MACT;AAAA,IACF;AAEA,WAAO;AAAA,MACL,mBAAe;AAAA,QACb,OAAO,oBAAoB;AACzB,cAAI,SAAsC;AAC1C,cAAI,QAAQ,MAAM,QAAQ,MAAM;AAChC,cAAI,aAAa;AACjB,iBAAO,SAAS,QAAQ;AACtB;AACA,qBAAS,MAAM,QAAQ;AAAA,cACrB,iBAAiB;AAAA,cACjB;AAAA,cACA;AAAA,YACF,CAAC;AAED,gBAAI,QAAQ;AACV,sBAAQ,MAAM,QAAQ,MAAM;AAAA,YAC9B;AAAA,UACF;AAAA,QACF;AAAA,QACA,EAAE,UAAU,aAAa;AAAA,MAC3B;AAAA,MACA,eAAe,YAAY;AACzB,cAAM,cAAc,MAAM,QAAQ,QAAQ,GAAG;AAE7C,YAAI,CAAC,aAAa;AAChB;AAAA,QACF;AAEA,eAAO,YAAY,WAAW;AAAA,MAChC;AAAA,MACA,cAAc,MAAM,QAAQ,WAAW,GAAG;AAAA,IAC5C;AAAA,EACF;AAEA,SAAO;AAAA,IACL,eAAe;AAAA,IACf,eAAe,MAAM,QAAQ,QAAQ,MAAS;AAAA,IAC9C,cAAc;AAAA,EAChB;AACF;AAGA,SAAS,OAAO;AAAC;","names":[]}
@@ -0,0 +1,38 @@
1
+ import { PersistedClient, Promisable, Persister } from '@tanstack/query-persist-client-core';
2
+
3
+ interface AsyncStorage {
4
+ getItem: (key: string) => Promise<string | null>;
5
+ setItem: (key: string, value: string) => Promise<unknown>;
6
+ removeItem: (key: string) => Promise<void>;
7
+ }
8
+ type AsyncPersistRetryer = (props: {
9
+ persistedClient: PersistedClient;
10
+ error: Error;
11
+ errorCount: number;
12
+ }) => Promisable<PersistedClient | undefined>;
13
+ interface CreateAsyncStoragePersisterOptions {
14
+ /** The storage client used for setting and retrieving items from cache.
15
+ * For SSR pass in `undefined`. Note that window.localStorage can be
16
+ * `null` in Android WebViews depending on how they are configured.
17
+ */
18
+ storage: AsyncStorage | undefined | null;
19
+ /** The key to use when storing the cache */
20
+ key?: string;
21
+ /** To avoid spamming,
22
+ * pass a time in ms to throttle saving the cache to disk */
23
+ throttleTime?: number;
24
+ /**
25
+ * How to serialize the data to storage.
26
+ * @default `JSON.stringify`
27
+ */
28
+ serialize?: (client: PersistedClient) => string;
29
+ /**
30
+ * How to deserialize the data from storage.
31
+ * @default `JSON.parse`
32
+ */
33
+ deserialize?: (cachedString: string) => PersistedClient;
34
+ retry?: AsyncPersistRetryer;
35
+ }
36
+ declare const createAsyncStoragePersister: ({ storage, key, throttleTime, serialize, deserialize, retry, }: CreateAsyncStoragePersisterOptions) => Persister;
37
+
38
+ export { AsyncPersistRetryer, createAsyncStoragePersister };