@tanstack/query-async-storage-persister 4.24.10 → 5.0.0-alpha.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.esm.js +0 -7
- package/build/lib/asyncThrottle.esm.js.map +1 -1
- package/build/lib/asyncThrottle.js +0 -9
- package/build/lib/asyncThrottle.js.map +1 -1
- package/build/lib/asyncThrottle.mjs +0 -7
- package/build/lib/asyncThrottle.mjs.map +1 -1
- package/build/lib/index.esm.js +5 -10
- package/build/lib/index.esm.js.map +1 -1
- package/build/lib/index.js +5 -12
- package/build/lib/index.js.map +1 -1
- package/build/lib/index.mjs +5 -10
- package/build/lib/index.mjs.map +1 -1
- package/build/umd/index.development.js +5 -19
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const noop = () => {
|
|
2
2
|
/* do nothing */
|
|
3
3
|
};
|
|
4
|
-
|
|
5
4
|
function asyncThrottle(func, {
|
|
6
5
|
interval = 1000,
|
|
7
6
|
onError = noop
|
|
@@ -11,12 +10,10 @@ function asyncThrottle(func, {
|
|
|
11
10
|
let lastTime = 0;
|
|
12
11
|
let timeout;
|
|
13
12
|
let currentArgs = null;
|
|
14
|
-
|
|
15
13
|
const execFunc = async () => {
|
|
16
14
|
if (currentArgs) {
|
|
17
15
|
const args = currentArgs;
|
|
18
16
|
currentArgs = null;
|
|
19
|
-
|
|
20
17
|
try {
|
|
21
18
|
running = true;
|
|
22
19
|
await func(...args);
|
|
@@ -24,12 +21,10 @@ function asyncThrottle(func, {
|
|
|
24
21
|
onError(error);
|
|
25
22
|
} finally {
|
|
26
23
|
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
27
|
-
|
|
28
24
|
running = false;
|
|
29
25
|
}
|
|
30
26
|
}
|
|
31
27
|
};
|
|
32
|
-
|
|
33
28
|
const delayFunc = async () => {
|
|
34
29
|
clearTimeout(timeout);
|
|
35
30
|
timeout = setTimeout(() => {
|
|
@@ -40,11 +35,9 @@ function asyncThrottle(func, {
|
|
|
40
35
|
}
|
|
41
36
|
}, interval);
|
|
42
37
|
};
|
|
43
|
-
|
|
44
38
|
return (...args) => {
|
|
45
39
|
currentArgs = args;
|
|
46
40
|
const tooSoon = Date.now() - lastTime < interval;
|
|
47
|
-
|
|
48
41
|
if (running || tooSoon) {
|
|
49
42
|
delayFunc();
|
|
50
43
|
} else {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asyncThrottle.esm.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;
|
|
1
|
+
{"version":3,"file":"asyncThrottle.esm.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;AAAA,CACD,CAAA;AAEM,SAASC,aAAa,CAC3BC,IAAsC,EACtC;AAAEC,EAAAA,QAAQ,GAAG,IAAI;AAAEC,EAAAA,OAAO,GAAGJ,IAAAA;AAA2B,CAAC,GAAG,EAAE,EAC9D;EACA,IAAI,OAAOE,IAAI,KAAK,UAAU,EAAE,MAAM,IAAIG,KAAK,CAAC,2BAA2B,CAAC,CAAA;EAE5E,IAAIC,OAAO,GAAG,KAAK,CAAA;EAC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Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
const noop = () => {
|
|
6
4
|
/* do nothing */
|
|
7
5
|
};
|
|
8
|
-
|
|
9
6
|
function asyncThrottle(func, {
|
|
10
7
|
interval = 1000,
|
|
11
8
|
onError = noop
|
|
@@ -15,12 +12,10 @@ function asyncThrottle(func, {
|
|
|
15
12
|
let lastTime = 0;
|
|
16
13
|
let timeout;
|
|
17
14
|
let currentArgs = null;
|
|
18
|
-
|
|
19
15
|
const execFunc = async () => {
|
|
20
16
|
if (currentArgs) {
|
|
21
17
|
const args = currentArgs;
|
|
22
18
|
currentArgs = null;
|
|
23
|
-
|
|
24
19
|
try {
|
|
25
20
|
running = true;
|
|
26
21
|
await func(...args);
|
|
@@ -28,12 +23,10 @@ function asyncThrottle(func, {
|
|
|
28
23
|
onError(error);
|
|
29
24
|
} finally {
|
|
30
25
|
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
31
|
-
|
|
32
26
|
running = false;
|
|
33
27
|
}
|
|
34
28
|
}
|
|
35
29
|
};
|
|
36
|
-
|
|
37
30
|
const delayFunc = async () => {
|
|
38
31
|
clearTimeout(timeout);
|
|
39
32
|
timeout = setTimeout(() => {
|
|
@@ -44,11 +37,9 @@ function asyncThrottle(func, {
|
|
|
44
37
|
}
|
|
45
38
|
}, interval);
|
|
46
39
|
};
|
|
47
|
-
|
|
48
40
|
return (...args) => {
|
|
49
41
|
currentArgs = args;
|
|
50
42
|
const tooSoon = Date.now() - lastTime < interval;
|
|
51
|
-
|
|
52
43
|
if (running || tooSoon) {
|
|
53
44
|
delayFunc();
|
|
54
45
|
} else {
|
|
@@ -1 +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":"
|
|
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;AAAA,CACD,CAAA;AAEM,SAASC,aAAa,CAC3BC,IAAsC,EACtC;AAAEC,EAAAA,QAAQ,GAAG,IAAI;AAAEC,EAAAA,OAAO,GAAGJ,IAAAA;AAA2B,CAAC,GAAG,EAAE,EAC9D;EACA,IAAI,OAAOE,IAAI,KAAK,UAAU,EAAE,MAAM,IAAIG,KAAK,CAAC,2BAA2B,CAAC,CAAA;EAE5E,IAAIC,OAAO,GAAG,KAAK,CAAA;EACnB,IAAIC,QAAQ,GAAG,CAAC,CAAA;AAChB,EAAA,IAAIC,OAAsC,CAAA;EAC1C,IAAIC,WAAwB,GAAG,IAAI,CAAA;EAEnC,MAAMC,QAAQ,GAAG,YAAY;AAC3B,IAAA,IAAID,WAAW,EAAE;MACf,MAAME,IAAI,GAAGF,WAAW,CAAA;AACxBA,MAAAA,WAAW,GAAG,IAAI,CAAA;MAClB,IAAI;AACFH,QAAAA,OAAO,GAAG,IAAI,CAAA;AACd,QAAA,MAAMJ,IAAI,CAAC,GAAGS,IAAI,CAAC,CAAA;OACpB,CAAC,OAAOC,KAAK,EAAE;QACdR,OAAO,CAACQ,KAAK,CAAC,CAAA;AAChB,OAAC,SAAS;AACRL,QAAAA,QAAQ,GAAGM,IAAI,CAACC,GAAG,EAAE,CAAC;AACtBR,QAAAA,OAAO,GAAG,KAAK,CAAA;AACjB,OAAA;AACF,KAAA;GACD,CAAA;EAED,MAAMS,SAAS,GAAG,YAAY;IAC5BC,YAAY,CAACR,OAAO,CAAC,CAAA;IACrBA,OAAO,GAAGS,UAAU,CAAC,MAAM;AACzB,MAAA,IAAIX,OAAO,EAAE;AACXS,QAAAA,SAAS,EAAE,CAAC;AACd,OAAC,MAAM;AACLL,QAAAA,QAAQ,EAAE,CAAA;AACZ,OAAA;KACD,EAAEP,QAAQ,CAAC,CAAA;GACb,CAAA;EAED,OAAO,CAAC,GAAGQ,IAAU,KAAK;AACxBF,IAAAA,WAAW,GAAGE,IAAI,CAAA;IAElB,MAAMO,OAAO,GAAGL,IAAI,CAACC,GAAG,EAAE,GAAGP,QAAQ,GAAGJ,QAAQ,CAAA;IAChD,IAAIG,OAAO,IAAIY,OAAO,EAAE;AACtBH,MAAAA,SAAS,EAAE,CAAA;AACb,KAAC,MAAM;AACLL,MAAAA,QAAQ,EAAE,CAAA;AACZ,KAAA;GACD,CAAA;AACH;;;;"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const noop = () => {
|
|
2
2
|
/* do nothing */
|
|
3
3
|
};
|
|
4
|
-
|
|
5
4
|
function asyncThrottle(func, {
|
|
6
5
|
interval = 1000,
|
|
7
6
|
onError = noop
|
|
@@ -11,12 +10,10 @@ function asyncThrottle(func, {
|
|
|
11
10
|
let lastTime = 0;
|
|
12
11
|
let timeout;
|
|
13
12
|
let currentArgs = null;
|
|
14
|
-
|
|
15
13
|
const execFunc = async () => {
|
|
16
14
|
if (currentArgs) {
|
|
17
15
|
const args = currentArgs;
|
|
18
16
|
currentArgs = null;
|
|
19
|
-
|
|
20
17
|
try {
|
|
21
18
|
running = true;
|
|
22
19
|
await func(...args);
|
|
@@ -24,12 +21,10 @@ function asyncThrottle(func, {
|
|
|
24
21
|
onError(error);
|
|
25
22
|
} finally {
|
|
26
23
|
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
27
|
-
|
|
28
24
|
running = false;
|
|
29
25
|
}
|
|
30
26
|
}
|
|
31
27
|
};
|
|
32
|
-
|
|
33
28
|
const delayFunc = async () => {
|
|
34
29
|
clearTimeout(timeout);
|
|
35
30
|
timeout = setTimeout(() => {
|
|
@@ -40,11 +35,9 @@ function asyncThrottle(func, {
|
|
|
40
35
|
}
|
|
41
36
|
}, interval);
|
|
42
37
|
};
|
|
43
|
-
|
|
44
38
|
return (...args) => {
|
|
45
39
|
currentArgs = args;
|
|
46
40
|
const tooSoon = Date.now() - lastTime < interval;
|
|
47
|
-
|
|
48
41
|
if (running || tooSoon) {
|
|
49
42
|
delayFunc();
|
|
50
43
|
} else {
|
|
@@ -1 +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;
|
|
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;AAAA,CACD,CAAA;AAEM,SAASC,aAAa,CAC3BC,IAAsC,EACtC;AAAEC,EAAAA,QAAQ,GAAG,IAAI;AAAEC,EAAAA,OAAO,GAAGJ,IAAAA;AAA2B,CAAC,GAAG,EAAE,EAC9D;EACA,IAAI,OAAOE,IAAI,KAAK,UAAU,EAAE,MAAM,IAAIG,KAAK,CAAC,2BAA2B,CAAC,CAAA;EAE5E,IAAIC,OAAO,GAAG,KAAK,CAAA;EACnB,IAAIC,QAAQ,GAAG,CAAC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3
3
|
const createAsyncStoragePersister = ({
|
|
4
4
|
storage,
|
|
5
|
-
key =
|
|
5
|
+
key = `REACT_QUERY_OFFLINE_CACHE`,
|
|
6
6
|
throttleTime = 1000,
|
|
7
7
|
serialize = JSON.stringify,
|
|
8
8
|
deserialize = JSON.parse,
|
|
@@ -17,21 +17,18 @@ const createAsyncStoragePersister = ({
|
|
|
17
17
|
return error;
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
|
|
21
20
|
return {
|
|
22
21
|
persistClient: asyncThrottle(async persistedClient => {
|
|
23
22
|
let client = persistedClient;
|
|
24
23
|
let error = await trySave(client);
|
|
25
24
|
let errorCount = 0;
|
|
26
|
-
|
|
27
25
|
while (error && client) {
|
|
28
26
|
errorCount++;
|
|
29
|
-
client = await
|
|
27
|
+
client = await retry?.({
|
|
30
28
|
persistedClient: client,
|
|
31
29
|
error,
|
|
32
30
|
errorCount
|
|
33
|
-
})
|
|
34
|
-
|
|
31
|
+
});
|
|
35
32
|
if (client) {
|
|
36
33
|
error = await trySave(client);
|
|
37
34
|
}
|
|
@@ -41,24 +38,22 @@ const createAsyncStoragePersister = ({
|
|
|
41
38
|
}),
|
|
42
39
|
restoreClient: async () => {
|
|
43
40
|
const cacheString = await storage.getItem(key);
|
|
44
|
-
|
|
45
41
|
if (!cacheString) {
|
|
46
42
|
return;
|
|
47
43
|
}
|
|
48
|
-
|
|
49
44
|
return deserialize(cacheString);
|
|
50
45
|
},
|
|
51
46
|
removeClient: () => storage.removeItem(key)
|
|
52
47
|
};
|
|
53
48
|
}
|
|
54
|
-
|
|
55
49
|
return {
|
|
56
50
|
persistClient: noop,
|
|
57
51
|
restoreClient: () => Promise.resolve(undefined),
|
|
58
52
|
removeClient: noop
|
|
59
53
|
};
|
|
60
|
-
};
|
|
54
|
+
};
|
|
61
55
|
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
62
57
|
function noop() {}
|
|
63
58
|
|
|
64
59
|
export { createAsyncStoragePersister };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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)
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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"],"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,OAAO;AACPC,EAAAA,GAAG,GAAI,CAA0B,yBAAA,CAAA;AACjCC,EAAAA,YAAY,GAAG,IAAI;EACnBC,SAAS,GAAGC,IAAI,CAACC,SAAS;EAC1BC,WAAW,GAAGF,IAAI,CAACG,KAAK;AACxBC,EAAAA,KAAAA;AACkC,CAAC,KAAgB;AACnD,EAAA,IAAI,OAAOR,OAAO,KAAK,WAAW,EAAE;AAClC,IAAA,MAAMS,OAAO,GAAG,MACdC,eAAgC,IACD;MAC/B,IAAI;QACF,MAAMV,OAAO,CAACW,OAAO,CAACV,GAAG,EAAEE,SAAS,CAACO,eAAe,CAAC,CAAC,CAAA;AACtD,QAAA,OAAA;OACD,CAAC,OAAOE,KAAK,EAAE;AACd,QAAA,OAAOA,KAAK,CAAA;AACd,OAAA;KACD,CAAA;IAED,OAAO;AACLC,MAAAA,aAAa,EAAEC,aAAa,CAC1B,MAAOJ,eAAe,IAAK;QACzB,IAAIK,MAAmC,GAAGL,eAAe,CAAA;AACzD,QAAA,IAAIE,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAM,CAAC,CAAA;QACjC,IAAIC,UAAU,GAAG,CAAC,CAAA;QAClB,OAAOJ,KAAK,IAAIG,MAAM,EAAE;AACtBC,UAAAA,UAAU,EAAE,CAAA;UACZD,MAAM,GAAG,MAAMP,KAAK,GAAG;AACrBE,YAAAA,eAAe,EAAEK,MAAM;YACvBH,KAAK;AACLI,YAAAA,UAAAA;AACF,WAAC,CAAC,CAAA;AAEF,UAAA,IAAID,MAAM,EAAE;AACVH,YAAAA,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAM,CAAC,CAAA;AAC/B,WAAA;AACF,SAAA;AACF,OAAC,EACD;AAAEE,QAAAA,QAAQ,EAAEf,YAAAA;AAAa,OAAC,CAC3B;AACDgB,MAAAA,aAAa,EAAE,YAAY;QACzB,MAAMC,WAAW,GAAG,MAAMnB,OAAO,CAACoB,OAAO,CAACnB,GAAG,CAAC,CAAA;QAE9C,IAAI,CAACkB,WAAW,EAAE;AAChB,UAAA,OAAA;AACF,SAAA;QAEA,OAAOb,WAAW,CAACa,WAAW,CAAC,CAAA;OAChC;AACDE,MAAAA,YAAY,EAAE,MAAMrB,OAAO,CAACsB,UAAU,CAACrB,GAAG,CAAA;KAC3C,CAAA;AACH,GAAA;EAEA,OAAO;AACLY,IAAAA,aAAa,EAAEU,IAAI;AACnBL,IAAAA,aAAa,EAAE,MAAMM,OAAO,CAACC,OAAO,CAACC,SAAS,CAAC;AAC/CL,IAAAA,YAAY,EAAEE,IAAAA;GACf,CAAA;AACH,EAAC;;AAED;AACA,SAASA,IAAI,GAAG;;;;"}
|
package/build/lib/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
3
|
var asyncThrottle = require('./asyncThrottle.js');
|
|
6
4
|
|
|
7
5
|
const createAsyncStoragePersister = ({
|
|
8
6
|
storage,
|
|
9
|
-
key =
|
|
7
|
+
key = `REACT_QUERY_OFFLINE_CACHE`,
|
|
10
8
|
throttleTime = 1000,
|
|
11
9
|
serialize = JSON.stringify,
|
|
12
10
|
deserialize = JSON.parse,
|
|
@@ -21,21 +19,18 @@ const createAsyncStoragePersister = ({
|
|
|
21
19
|
return error;
|
|
22
20
|
}
|
|
23
21
|
};
|
|
24
|
-
|
|
25
22
|
return {
|
|
26
23
|
persistClient: asyncThrottle.asyncThrottle(async persistedClient => {
|
|
27
24
|
let client = persistedClient;
|
|
28
25
|
let error = await trySave(client);
|
|
29
26
|
let errorCount = 0;
|
|
30
|
-
|
|
31
27
|
while (error && client) {
|
|
32
28
|
errorCount++;
|
|
33
|
-
client = await
|
|
29
|
+
client = await retry?.({
|
|
34
30
|
persistedClient: client,
|
|
35
31
|
error,
|
|
36
32
|
errorCount
|
|
37
|
-
})
|
|
38
|
-
|
|
33
|
+
});
|
|
39
34
|
if (client) {
|
|
40
35
|
error = await trySave(client);
|
|
41
36
|
}
|
|
@@ -45,24 +40,22 @@ const createAsyncStoragePersister = ({
|
|
|
45
40
|
}),
|
|
46
41
|
restoreClient: async () => {
|
|
47
42
|
const cacheString = await storage.getItem(key);
|
|
48
|
-
|
|
49
43
|
if (!cacheString) {
|
|
50
44
|
return;
|
|
51
45
|
}
|
|
52
|
-
|
|
53
46
|
return deserialize(cacheString);
|
|
54
47
|
},
|
|
55
48
|
removeClient: () => storage.removeItem(key)
|
|
56
49
|
};
|
|
57
50
|
}
|
|
58
|
-
|
|
59
51
|
return {
|
|
60
52
|
persistClient: noop,
|
|
61
53
|
restoreClient: () => Promise.resolve(undefined),
|
|
62
54
|
removeClient: noop
|
|
63
55
|
};
|
|
64
|
-
};
|
|
56
|
+
};
|
|
65
57
|
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
66
59
|
function noop() {}
|
|
67
60
|
|
|
68
61
|
exports.createAsyncStoragePersister = createAsyncStoragePersister;
|
package/build/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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)
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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"],"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,OAAO;AACPC,EAAAA,GAAG,GAAI,CAA0B,yBAAA,CAAA;AACjCC,EAAAA,YAAY,GAAG,IAAI;EACnBC,SAAS,GAAGC,IAAI,CAACC,SAAS;EAC1BC,WAAW,GAAGF,IAAI,CAACG,KAAK;AACxBC,EAAAA,KAAAA;AACkC,CAAC,KAAgB;AACnD,EAAA,IAAI,OAAOR,OAAO,KAAK,WAAW,EAAE;AAClC,IAAA,MAAMS,OAAO,GAAG,MACdC,eAAgC,IACD;MAC/B,IAAI;QACF,MAAMV,OAAO,CAACW,OAAO,CAACV,GAAG,EAAEE,SAAS,CAACO,eAAe,CAAC,CAAC,CAAA;AACtD,QAAA,OAAA;OACD,CAAC,OAAOE,KAAK,EAAE;AACd,QAAA,OAAOA,KAAK,CAAA;AACd,OAAA;KACD,CAAA;IAED,OAAO;AACLC,MAAAA,aAAa,EAAEC,2BAAa,CAC1B,MAAOJ,eAAe,IAAK;QACzB,IAAIK,MAAmC,GAAGL,eAAe,CAAA;AACzD,QAAA,IAAIE,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAM,CAAC,CAAA;QACjC,IAAIC,UAAU,GAAG,CAAC,CAAA;QAClB,OAAOJ,KAAK,IAAIG,MAAM,EAAE;AACtBC,UAAAA,UAAU,EAAE,CAAA;UACZD,MAAM,GAAG,MAAMP,KAAK,GAAG;AACrBE,YAAAA,eAAe,EAAEK,MAAM;YACvBH,KAAK;AACLI,YAAAA,UAAAA;AACF,WAAC,CAAC,CAAA;AAEF,UAAA,IAAID,MAAM,EAAE;AACVH,YAAAA,KAAK,GAAG,MAAMH,OAAO,CAACM,MAAM,CAAC,CAAA;AAC/B,WAAA;AACF,SAAA;AACF,OAAC,EACD;AAAEE,QAAAA,QAAQ,EAAEf,YAAAA;AAAa,OAAC,CAC3B;AACDgB,MAAAA,aAAa,EAAE,YAAY;QACzB,MAAMC,WAAW,GAAG,MAAMnB,OAAO,CAACoB,OAAO,CAACnB,GAAG,CAAC,CAAA;QAE9C,IAAI,CAACkB,WAAW,EAAE;AAChB,UAAA,OAAA;AACF,SAAA;QAEA,OAAOb,WAAW,CAACa,WAAW,CAAC,CAAA;OAChC;AACDE,MAAAA,YAAY,EAAE,MAAMrB,OAAO,CAACsB,UAAU,CAACrB,GAAG,CAAA;KAC3C,CAAA;AACH,GAAA;EAEA,OAAO;AACLY,IAAAA,aAAa,EAAEU,IAAI;AACnBL,IAAAA,aAAa,EAAE,MAAMM,OAAO,CAACC,OAAO,CAACC,SAAS,CAAC;AAC/CL,IAAAA,YAAY,EAAEE,IAAAA;GACf,CAAA;AACH,EAAC;;AAED;AACA,SAASA,IAAI,GAAG;;;;"}
|
package/build/lib/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { asyncThrottle } from './asyncThrottle.mjs';
|
|
|
2
2
|
|
|
3
3
|
const createAsyncStoragePersister = ({
|
|
4
4
|
storage,
|
|
5
|
-
key =
|
|
5
|
+
key = `REACT_QUERY_OFFLINE_CACHE`,
|
|
6
6
|
throttleTime = 1000,
|
|
7
7
|
serialize = JSON.stringify,
|
|
8
8
|
deserialize = JSON.parse,
|
|
@@ -17,21 +17,18 @@ const createAsyncStoragePersister = ({
|
|
|
17
17
|
return error;
|
|
18
18
|
}
|
|
19
19
|
};
|
|
20
|
-
|
|
21
20
|
return {
|
|
22
21
|
persistClient: asyncThrottle(async persistedClient => {
|
|
23
22
|
let client = persistedClient;
|
|
24
23
|
let error = await trySave(client);
|
|
25
24
|
let errorCount = 0;
|
|
26
|
-
|
|
27
25
|
while (error && client) {
|
|
28
26
|
errorCount++;
|
|
29
|
-
client = await
|
|
27
|
+
client = await retry?.({
|
|
30
28
|
persistedClient: client,
|
|
31
29
|
error,
|
|
32
30
|
errorCount
|
|
33
|
-
})
|
|
34
|
-
|
|
31
|
+
});
|
|
35
32
|
if (client) {
|
|
36
33
|
error = await trySave(client);
|
|
37
34
|
}
|
|
@@ -41,24 +38,22 @@ const createAsyncStoragePersister = ({
|
|
|
41
38
|
}),
|
|
42
39
|
restoreClient: async () => {
|
|
43
40
|
const cacheString = await storage.getItem(key);
|
|
44
|
-
|
|
45
41
|
if (!cacheString) {
|
|
46
42
|
return;
|
|
47
43
|
}
|
|
48
|
-
|
|
49
44
|
return deserialize(cacheString);
|
|
50
45
|
},
|
|
51
46
|
removeClient: () => storage.removeItem(key)
|
|
52
47
|
};
|
|
53
48
|
}
|
|
54
|
-
|
|
55
49
|
return {
|
|
56
50
|
persistClient: noop,
|
|
57
51
|
restoreClient: () => Promise.resolve(undefined),
|
|
58
52
|
removeClient: noop
|
|
59
53
|
};
|
|
60
|
-
};
|
|
54
|
+
};
|
|
61
55
|
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
62
57
|
function noop() {}
|
|
63
58
|
|
|
64
59
|
export { createAsyncStoragePersister };
|
package/build/lib/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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)
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../src/index.ts"],"sourcesContent":["import type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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"],"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,OAAO;AACPC,EAAAA,GAAG,GAAI,CAA0B,yBAAA,CAAA;AACjCC,EAAAA,YAAY,GAAG,IAAI;EACnBC,SAAS,GAAGC,IAAI,CAACC,SAAS;EAC1BC,WAAW,GAAGF,IAAI,CAACG,KAAK;AAC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;KAC3C,CAAA;AACH,GAAA;EAEA,OAAO;AACLY,IAAAA,aAAa,EAAEU,IAAI;AACnBL,IAAAA,aAAa,EAAE,MAAMM,OAAO,CAACC,OAAO,CAACC,SAAS,CAAC;AAC/CL,IAAAA,YAAY,EAAEE,IAAAA;GACf,CAAA;AACH,EAAC;;AAED;AACA,SAASA,IAAI,GAAG;;;;"}
|
|
@@ -7,7 +7,6 @@
|
|
|
7
7
|
const noop$1 = () => {
|
|
8
8
|
/* do nothing */
|
|
9
9
|
};
|
|
10
|
-
|
|
11
10
|
function asyncThrottle(func, {
|
|
12
11
|
interval = 1000,
|
|
13
12
|
onError = noop$1
|
|
@@ -17,12 +16,10 @@
|
|
|
17
16
|
let lastTime = 0;
|
|
18
17
|
let timeout;
|
|
19
18
|
let currentArgs = null;
|
|
20
|
-
|
|
21
19
|
const execFunc = async () => {
|
|
22
20
|
if (currentArgs) {
|
|
23
21
|
const args = currentArgs;
|
|
24
22
|
currentArgs = null;
|
|
25
|
-
|
|
26
23
|
try {
|
|
27
24
|
running = true;
|
|
28
25
|
await func(...args);
|
|
@@ -30,12 +27,10 @@
|
|
|
30
27
|
onError(error);
|
|
31
28
|
} finally {
|
|
32
29
|
lastTime = Date.now(); // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
33
|
-
|
|
34
30
|
running = false;
|
|
35
31
|
}
|
|
36
32
|
}
|
|
37
33
|
};
|
|
38
|
-
|
|
39
34
|
const delayFunc = async () => {
|
|
40
35
|
clearTimeout(timeout);
|
|
41
36
|
timeout = setTimeout(() => {
|
|
@@ -46,11 +41,9 @@
|
|
|
46
41
|
}
|
|
47
42
|
}, interval);
|
|
48
43
|
};
|
|
49
|
-
|
|
50
44
|
return (...args) => {
|
|
51
45
|
currentArgs = args;
|
|
52
46
|
const tooSoon = Date.now() - lastTime < interval;
|
|
53
|
-
|
|
54
47
|
if (running || tooSoon) {
|
|
55
48
|
delayFunc();
|
|
56
49
|
} else {
|
|
@@ -61,7 +54,7 @@
|
|
|
61
54
|
|
|
62
55
|
const createAsyncStoragePersister = ({
|
|
63
56
|
storage,
|
|
64
|
-
key =
|
|
57
|
+
key = `REACT_QUERY_OFFLINE_CACHE`,
|
|
65
58
|
throttleTime = 1000,
|
|
66
59
|
serialize = JSON.stringify,
|
|
67
60
|
deserialize = JSON.parse,
|
|
@@ -76,21 +69,18 @@
|
|
|
76
69
|
return error;
|
|
77
70
|
}
|
|
78
71
|
};
|
|
79
|
-
|
|
80
72
|
return {
|
|
81
73
|
persistClient: asyncThrottle(async persistedClient => {
|
|
82
74
|
let client = persistedClient;
|
|
83
75
|
let error = await trySave(client);
|
|
84
76
|
let errorCount = 0;
|
|
85
|
-
|
|
86
77
|
while (error && client) {
|
|
87
78
|
errorCount++;
|
|
88
|
-
client = await
|
|
79
|
+
client = await retry?.({
|
|
89
80
|
persistedClient: client,
|
|
90
81
|
error,
|
|
91
82
|
errorCount
|
|
92
|
-
})
|
|
93
|
-
|
|
83
|
+
});
|
|
94
84
|
if (client) {
|
|
95
85
|
error = await trySave(client);
|
|
96
86
|
}
|
|
@@ -100,29 +90,25 @@
|
|
|
100
90
|
}),
|
|
101
91
|
restoreClient: async () => {
|
|
102
92
|
const cacheString = await storage.getItem(key);
|
|
103
|
-
|
|
104
93
|
if (!cacheString) {
|
|
105
94
|
return;
|
|
106
95
|
}
|
|
107
|
-
|
|
108
96
|
return deserialize(cacheString);
|
|
109
97
|
},
|
|
110
98
|
removeClient: () => storage.removeItem(key)
|
|
111
99
|
};
|
|
112
100
|
}
|
|
113
|
-
|
|
114
101
|
return {
|
|
115
102
|
persistClient: noop,
|
|
116
103
|
restoreClient: () => Promise.resolve(undefined),
|
|
117
104
|
removeClient: noop
|
|
118
105
|
};
|
|
119
|
-
};
|
|
106
|
+
};
|
|
120
107
|
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
121
109
|
function noop() {}
|
|
122
110
|
|
|
123
111
|
exports.createAsyncStoragePersister = createAsyncStoragePersister;
|
|
124
112
|
|
|
125
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
126
|
-
|
|
127
113
|
}));
|
|
128
114
|
//# sourceMappingURL=index.development.js.map
|
|
@@ -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 type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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)
|
|
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 type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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"],"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;EAAA,CACD,CAAA;EAEM,SAASC,aAAa,CAC3BC,IAAsC,EACtC;EAAEC,EAAAA,QAAQ,GAAG,IAAI;EAAEC,EAAAA,OAAO,GAAGJ,MAAAA;EAA2B,CAAC,GAAG,EAAE,EAC9D;IACA,IAAI,OAAOE,IAAI,KAAK,UAAU,EAAE,MAAM,IAAIG,KAAK,CAAC,2BAA2B,CAAC,CAAA;IAE5E,IAAIC,OAAO,GAAG,KAAK,CAAA;IACnB,IAAIC,QAAQ,GAAG,CAAC,CAAA;EAChB,EAAA,IAAIC,OAAsC,CAAA;IAC1C,IAAIC,WAAwB,GAAG,IAAI,CAAA;IAEnC,MAAMC,QAAQ,GAAG,YAAY;EAC3B,IAAA,IAAID,WAAW,EAAE;QACf,MAAME,IAAI,GAAGF,WAAW,CAAA;EACxBA,MAAAA,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AAA,aAAa,EAAE,MAAMK,OAAO,CAACC,OAAO,CAACC,SAAS,CAAC;EAC/CJ,IAAAA,YAAY,EAAEtC,IAAAA;KACf,CAAA;EACH,EAAC;;EAED;EACA,SAASA,IAAI,GAAG;;;;;;;;"}
|
|
@@ -1,2 +1,2 @@
|
|
|
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
|
|
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 c=async()=>{if(a){const t=a;a=null;try{o=!0,await e(...t)}catch(e){n(e)}finally{s=Date.now(),o=!1}}},l=async()=>{clearTimeout(i),i=setTimeout((()=>{o?l():c()}),r)};return(...e)=>{a=e;const t=Date.now()-s<r;o||t?l():c()}}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{return void 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(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}}}));
|
|
2
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 type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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)
|
|
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 type {\n PersistedClient,\n Persister,\n Promisable,\n} from '@tanstack/query-persist-client-core'\nimport { asyncThrottle } from './asyncThrottle'\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`.\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 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"],"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,IAAIC,QAAEA,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,KCmDN,SAASV,mCA9DkC,EACzCoB,UACAC,MAAO,4BACPC,eAAe,IACfC,YAAYC,KAAKC,UACjBC,cAAcF,KAAKG,MACnBC,YAEA,QAAuB,IAAZR,EAAyB,CAClC,MAAMS,EAAUlB,UAGd,IAEE,kBADMS,EAAQU,QAAQT,EAAKE,EAAUQ,IAErC,MAAOlB,GACP,OAAOA,IAIX,MAAO,CACLmB,cAAe/B,GACbU,UACE,IAAIsB,EAAsCF,EACtClB,QAAcgB,EAAQI,GACtBC,EAAa,EACjB,KAAOrB,GAASoB,GACdC,IACAD,QAAeL,IAAQ,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": "
|
|
3
|
+
"version": "5.0.0-alpha.1",
|
|
4
4
|
"description": "A persister for asynchronous storages, to be used with TanStack/Query",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"src"
|
|
29
29
|
],
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@tanstack/query-persist-client-core": "
|
|
31
|
+
"@tanstack/query-persist-client-core": "5.0.0-alpha.1"
|
|
32
32
|
},
|
|
33
33
|
"scripts": {
|
|
34
34
|
"clean": "rimraf ./build",
|