@tanstack/query-async-storage-persister 4.0.0

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.
@@ -0,0 +1,137 @@
1
+ /**
2
+ * query-async-storage-persister
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ (function (global, factory) {
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
13
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.QueryAsyncStoragePersister = {}));
15
+ })(this, (function (exports) { 'use strict';
16
+
17
+ const noop$1 = () => {
18
+ /* do nothing */
19
+ };
20
+
21
+ function asyncThrottle(func, {
22
+ interval = 1000,
23
+ onError = noop$1
24
+ } = {}) {
25
+ if (typeof func !== 'function') throw new Error('argument is not function.');
26
+ let running = false;
27
+ let lastTime = 0;
28
+ let timeout;
29
+ let currentArgs = null;
30
+
31
+ const execFunc = async () => {
32
+ if (currentArgs) {
33
+ const args = currentArgs;
34
+ currentArgs = null;
35
+
36
+ try {
37
+ running = true;
38
+ await func(...args);
39
+ } catch (error) {
40
+ onError(error);
41
+ } finally {
42
+ lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
43
+
44
+ running = false;
45
+ }
46
+ }
47
+ };
48
+
49
+ const delayFunc = async () => {
50
+ clearTimeout(timeout);
51
+ timeout = setTimeout(() => {
52
+ if (running) {
53
+ delayFunc(); // Will come here when 'func' execution time is greater than the interval.
54
+ } else {
55
+ execFunc();
56
+ }
57
+ }, interval);
58
+ };
59
+
60
+ return (...args) => {
61
+ currentArgs = args;
62
+ const tooSoon = Date.now() - lastTime < interval;
63
+
64
+ if (running || tooSoon) {
65
+ delayFunc();
66
+ } else {
67
+ execFunc();
68
+ }
69
+ };
70
+ }
71
+
72
+ const createAsyncStoragePersister = ({
73
+ storage,
74
+ key = "REACT_QUERY_OFFLINE_CACHE",
75
+ throttleTime = 1000,
76
+ serialize = JSON.stringify,
77
+ deserialize = JSON.parse,
78
+ retry
79
+ }) => {
80
+ if (typeof storage !== 'undefined') {
81
+ const trySave = async persistedClient => {
82
+ try {
83
+ await storage.setItem(key, serialize(persistedClient));
84
+ } catch (error) {
85
+ return error;
86
+ }
87
+ };
88
+
89
+ return {
90
+ persistClient: asyncThrottle(async persistedClient => {
91
+ let client = persistedClient;
92
+ let error = await trySave(client);
93
+ let errorCount = 0;
94
+
95
+ while (error && client) {
96
+ errorCount++;
97
+ client = await (retry == null ? void 0 : retry({
98
+ persistedClient: client,
99
+ error,
100
+ errorCount
101
+ }));
102
+
103
+ if (client) {
104
+ error = await trySave(client);
105
+ }
106
+ }
107
+ }, {
108
+ interval: throttleTime
109
+ }),
110
+ restoreClient: async () => {
111
+ const cacheString = await storage.getItem(key);
112
+
113
+ if (!cacheString) {
114
+ return;
115
+ }
116
+
117
+ return deserialize(cacheString);
118
+ },
119
+ removeClient: () => storage.removeItem(key)
120
+ };
121
+ }
122
+
123
+ return {
124
+ persistClient: noop,
125
+ restoreClient: () => Promise.resolve(undefined),
126
+ removeClient: noop
127
+ };
128
+ }; // eslint-disable-next-line @typescript-eslint/no-empty-function
129
+
130
+ function noop() {}
131
+
132
+ exports.createAsyncStoragePersister = createAsyncStoragePersister;
133
+
134
+ Object.defineProperty(exports, '__esModule', { value: true });
135
+
136
+ }));
137
+ //# sourceMappingURL=index.development.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.development.js","sources":["../../src/asyncThrottle.ts","../../src/index.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","import {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/react-query-persist-client'\nimport { asyncThrottle } from './asyncThrottle'\n\ninterface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<void>\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`.\n */\n storage: AsyncStorage | undefined\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 (typeof storage !== 'undefined') {\n const trySave = async (\n persistedClient: PersistedClient,\n ): Promise<Error | undefined> => {\n try {\n await storage.setItem(key, serialize(persistedClient))\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) as PersistedClient\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"],"names":["noop","asyncThrottle","func","interval","onError","Error","running","lastTime","timeout","currentArgs","execFunc","args","error","Date","now","delayFunc","clearTimeout","setTimeout","tooSoon","createAsyncStoragePersister","storage","key","throttleTime","serialize","JSON","stringify","deserialize","parse","retry","trySave","persistedClient","setItem","persistClient","client","errorCount","restoreClient","cacheString","getItem","removeClient","removeItem","Promise","resolve","undefined"],"mappings":";;;;;;;;;;;;;;;;EAKA,MAAMA,MAAI,GAAG,MAAM;EACjB;EACD,CAFD,CAAA;;EAIO,SAASC,aAAT,CACLC,IADK,EAEL;EAAEC,EAAAA,QAAQ,GAAG,IAAb;EAAmBC,EAAAA,OAAO,GAAGJ,MAAAA;EAA7B,CAAA,GAA4D,EAFvD,EAGL;IACA,IAAI,OAAOE,IAAP,KAAgB,UAApB,EAAgC,MAAM,IAAIG,KAAJ,CAAU,2BAAV,CAAN,CAAA;IAEhC,IAAIC,OAAO,GAAG,KAAd,CAAA;IACA,IAAIC,QAAQ,GAAG,CAAf,CAAA;EACA,EAAA,IAAIC,OAAJ,CAAA;IAC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AA;;UAED,OAAOV,WAAW,CAACU,WAAD,CAAlB,CAAA;SA5BG;EA8BLE,MAAAA,YAAY,EAAE,MAAMlB,OAAO,CAACmB,UAAR,CAAmBlB,GAAnB,CAAA;OA9BtB,CAAA;EAgCD,GAAA;;IAED,OAAO;EACLW,IAAAA,aAAa,EAAEhC,IADV;EAELmC,IAAAA,aAAa,EAAE,MAAMK,OAAO,CAACC,OAAR,CAAgBC,SAAhB,CAFhB;EAGLJ,IAAAA,YAAY,EAAEtC,IAAAA;KAHhB,CAAA;EAKD;;EAGD,SAASA,IAAT,GAAgB;;;;;;;;;;"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * query-async-storage-persister
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).QueryAsyncStoragePersister={})}(this,(function(e){"use strict";const t=()=>{};function r(e,{interval:r=1e3,onError:n=t}={}){if("function"!=typeof e)throw new Error("argument is not function.");let i,o=!1,s=0,a=null;const l=async()=>{if(a){const t=a;a=null;try{o=!0,await e(...t)}catch(e){n(e)}finally{s=Date.now(),o=!1}}},c=async()=>{clearTimeout(i),i=setTimeout((()=>{o?c():l()}),r)};return(...e)=>{a=e;const t=Date.now()-s<r;o||t?c():l()}}function n(){}e.createAsyncStoragePersister=({storage:e,key:t="REACT_QUERY_OFFLINE_CACHE",throttleTime:i=1e3,serialize:o=JSON.stringify,deserialize:s=JSON.parse,retry:a})=>{if(void 0!==e){const n=async r=>{try{await e.setItem(t,o(r))}catch(e){return e}};return{persistClient:r((async e=>{let t=e,r=await n(t),i=0;for(;r&&t;)i++,t=await(null==a?void 0:a({persistedClient:t,error:r,errorCount:i})),t&&(r=await n(t))}),{interval:i}),restoreClient:async()=>{const r=await e.getItem(t);if(r)return s(r)},removeClient:()=>e.removeItem(t)}}return{persistClient:n,restoreClient:()=>Promise.resolve(void 0),removeClient:n}},Object.defineProperty(e,"__esModule",{value:!0})}));
12
+ //# sourceMappingURL=index.production.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.production.js","sources":["../../src/asyncThrottle.ts","../../src/index.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","import {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/react-query-persist-client'\nimport { asyncThrottle } from './asyncThrottle'\n\ninterface AsyncStorage {\n getItem: (key: string) => Promise<string | null>\n setItem: (key: string, value: string) => Promise<void>\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`.\n */\n storage: AsyncStorage | undefined\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 (typeof storage !== 'undefined') {\n const trySave = async (\n persistedClient: PersistedClient,\n ): Promise<Error | undefined> => {\n try {\n await storage.setItem(key, serialize(persistedClient))\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) as PersistedClient\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"],"names":["noop","asyncThrottle","func","interval","onError","Error","timeout","running","lastTime","currentArgs","execFunc","async","args","error","Date","now","delayFunc","clearTimeout","setTimeout","tooSoon","storage","key","throttleTime","serialize","JSON","stringify","deserialize","parse","retry","trySave","setItem","persistedClient","persistClient","client","errorCount","restoreClient","cacheString","getItem","removeClient","removeItem","Promise","resolve","undefined"],"m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ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "@tanstack/query-async-storage-persister",
3
+ "version": "4.0.0",
4
+ "description": "TODO",
5
+ "author": "tannerlinsley",
6
+ "license": "MIT",
7
+ "repository": "tanstack/query",
8
+ "homepage": "https://tanstack.com/query",
9
+ "funding": {
10
+ "type": "github",
11
+ "url": "https://github.com/sponsors/tannerlinsley"
12
+ },
13
+ "module": "build/esm/index.js",
14
+ "main": "build/cjs/index.js",
15
+ "browser": "build/umd/index.production.js",
16
+ "types": "build/types/packages/query-async-storage-persister/src/index.d.ts",
17
+ "files": [
18
+ "build",
19
+ "src"
20
+ ],
21
+ "scripts": {
22
+ "test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src",
23
+ "compile": "../../node_modules/.bin/tsc -p tsconfig.json --noEmit --emitDeclarationOnly false"
24
+ }
25
+ }
@@ -0,0 +1,127 @@
1
+ import { asyncThrottle } from '../asyncThrottle'
2
+ import { sleep as delay } from '../../../../tests/utils'
3
+
4
+ describe('asyncThrottle', () => {
5
+ test('basic', async () => {
6
+ const interval = 10
7
+ const execTimeStamps: number[] = []
8
+ const mockFunc = jest.fn(
9
+ async (id: number, complete?: (value?: unknown) => void) => {
10
+ await delay(1)
11
+ execTimeStamps.push(Date.now())
12
+ if (complete) {
13
+ complete(id)
14
+ }
15
+ },
16
+ )
17
+ const testFunc = asyncThrottle(mockFunc, { interval })
18
+
19
+ testFunc(1)
20
+ await delay(1)
21
+ testFunc(2)
22
+ await delay(1)
23
+ await new Promise((resolve) => testFunc(3, resolve))
24
+
25
+ expect(mockFunc).toBeCalledTimes(2)
26
+ expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
27
+ expect(execTimeStamps.length).toBe(2)
28
+ expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
29
+ interval,
30
+ )
31
+ })
32
+
33
+ test('Bug #3331 case 1: Special timing', async () => {
34
+ const interval = 1000
35
+ const execTimeStamps: number[] = []
36
+ const mockFunc = jest.fn(
37
+ async (id: number, complete?: (value?: unknown) => void) => {
38
+ await delay(30)
39
+ execTimeStamps.push(Date.now())
40
+ if (complete) {
41
+ complete(id)
42
+ }
43
+ },
44
+ )
45
+ const testFunc = asyncThrottle(mockFunc, { interval })
46
+
47
+ testFunc(1)
48
+ testFunc(2)
49
+ await delay(35)
50
+ testFunc(3)
51
+ await delay(35)
52
+ await new Promise((resolve) => testFunc(4, resolve))
53
+
54
+ expect(mockFunc).toBeCalledTimes(2)
55
+ expect(mockFunc.mock.calls[1]?.[0]).toBe(4)
56
+ expect(execTimeStamps.length).toBe(2)
57
+ expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
58
+ interval,
59
+ )
60
+ })
61
+
62
+ test('Bug #3331 case 2: "func" execution time is greater than the interval.', async () => {
63
+ const interval = 1000
64
+ const execTimeStamps: number[] = []
65
+ const mockFunc = jest.fn(
66
+ async (id: number, complete?: (value?: unknown) => void) => {
67
+ await delay(interval + 10)
68
+ execTimeStamps.push(Date.now())
69
+ if (complete) {
70
+ complete(id)
71
+ }
72
+ },
73
+ )
74
+ const testFunc = asyncThrottle(mockFunc, { interval })
75
+
76
+ testFunc(1)
77
+ testFunc(2)
78
+ await new Promise((resolve) => testFunc(3, resolve))
79
+
80
+ expect(mockFunc).toBeCalledTimes(2)
81
+ expect(mockFunc.mock.calls[1]?.[0]).toBe(3)
82
+ expect(execTimeStamps.length).toBe(2)
83
+ expect(execTimeStamps[1]! - execTimeStamps[0]!).toBeGreaterThanOrEqual(
84
+ interval,
85
+ )
86
+ })
87
+
88
+ test('"func" throw error not break next invoke', async () => {
89
+ const mockFunc = jest.fn(
90
+ async (id: number, complete?: (value?: unknown) => void) => {
91
+ if (id === 1) throw new Error('error')
92
+ await delay(1)
93
+ if (complete) {
94
+ complete(id)
95
+ }
96
+ },
97
+ )
98
+ const testFunc = asyncThrottle(mockFunc, { interval: 10 })
99
+
100
+ testFunc(1)
101
+ await delay(1)
102
+ await new Promise((resolve) => testFunc(2, resolve))
103
+
104
+ expect(mockFunc).toBeCalledTimes(2)
105
+ expect(mockFunc.mock.calls[1]?.[0]).toBe(2)
106
+ })
107
+
108
+ test('"onError" should be called when "func" throw error', (done) => {
109
+ const err = new Error('error')
110
+ const handleError = (e: unknown) => {
111
+ expect(e).toBe(err)
112
+ done()
113
+ }
114
+
115
+ const testFunc = asyncThrottle(
116
+ () => {
117
+ throw err
118
+ },
119
+ { onError: handleError },
120
+ )
121
+ testFunc()
122
+ })
123
+
124
+ test('should throw error when "func" is not a function', () => {
125
+ expect(() => asyncThrottle(1 as any)).toThrowError()
126
+ })
127
+ })
@@ -0,0 +1,58 @@
1
+ export interface AsyncThrottleOptions {
2
+ interval?: number
3
+ onError?: (error: unknown) => void
4
+ }
5
+
6
+ const noop = () => {
7
+ /* do nothing */
8
+ }
9
+
10
+ export function asyncThrottle<Args extends readonly unknown[]>(
11
+ func: (...args: Args) => Promise<void>,
12
+ { interval = 1000, onError = noop }: AsyncThrottleOptions = {},
13
+ ) {
14
+ if (typeof func !== 'function') throw new Error('argument is not function.')
15
+
16
+ let running = false
17
+ let lastTime = 0
18
+ let timeout: ReturnType<typeof setTimeout>
19
+ let currentArgs: Args | null = null
20
+
21
+ const execFunc = async () => {
22
+ if (currentArgs) {
23
+ const args = currentArgs
24
+ currentArgs = null
25
+ try {
26
+ running = true
27
+ await func(...args)
28
+ } catch (error) {
29
+ onError(error)
30
+ } finally {
31
+ lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.
32
+ running = false
33
+ }
34
+ }
35
+ }
36
+
37
+ const delayFunc = async () => {
38
+ clearTimeout(timeout)
39
+ timeout = setTimeout(() => {
40
+ if (running) {
41
+ delayFunc() // Will come here when 'func' execution time is greater than the interval.
42
+ } else {
43
+ execFunc()
44
+ }
45
+ }, interval)
46
+ }
47
+
48
+ return (...args: Args) => {
49
+ currentArgs = args
50
+
51
+ const tooSoon = Date.now() - lastTime < interval
52
+ if (running || tooSoon) {
53
+ delayFunc()
54
+ } else {
55
+ execFunc()
56
+ }
57
+ }
58
+ }
package/src/index.ts ADDED
@@ -0,0 +1,105 @@
1
+ import {
2
+ PersistedClient,
3
+ Persister,
4
+ Promisable,
5
+ } from '@tanstack/react-query-persist-client'
6
+ import { asyncThrottle } from './asyncThrottle'
7
+
8
+ interface AsyncStorage {
9
+ getItem: (key: string) => Promise<string | null>
10
+ setItem: (key: string, value: string) => Promise<void>
11
+ removeItem: (key: string) => Promise<void>
12
+ }
13
+
14
+ export type AsyncPersistRetryer = (props: {
15
+ persistedClient: PersistedClient
16
+ error: Error
17
+ errorCount: number
18
+ }) => Promisable<PersistedClient | undefined>
19
+
20
+ interface CreateAsyncStoragePersisterOptions {
21
+ /** The storage client used for setting and retrieving items from cache.
22
+ * For SSR pass in `undefined`.
23
+ */
24
+ storage: AsyncStorage | undefined
25
+ /** The key to use when storing the cache */
26
+ key?: string
27
+ /** To avoid spamming,
28
+ * pass a time in ms to throttle saving the cache to disk */
29
+ throttleTime?: number
30
+ /**
31
+ * How to serialize the data to storage.
32
+ * @default `JSON.stringify`
33
+ */
34
+ serialize?: (client: PersistedClient) => string
35
+ /**
36
+ * How to deserialize the data from storage.
37
+ * @default `JSON.parse`
38
+ */
39
+ deserialize?: (cachedString: string) => PersistedClient
40
+
41
+ retry?: AsyncPersistRetryer
42
+ }
43
+
44
+ export const createAsyncStoragePersister = ({
45
+ storage,
46
+ key = `REACT_QUERY_OFFLINE_CACHE`,
47
+ throttleTime = 1000,
48
+ serialize = JSON.stringify,
49
+ deserialize = JSON.parse,
50
+ retry,
51
+ }: CreateAsyncStoragePersisterOptions): Persister => {
52
+ if (typeof storage !== 'undefined') {
53
+ const trySave = async (
54
+ persistedClient: PersistedClient,
55
+ ): Promise<Error | undefined> => {
56
+ try {
57
+ await storage.setItem(key, serialize(persistedClient))
58
+ } catch (error) {
59
+ return error as Error
60
+ }
61
+ }
62
+
63
+ return {
64
+ persistClient: asyncThrottle(
65
+ async (persistedClient) => {
66
+ let client: PersistedClient | undefined = persistedClient
67
+ let error = await trySave(client)
68
+ let errorCount = 0
69
+ while (error && client) {
70
+ errorCount++
71
+ client = await retry?.({
72
+ persistedClient: client,
73
+ error,
74
+ errorCount,
75
+ })
76
+
77
+ if (client) {
78
+ error = await trySave(client)
79
+ }
80
+ }
81
+ },
82
+ { interval: throttleTime },
83
+ ),
84
+ restoreClient: async () => {
85
+ const cacheString = await storage.getItem(key)
86
+
87
+ if (!cacheString) {
88
+ return
89
+ }
90
+
91
+ return deserialize(cacheString) as PersistedClient
92
+ },
93
+ removeClient: () => storage.removeItem(key),
94
+ }
95
+ }
96
+
97
+ return {
98
+ persistClient: noop,
99
+ restoreClient: () => Promise.resolve(undefined),
100
+ removeClient: noop,
101
+ }
102
+ }
103
+
104
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
105
+ function noop() {}