@tanstack/query-async-storage-persister 4.3.0-beta.4 → 4.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/lib/asyncThrottle.js +61 -0
- package/build/lib/asyncThrottle.js.map +1 -0
- package/build/lib/asyncThrottle.mjs +57 -0
- package/build/lib/asyncThrottle.mjs.map +1 -0
- package/build/lib/index.js +2 -65
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +1 -64
- package/build/lib/index.mjs.map +1 -1
- package/build/umd/index.development.js +0 -10
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +0 -10
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const noop = () => {
|
|
6
|
+
/* do nothing */
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
function asyncThrottle(func, {
|
|
10
|
+
interval = 1000,
|
|
11
|
+
onError = noop
|
|
12
|
+
} = {}) {
|
|
13
|
+
if (typeof func !== 'function') throw new Error('argument is not function.');
|
|
14
|
+
let running = false;
|
|
15
|
+
let lastTime = 0;
|
|
16
|
+
let timeout;
|
|
17
|
+
let currentArgs = null;
|
|
18
|
+
|
|
19
|
+
const execFunc = async () => {
|
|
20
|
+
if (currentArgs) {
|
|
21
|
+
const args = currentArgs;
|
|
22
|
+
currentArgs = null;
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
running = true;
|
|
26
|
+
await func(...args);
|
|
27
|
+
} catch (error) {
|
|
28
|
+
onError(error);
|
|
29
|
+
} finally {
|
|
30
|
+
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
31
|
+
|
|
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) => {
|
|
49
|
+
currentArgs = args;
|
|
50
|
+
const tooSoon = Date.now() - lastTime < interval;
|
|
51
|
+
|
|
52
|
+
if (running || tooSoon) {
|
|
53
|
+
delayFunc();
|
|
54
|
+
} else {
|
|
55
|
+
execFunc();
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
exports.asyncThrottle = asyncThrottle;
|
|
61
|
+
//# sourceMappingURL=asyncThrottle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asyncThrottle.js","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"],"names":["noop","asyncThrottle","func","interval","onError","Error","running","lastTime","timeout","currentArgs","execFunc","args","error","Date","now","delayFunc","clearTimeout","setTimeout","tooSoon"],"mappings":";;;;AAKA,MAAMA,IAAI,GAAG,MAAM;AACjB;AACD,CAFD,CAAA;;AAIO,SAASC,aAAT,CACLC,IADK,EAEL;AAAEC,EAAAA,QAAQ,GAAG,IAAb;AAAmBC,EAAAA,OAAO,GAAGJ,IAAAA;AAA7B,CAAA,GAA4D,EAFvD,EAGL;EACA,IAAI,OAAOE,IAAP,KAAgB,UAApB,EAAgC,MAAM,IAAIG,KAAJ,CAAU,2BAAV,CAAN,CAAA;EAEhC,IAAIC,OAAO,GAAG,KAAd,CAAA;EACA,IAAIC,QAAQ,GAAG,CAAf,CAAA;AACA,EAAA,IAAIC,OAAJ,CAAA;EACA,IAAIC,WAAwB,GAAG,IAA/B,CAAA;;EAEA,MAAMC,QAAQ,GAAG,YAAY;AAC3B,IAAA,IAAID,WAAJ,EAAiB;MACf,MAAME,IAAI,GAAGF,WAAb,CAAA;AACAA,MAAAA,WAAW,GAAG,IAAd,CAAA;;MACA,IAAI;AACFH,QAAAA,OAAO,GAAG,IAAV,CAAA;AACA,QAAA,MAAMJ,IAAI,CAAC,GAAGS,IAAJ,CAAV,CAAA;OAFF,CAGE,OAAOC,KAAP,EAAc;QACdR,OAAO,CAACQ,KAAD,CAAP,CAAA;AACD,OALD,SAKU;AACRL,QAAAA,QAAQ,GAAGM,IAAI,CAACC,GAAL,EAAX,CADQ;;AAERR,QAAAA,OAAO,GAAG,KAAV,CAAA;AACD,OAAA;AACF,KAAA;GAbH,CAAA;;EAgBA,MAAMS,SAAS,GAAG,YAAY;IAC5BC,YAAY,CAACR,OAAD,CAAZ,CAAA;IACAA,OAAO,GAAGS,UAAU,CAAC,MAAM;AACzB,MAAA,IAAIX,OAAJ,EAAa;AACXS,QAAAA,SAAS,GADE;AAEZ,OAFD,MAEO;QACLL,QAAQ,EAAA,CAAA;AACT,OAAA;KALiB,EAMjBP,QANiB,CAApB,CAAA;GAFF,CAAA;;EAWA,OAAO,CAAC,GAAGQ,IAAJ,KAAmB;AACxBF,IAAAA,WAAW,GAAGE,IAAd,CAAA;IAEA,MAAMO,OAAO,GAAGL,IAAI,CAACC,GAAL,EAAaP,GAAAA,QAAb,GAAwBJ,QAAxC,CAAA;;IACA,IAAIG,OAAO,IAAIY,OAAf,EAAwB;MACtBH,SAAS,EAAA,CAAA;AACV,KAFD,MAEO;MACLL,QAAQ,EAAA,CAAA;AACT,KAAA;GARH,CAAA;AAUD;;;;"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const noop = () => {
|
|
2
|
+
/* do nothing */
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
function asyncThrottle(func, {
|
|
6
|
+
interval = 1000,
|
|
7
|
+
onError = noop
|
|
8
|
+
} = {}) {
|
|
9
|
+
if (typeof func !== 'function') throw new Error('argument is not function.');
|
|
10
|
+
let running = false;
|
|
11
|
+
let lastTime = 0;
|
|
12
|
+
let timeout;
|
|
13
|
+
let currentArgs = null;
|
|
14
|
+
|
|
15
|
+
const execFunc = async () => {
|
|
16
|
+
if (currentArgs) {
|
|
17
|
+
const args = currentArgs;
|
|
18
|
+
currentArgs = null;
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
running = true;
|
|
22
|
+
await func(...args);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
onError(error);
|
|
25
|
+
} finally {
|
|
26
|
+
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
27
|
+
|
|
28
|
+
running = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const delayFunc = async () => {
|
|
34
|
+
clearTimeout(timeout);
|
|
35
|
+
timeout = setTimeout(() => {
|
|
36
|
+
if (running) {
|
|
37
|
+
delayFunc(); // Will come here when 'func' execution time is greater than the interval.
|
|
38
|
+
} else {
|
|
39
|
+
execFunc();
|
|
40
|
+
}
|
|
41
|
+
}, interval);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
return (...args) => {
|
|
45
|
+
currentArgs = args;
|
|
46
|
+
const tooSoon = Date.now() - lastTime < interval;
|
|
47
|
+
|
|
48
|
+
if (running || tooSoon) {
|
|
49
|
+
delayFunc();
|
|
50
|
+
} else {
|
|
51
|
+
execFunc();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export { asyncThrottle };
|
|
57
|
+
//# sourceMappingURL=asyncThrottle.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asyncThrottle.mjs","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"],"names":["noop","asyncThrottle","func","interval","onError","Error","running","lastTime","timeout","currentArgs","execFunc","args","error","Date","now","delayFunc","clearTimeout","setTimeout","tooSoon"],"mappings":"AAKA,MAAMA,IAAI,GAAG,MAAM;AACjB;AACD,CAFD,CAAA;;AAIO,SAASC,aAAT,CACLC,IADK,EAEL;AAAEC,EAAAA,QAAQ,GAAG,IAAb;AAAmBC,EAAAA,OAAO,GAAGJ,IAAAA;AAA7B,CAAA,GAA4D,EAFvD,EAGL;EACA,IAAI,OAAOE,IAAP,KAAgB,UAApB,EAAgC,MAAM,IAAIG,KAAJ,CAAU,2BAAV,CAAN,CAAA;EAEhC,IAAIC,OAAO,GAAG,KAAd,CAAA;EACA,IAAIC,QAAQ,GAAG,CAAf,CAAA;AACA,EAAA,IAAIC,OAAJ,CAAA;EACA,IAAIC,WAAwB,GAAG,IAA/B,CAAA;;EAEA,MAAMC,QAAQ,GAAG,YAAY;AAC3B,IAAA,IAAID,WAAJ,EAAiB;MACf,MAAME,IAAI,GAAGF,WAAb,CAAA;AACAA,MAAAA,WAAW,GAAG,IAAd,CAAA;;MACA,IAAI;AACFH,QAAAA,OAAO,GAAG,IAAV,CAAA;AACA,QAAA,MAAMJ,IAAI,CAAC,GAAGS,IAAJ,CAAV,CAAA;OAFF,CAGE,OAAOC,KAAP,EAAc;QACdR,OAAO,CAACQ,KAAD,CAAP,CAAA;AACD,OALD,SAKU;AACRL,QAAAA,QAAQ,GAAGM,IAAI,CAACC,GAAL,EAAX,CADQ;;AAERR,QAAAA,OAAO,GAAG,KAAV,CAAA;AACD,OAAA;AACF,KAAA;GAbH,CAAA;;EAgBA,MAAMS,SAAS,GAAG,YAAY;IAC5BC,YAAY,CAACR,OAAD,CAAZ,CAAA;IACAA,OAAO,GAAGS,UAAU,CAAC,MAAM;AACzB,MAAA,IAAIX,OAAJ,EAAa;AACXS,QAAAA,SAAS,GADE;AAEZ,OAFD,MAEO;QACLL,QAAQ,EAAA,CAAA;AACT,OAAA;KALiB,EAMjBP,QANiB,CAApB,CAAA;GAFF,CAAA;;EAWA,OAAO,CAAC,GAAGQ,IAAJ,KAAmB;AACxBF,IAAAA,WAAW,GAAGE,IAAd,CAAA;IAEA,MAAMO,OAAO,GAAGL,IAAI,CAACC,GAAL,EAAaP,GAAAA,QAAb,GAAwBJ,QAAxC,CAAA;;IACA,IAAIG,OAAO,IAAIY,OAAf,EAAwB;MACtBH,SAAS,EAAA,CAAA;AACV,KAFD,MAEO;MACLL,QAAQ,EAAA,CAAA;AACT,KAAA;GARH,CAAA;AAUD;;;;"}
|
package/build/lib/index.js
CHANGED
|
@@ -1,71 +1,8 @@
|
|
|
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
1
|
'use strict';
|
|
12
2
|
|
|
13
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
4
|
|
|
15
|
-
|
|
16
|
-
/* do nothing */
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
function asyncThrottle(func, {
|
|
20
|
-
interval = 1000,
|
|
21
|
-
onError = noop$1
|
|
22
|
-
} = {}) {
|
|
23
|
-
if (typeof func !== 'function') throw new Error('argument is not function.');
|
|
24
|
-
let running = false;
|
|
25
|
-
let lastTime = 0;
|
|
26
|
-
let timeout;
|
|
27
|
-
let currentArgs = null;
|
|
28
|
-
|
|
29
|
-
const execFunc = async () => {
|
|
30
|
-
if (currentArgs) {
|
|
31
|
-
const args = currentArgs;
|
|
32
|
-
currentArgs = null;
|
|
33
|
-
|
|
34
|
-
try {
|
|
35
|
-
running = true;
|
|
36
|
-
await func(...args);
|
|
37
|
-
} catch (error) {
|
|
38
|
-
onError(error);
|
|
39
|
-
} finally {
|
|
40
|
-
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
41
|
-
|
|
42
|
-
running = false;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const delayFunc = async () => {
|
|
48
|
-
clearTimeout(timeout);
|
|
49
|
-
timeout = setTimeout(() => {
|
|
50
|
-
if (running) {
|
|
51
|
-
delayFunc(); // Will come here when 'func' execution time is greater than the interval.
|
|
52
|
-
} else {
|
|
53
|
-
execFunc();
|
|
54
|
-
}
|
|
55
|
-
}, interval);
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
return (...args) => {
|
|
59
|
-
currentArgs = args;
|
|
60
|
-
const tooSoon = Date.now() - lastTime < interval;
|
|
61
|
-
|
|
62
|
-
if (running || tooSoon) {
|
|
63
|
-
delayFunc();
|
|
64
|
-
} else {
|
|
65
|
-
execFunc();
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
5
|
+
var asyncThrottle = require('./asyncThrottle.js');
|
|
69
6
|
|
|
70
7
|
const createAsyncStoragePersister = ({
|
|
71
8
|
storage,
|
|
@@ -85,7 +22,7 @@ const createAsyncStoragePersister = ({
|
|
|
85
22
|
};
|
|
86
23
|
|
|
87
24
|
return {
|
|
88
|
-
persistClient: asyncThrottle(async persistedClient => {
|
|
25
|
+
persistClient: asyncThrottle.asyncThrottle(async persistedClient => {
|
|
89
26
|
let client = persistedClient;
|
|
90
27
|
let error = await trySave(client);
|
|
91
28
|
let errorCount = 0;
|
package/build/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["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":["createAsyncStoragePersister","storage","key","throttleTime","serialize","JSON","stringify","deserialize","parse","retry","trySave","persistedClient","setItem","error","persistClient","asyncThrottle","client","errorCount","interval","restoreClient","cacheString","getItem","removeClient","removeItem","noop","Promise","resolve","undefined"],"mappings":";;;;;;AA2CO,MAAMA,2BAA2B,GAAG,CAAC;EAC1CC,OAD0C;AAE1CC,EAAAA,GAAG,GAFuC,2BAAA;AAG1CC,EAAAA,YAAY,GAAG,IAH2B;EAI1CC,SAAS,GAAGC,IAAI,CAACC,SAJyB;EAK1CC,WAAW,GAAGF,IAAI,CAACG,KALuB;AAM1CC,EAAAA,KAAAA;AAN0C,CAAD,KAOU;AACnD,EAAA,IAAI,OAAOR,OAAP,KAAmB,WAAvB,EAAoC;AAClC,IAAA,MAAMS,OAAO,GAAG,MACdC,eADc,IAEiB;MAC/B,IAAI;QACF,MAAMV,OAAO,CAACW,OAAR,CAAgBV,GAAhB,EAAqBE,SAAS,CAACO,eAAD,CAA9B,CAAN,CAAA;OADF,CAEE,OAAOE,KAAP,EAAc;AACd,QAAA,OAAOA,KAAP,CAAA;AACD,OAAA;KAPH,CAAA;;IAUA,OAAO;AACLC,MAAAA,aAAa,EAAEC,2BAAa,CAC1B,MAAOJ,eAAP,IAA2B;QACzB,IAAIK,MAAmC,GAAGL,eAA1C,CAAA;AACA,QAAA,IAAIE,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAD,CAAzB,CAAA;QACA,IAAIC,UAAU,GAAG,CAAjB,CAAA;;QACA,OAAOJ,KAAK,IAAIG,MAAhB,EAAwB;UACtBC,UAAU,EAAA,CAAA;AACVD,UAAAA,MAAM,GAAG,OAAMP,KAAN,IAAA,IAAA,GAAA,KAAA,CAAA,GAAMA,KAAK,CAAG;AACrBE,YAAAA,eAAe,EAAEK,MADI;YAErBH,KAFqB;AAGrBI,YAAAA,UAAAA;AAHqB,WAAH,CAAX,CAAT,CAAA;;AAMA,UAAA,IAAID,MAAJ,EAAY;AACVH,YAAAA,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAD,CAArB,CAAA;AACD,WAAA;AACF,SAAA;AACF,OAjByB,EAkB1B;AAAEE,QAAAA,QAAQ,EAAEf,YAAAA;AAAZ,OAlB0B,CADvB;AAqBLgB,MAAAA,aAAa,EAAE,YAAY;QACzB,MAAMC,WAAW,GAAG,MAAMnB,OAAO,CAACoB,OAAR,CAAgBnB,GAAhB,CAA1B,CAAA;;QAEA,IAAI,CAACkB,WAAL,EAAkB;AAChB,UAAA,OAAA;AACD,SAAA;;QAED,OAAOb,WAAW,CAACa,WAAD,CAAlB,CAAA;OA5BG;AA8BLE,MAAAA,YAAY,EAAE,MAAMrB,OAAO,CAACsB,UAAR,CAAmBrB,GAAnB,CAAA;KA9BtB,CAAA;AAgCD,GAAA;;EAED,OAAO;AACLY,IAAAA,aAAa,EAAEU,IADV;AAELL,IAAAA,aAAa,EAAE,MAAMM,OAAO,CAACC,OAAR,CAAgBC,SAAhB,CAFhB;AAGLL,IAAAA,YAAY,EAAEE,IAAAA;GAHhB,CAAA;AAKD;;AAGD,SAASA,IAAT,GAAgB;;;;"}
|
package/build/lib/index.mjs
CHANGED
|
@@ -1,67 +1,4 @@
|
|
|
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
|
-
const noop$1 = () => {
|
|
12
|
-
/* do nothing */
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
function asyncThrottle(func, {
|
|
16
|
-
interval = 1000,
|
|
17
|
-
onError = noop$1
|
|
18
|
-
} = {}) {
|
|
19
|
-
if (typeof func !== 'function') throw new Error('argument is not function.');
|
|
20
|
-
let running = false;
|
|
21
|
-
let lastTime = 0;
|
|
22
|
-
let timeout;
|
|
23
|
-
let currentArgs = null;
|
|
24
|
-
|
|
25
|
-
const execFunc = async () => {
|
|
26
|
-
if (currentArgs) {
|
|
27
|
-
const args = currentArgs;
|
|
28
|
-
currentArgs = null;
|
|
29
|
-
|
|
30
|
-
try {
|
|
31
|
-
running = true;
|
|
32
|
-
await func(...args);
|
|
33
|
-
} catch (error) {
|
|
34
|
-
onError(error);
|
|
35
|
-
} finally {
|
|
36
|
-
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
37
|
-
|
|
38
|
-
running = false;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const delayFunc = async () => {
|
|
44
|
-
clearTimeout(timeout);
|
|
45
|
-
timeout = setTimeout(() => {
|
|
46
|
-
if (running) {
|
|
47
|
-
delayFunc(); // Will come here when 'func' execution time is greater than the interval.
|
|
48
|
-
} else {
|
|
49
|
-
execFunc();
|
|
50
|
-
}
|
|
51
|
-
}, interval);
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
return (...args) => {
|
|
55
|
-
currentArgs = args;
|
|
56
|
-
const tooSoon = Date.now() - lastTime < interval;
|
|
57
|
-
|
|
58
|
-
if (running || tooSoon) {
|
|
59
|
-
delayFunc();
|
|
60
|
-
} else {
|
|
61
|
-
execFunc();
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
}
|
|
1
|
+
import { asyncThrottle } from './asyncThrottle.mjs';
|
|
65
2
|
|
|
66
3
|
const createAsyncStoragePersister = ({
|
|
67
4
|
storage,
|
package/build/lib/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../src/
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../src/index.ts"],"sourcesContent":["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":["createAsyncStoragePersister","storage","key","throttleTime","serialize","JSON","stringify","deserialize","parse","retry","trySave","persistedClient","setItem","error","persistClient","asyncThrottle","client","errorCount","interval","restoreClient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AAA;AAAZ,OAlB0B,CADvB;AAqBLgB,MAAAA,aAAa,EAAE,YAAY;QACzB,MAAMC,WAAW,GAAG,MAAMnB,OAAO,CAACoB,OAAR,CAAgBnB,GAAhB,CAA1B,CAAA;;QAEA,IAAI,CAACkB,WAAL,EAAkB;AAChB,UAAA,OAAA;AACD,SAAA;;QAED,OAAOb,WAAW,CAACa,WAAD,CAAlB,CAAA;OA5BG;AA8BLE,MAAAA,YAAY,EAAE,MAAMrB,OAAO,CAACsB,UAAR,CAAmBrB,GAAnB,CAAA;KA9BtB,CAAA;AAgCD,GAAA;;EAED,OAAO;AACLY,IAAAA,aAAa,EAAEU,IADV;AAELL,IAAAA,aAAa,EAAE,MAAMM,OAAO,CAACC,OAAR,CAAgBC,SAAhB,CAFhB;AAGLL,IAAAA,YAAY,EAAEE,IAAAA;GAHhB,CAAA;AAKD;;AAGD,SAASA,IAAT,GAAgB;;;;"}
|
|
@@ -1,13 +1,3 @@
|
|
|
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
1
|
(function (global, factory) {
|
|
12
2
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|
13
3
|
typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|
@@ -1 +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":"
|
|
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;IACA,IAAIC,WAAwB,GAAG,IAA/B,CAAA;;IAEA,MAAMC,QAAQ,GAAG,YAAY;EAC3B,IAAA,IAAID,WAAJ,EAAiB;QACf,MAAME,IAAI,GAAGF,WAAb,CAAA;EACAA,MAAAA,WAAW,GAAG,IAAd,CA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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;;;;;;;;;;"}
|
|
@@ -1,12 +1,2 @@
|
|
|
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
1
|
!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
2
|
//# sourceMappingURL=index.production.js.map
|
|
@@ -1 +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"],"mappings":"
|
|
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"],"mappings":"kQAKA,MAAMA,EAAO,OAIN,SAASC,EACdC,GACAC,SAAEA,EAAW,IAAbC,QAAmBA,EAAUJ,GAA+B,IAE5D,GAAoB,mBAATE,EAAqB,MAAM,IAAIG,MAAM,6BAEhD,IAEIC,EAFAC,GAAU,EACVC,EAAW,EAEXC,EAA2B,KAE/B,MAAMC,EAAWC,UACf,GAAIF,EAAa,CACf,MAAMG,EAAOH,EACbA,EAAc,KACd,IACEF,GAAU,QACJL,KAAQU,GACd,MAAOC,GACPT,EAAQS,GACA,QACRL,EAAWM,KAAKC,MAChBR,GAAU,KAKVS,EAAYL,UAChBM,aAAaX,GACbA,EAAUY,YAAW,KACfX,EACFS,IAEAN,MAEDP,IAGL,MAAO,IAAIS,KACTH,EAAcG,EAEd,MAAMO,EAAUL,KAAKC,MAAQP,EAAWL,EACpCI,GAAWY,EACbH,IAEAN,KCkDN,SAASV,mCA7DkC,EACzCoB,UACAC,MAF0C,4BAG1CC,eAAe,IACfC,YAAYC,KAAKC,UACjBC,cAAcF,KAAKG,MACnBC,YAEA,QAAuB,IAAZR,EAAyB,CAClC,MAAMS,EAAUlB,UAGd,UACQS,EAAQU,QAAQT,EAAKE,EAAUQ,IACrC,MAAOlB,GACP,OAAOA,IAIX,MAAO,CACLmB,cAAe/B,GACbU,UACE,IAAIsB,EAAsCF,EACtClB,QAAcgB,EAAQI,GACtBC,EAAa,EACjB,KAAOrB,GAASoB,GACdC,IACAD,QAAS,MAAML,OAAN,EAAMA,EAAQ,CACrBG,gBAAiBE,EACjBpB,QACAqB,gBAGED,IACFpB,QAAcgB,EAAQI,MAI5B,CAAE9B,SAAUmB,IAEda,cAAexB,UACb,MAAMyB,QAAoBhB,EAAQiB,QAAQhB,GAE1C,GAAKe,EAIL,OAAOV,EAAYU,IAErBE,aAAc,IAAMlB,EAAQmB,WAAWlB,IAI3C,MAAO,CACLW,cAAehC,EACfmC,cAAe,IAAMK,QAAQC,aAAQC,GACrCJ,aAActC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-async-storage-persister",
|
|
3
|
-
"version": "4.3.
|
|
3
|
+
"version": "4.3.1",
|
|
4
4
|
"description": "TODO",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,6 +31,6 @@
|
|
|
31
31
|
"test:eslint": "../../node_modules/.bin/eslint --ext .ts,.tsx ./src"
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@tanstack/react-query-persist-client": "4.3.
|
|
34
|
+
"@tanstack/react-query-persist-client": "4.3.1"
|
|
35
35
|
}
|
|
36
36
|
}
|