@tanstack/query-async-storage-persister 5.29.0 → 5.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/asyncThrottle.cjs +25 -33
- package/build/legacy/asyncThrottle.cjs.map +1 -1
- package/build/legacy/asyncThrottle.d.cts +1 -1
- package/build/legacy/asyncThrottle.d.ts +1 -1
- package/build/legacy/asyncThrottle.js +25 -33
- package/build/legacy/asyncThrottle.js.map +1 -1
- package/build/modern/asyncThrottle.cjs +25 -33
- package/build/modern/asyncThrottle.cjs.map +1 -1
- package/build/modern/asyncThrottle.d.cts +1 -1
- package/build/modern/asyncThrottle.d.ts +1 -1
- package/build/modern/asyncThrottle.js +25 -33
- package/build/modern/asyncThrottle.js.map +1 -1
- package/package.json +2 -2
- package/src/asyncThrottle.ts +24 -37
|
@@ -27,43 +27,35 @@ var import_utils = require("./utils.cjs");
|
|
|
27
27
|
function asyncThrottle(func, { interval = 1e3, onError = import_utils.noop } = {}) {
|
|
28
28
|
if (typeof func !== "function")
|
|
29
29
|
throw new Error("argument is not function.");
|
|
30
|
-
let
|
|
31
|
-
let
|
|
32
|
-
let
|
|
33
|
-
let
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
let nextExecutionTime = 0;
|
|
31
|
+
let lastArgs = null;
|
|
32
|
+
let isExecuting = false;
|
|
33
|
+
let isScheduled = false;
|
|
34
|
+
return async (...args) => {
|
|
35
|
+
lastArgs = args;
|
|
36
|
+
if (isScheduled)
|
|
37
|
+
return;
|
|
38
|
+
isScheduled = true;
|
|
39
|
+
while (isExecuting) {
|
|
40
|
+
await new Promise((done) => setTimeout(done, interval));
|
|
41
|
+
}
|
|
42
|
+
while (Date.now() < nextExecutionTime) {
|
|
43
|
+
await new Promise(
|
|
44
|
+
(done) => setTimeout(done, nextExecutionTime - Date.now())
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
isScheduled = false;
|
|
48
|
+
isExecuting = true;
|
|
49
|
+
try {
|
|
50
|
+
await func(...lastArgs);
|
|
51
|
+
} catch (error) {
|
|
38
52
|
try {
|
|
39
|
-
running = true;
|
|
40
|
-
await func(...args);
|
|
41
|
-
} catch (error) {
|
|
42
53
|
onError(error);
|
|
43
|
-
}
|
|
44
|
-
lastTime = Date.now();
|
|
45
|
-
running = false;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
const delayFunc = async () => {
|
|
50
|
-
clearTimeout(timeout);
|
|
51
|
-
timeout = setTimeout(() => {
|
|
52
|
-
if (running) {
|
|
53
|
-
delayFunc();
|
|
54
|
-
} else {
|
|
55
|
-
execFunc();
|
|
54
|
+
} catch {
|
|
56
55
|
}
|
|
57
|
-
}, interval);
|
|
58
|
-
};
|
|
59
|
-
return (...args) => {
|
|
60
|
-
currentArgs = args;
|
|
61
|
-
const tooSoon = Date.now() - lastTime < interval;
|
|
62
|
-
if (running || tooSoon) {
|
|
63
|
-
delayFunc();
|
|
64
|
-
} else {
|
|
65
|
-
execFunc();
|
|
66
56
|
}
|
|
57
|
+
nextExecutionTime = Date.now() + interval;
|
|
58
|
+
isExecuting = false;
|
|
67
59
|
};
|
|
68
60
|
}
|
|
69
61
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let
|
|
1
|
+
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let nextExecutionTime = 0\n let lastArgs = null\n let isExecuting = false\n let isScheduled = false\n\n return async (...args: TArgs) => {\n lastArgs = args\n if (isScheduled) return\n isScheduled = true\n while (isExecuting) {\n await new Promise((done) => setTimeout(done, interval))\n }\n while (Date.now() < nextExecutionTime) {\n await new Promise((done) =>\n setTimeout(done, nextExecutionTime - Date.now()),\n )\n }\n isScheduled = false\n isExecuting = true\n try {\n await func(...lastArgs)\n } catch (error) {\n try {\n onError(error)\n } catch {}\n }\n nextExecutionTime = Date.now() + interval\n isExecuting = false\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAqB;AAOd,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,kBAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,oBAAoB;AACxB,MAAI,WAAW;AACf,MAAI,cAAc;AAClB,MAAI,cAAc;AAElB,SAAO,UAAU,SAAgB;AAC/B,eAAW;AACX,QAAI;AAAa;AACjB,kBAAc;AACd,WAAO,aAAa;AAClB,YAAM,IAAI,QAAQ,CAAC,SAAS,WAAW,MAAM,QAAQ,CAAC;AAAA,IACxD;AACA,WAAO,KAAK,IAAI,IAAI,mBAAmB;AACrC,YAAM,IAAI;AAAA,QAAQ,CAAC,SACjB,WAAW,MAAM,oBAAoB,KAAK,IAAI,CAAC;AAAA,MACjD;AAAA,IACF;AACA,kBAAc;AACd,kBAAc;AACd,QAAI;AACF,YAAM,KAAK,GAAG,QAAQ;AAAA,IACxB,SAAS,OAAO;AACd,UAAI;AACF,gBAAQ,KAAK;AAAA,MACf,QAAQ;AAAA,MAAC;AAAA,IACX;AACA,wBAAoB,KAAK,IAAI,IAAI;AACjC,kBAAc;AAAA,EAChB;AACF;","names":[]}
|
|
@@ -2,6 +2,6 @@ interface AsyncThrottleOptions {
|
|
|
2
2
|
interval?: number;
|
|
3
3
|
onError?: (error: unknown) => void;
|
|
4
4
|
}
|
|
5
|
-
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => void
|
|
5
|
+
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => Promise<void>;
|
|
6
6
|
|
|
7
7
|
export { type AsyncThrottleOptions, asyncThrottle };
|
|
@@ -2,6 +2,6 @@ interface AsyncThrottleOptions {
|
|
|
2
2
|
interval?: number;
|
|
3
3
|
onError?: (error: unknown) => void;
|
|
4
4
|
}
|
|
5
|
-
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => void
|
|
5
|
+
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => Promise<void>;
|
|
6
6
|
|
|
7
7
|
export { type AsyncThrottleOptions, asyncThrottle };
|
|
@@ -3,43 +3,35 @@ import { noop } from "./utils.js";
|
|
|
3
3
|
function asyncThrottle(func, { interval = 1e3, onError = noop } = {}) {
|
|
4
4
|
if (typeof func !== "function")
|
|
5
5
|
throw new Error("argument is not function.");
|
|
6
|
-
let
|
|
7
|
-
let
|
|
8
|
-
let
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
let nextExecutionTime = 0;
|
|
7
|
+
let lastArgs = null;
|
|
8
|
+
let isExecuting = false;
|
|
9
|
+
let isScheduled = false;
|
|
10
|
+
return async (...args) => {
|
|
11
|
+
lastArgs = args;
|
|
12
|
+
if (isScheduled)
|
|
13
|
+
return;
|
|
14
|
+
isScheduled = true;
|
|
15
|
+
while (isExecuting) {
|
|
16
|
+
await new Promise((done) => setTimeout(done, interval));
|
|
17
|
+
}
|
|
18
|
+
while (Date.now() < nextExecutionTime) {
|
|
19
|
+
await new Promise(
|
|
20
|
+
(done) => setTimeout(done, nextExecutionTime - Date.now())
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
isScheduled = false;
|
|
24
|
+
isExecuting = true;
|
|
25
|
+
try {
|
|
26
|
+
await func(...lastArgs);
|
|
27
|
+
} catch (error) {
|
|
14
28
|
try {
|
|
15
|
-
running = true;
|
|
16
|
-
await func(...args);
|
|
17
|
-
} catch (error) {
|
|
18
29
|
onError(error);
|
|
19
|
-
}
|
|
20
|
-
lastTime = Date.now();
|
|
21
|
-
running = false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
const delayFunc = async () => {
|
|
26
|
-
clearTimeout(timeout);
|
|
27
|
-
timeout = setTimeout(() => {
|
|
28
|
-
if (running) {
|
|
29
|
-
delayFunc();
|
|
30
|
-
} else {
|
|
31
|
-
execFunc();
|
|
30
|
+
} catch {
|
|
32
31
|
}
|
|
33
|
-
}, interval);
|
|
34
|
-
};
|
|
35
|
-
return (...args) => {
|
|
36
|
-
currentArgs = args;
|
|
37
|
-
const tooSoon = Date.now() - lastTime < interval;
|
|
38
|
-
if (running || tooSoon) {
|
|
39
|
-
delayFunc();
|
|
40
|
-
} else {
|
|
41
|
-
execFunc();
|
|
42
32
|
}
|
|
33
|
+
nextExecutionTime = Date.now() + interval;
|
|
34
|
+
isExecuting = false;
|
|
43
35
|
};
|
|
44
36
|
}
|
|
45
37
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let
|
|
1
|
+
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let nextExecutionTime = 0\n let lastArgs = null\n let isExecuting = false\n let isScheduled = false\n\n return async (...args: TArgs) => {\n lastArgs = args\n if (isScheduled) return\n isScheduled = true\n while (isExecuting) {\n await new Promise((done) => setTimeout(done, interval))\n }\n while (Date.now() < nextExecutionTime) {\n await new Promise((done) =>\n setTimeout(done, nextExecutionTime - Date.now()),\n )\n }\n isScheduled = false\n isExecuting = true\n try {\n await func(...lastArgs)\n } catch (error) {\n try {\n onError(error)\n } catch {}\n }\n nextExecutionTime = Date.now() + interval\n isExecuting = false\n }\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAOd,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,KAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,oBAAoB;AACxB,MAAI,WAAW;AACf,MAAI,cAAc;AAClB,MAAI,cAAc;AAElB,SAAO,UAAU,SAAgB;AAC/B,eAAW;AACX,QAAI;AAAa;AACjB,kBAAc;AACd,WAAO,aAAa;AAClB,YAAM,IAAI,QAAQ,CAAC,SAAS,WAAW,MAAM,QAAQ,CAAC;AAAA,IACxD;AACA,WAAO,KAAK,IAAI,IAAI,mBAAmB;AACrC,YAAM,IAAI;AAAA,QAAQ,CAAC,SACjB,WAAW,MAAM,oBAAoB,KAAK,IAAI,CAAC;AAAA,MACjD;AAAA,IACF;AACA,kBAAc;AACd,kBAAc;AACd,QAAI;AACF,YAAM,KAAK,GAAG,QAAQ;AAAA,IACxB,SAAS,OAAO;AACd,UAAI;AACF,gBAAQ,KAAK;AAAA,MACf,QAAQ;AAAA,MAAC;AAAA,IACX;AACA,wBAAoB,KAAK,IAAI,IAAI;AACjC,kBAAc;AAAA,EAChB;AACF;","names":[]}
|
|
@@ -27,43 +27,35 @@ var import_utils = require("./utils.cjs");
|
|
|
27
27
|
function asyncThrottle(func, { interval = 1e3, onError = import_utils.noop } = {}) {
|
|
28
28
|
if (typeof func !== "function")
|
|
29
29
|
throw new Error("argument is not function.");
|
|
30
|
-
let
|
|
31
|
-
let
|
|
32
|
-
let
|
|
33
|
-
let
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
let nextExecutionTime = 0;
|
|
31
|
+
let lastArgs = null;
|
|
32
|
+
let isExecuting = false;
|
|
33
|
+
let isScheduled = false;
|
|
34
|
+
return async (...args) => {
|
|
35
|
+
lastArgs = args;
|
|
36
|
+
if (isScheduled)
|
|
37
|
+
return;
|
|
38
|
+
isScheduled = true;
|
|
39
|
+
while (isExecuting) {
|
|
40
|
+
await new Promise((done) => setTimeout(done, interval));
|
|
41
|
+
}
|
|
42
|
+
while (Date.now() < nextExecutionTime) {
|
|
43
|
+
await new Promise(
|
|
44
|
+
(done) => setTimeout(done, nextExecutionTime - Date.now())
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
isScheduled = false;
|
|
48
|
+
isExecuting = true;
|
|
49
|
+
try {
|
|
50
|
+
await func(...lastArgs);
|
|
51
|
+
} catch (error) {
|
|
38
52
|
try {
|
|
39
|
-
running = true;
|
|
40
|
-
await func(...args);
|
|
41
|
-
} catch (error) {
|
|
42
53
|
onError(error);
|
|
43
|
-
}
|
|
44
|
-
lastTime = Date.now();
|
|
45
|
-
running = false;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
const delayFunc = async () => {
|
|
50
|
-
clearTimeout(timeout);
|
|
51
|
-
timeout = setTimeout(() => {
|
|
52
|
-
if (running) {
|
|
53
|
-
delayFunc();
|
|
54
|
-
} else {
|
|
55
|
-
execFunc();
|
|
54
|
+
} catch {
|
|
56
55
|
}
|
|
57
|
-
}, interval);
|
|
58
|
-
};
|
|
59
|
-
return (...args) => {
|
|
60
|
-
currentArgs = args;
|
|
61
|
-
const tooSoon = Date.now() - lastTime < interval;
|
|
62
|
-
if (running || tooSoon) {
|
|
63
|
-
delayFunc();
|
|
64
|
-
} else {
|
|
65
|
-
execFunc();
|
|
66
56
|
}
|
|
57
|
+
nextExecutionTime = Date.now() + interval;
|
|
58
|
+
isExecuting = false;
|
|
67
59
|
};
|
|
68
60
|
}
|
|
69
61
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let
|
|
1
|
+
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let nextExecutionTime = 0\n let lastArgs = null\n let isExecuting = false\n let isScheduled = false\n\n return async (...args: TArgs) => {\n lastArgs = args\n if (isScheduled) return\n isScheduled = true\n while (isExecuting) {\n await new Promise((done) => setTimeout(done, interval))\n }\n while (Date.now() < nextExecutionTime) {\n await new Promise((done) =>\n setTimeout(done, nextExecutionTime - Date.now()),\n )\n }\n isScheduled = false\n isExecuting = true\n try {\n await func(...lastArgs)\n } catch (error) {\n try {\n onError(error)\n } catch {}\n }\n nextExecutionTime = Date.now() + interval\n isExecuting = false\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAqB;AAOd,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,kBAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,oBAAoB;AACxB,MAAI,WAAW;AACf,MAAI,cAAc;AAClB,MAAI,cAAc;AAElB,SAAO,UAAU,SAAgB;AAC/B,eAAW;AACX,QAAI;AAAa;AACjB,kBAAc;AACd,WAAO,aAAa;AAClB,YAAM,IAAI,QAAQ,CAAC,SAAS,WAAW,MAAM,QAAQ,CAAC;AAAA,IACxD;AACA,WAAO,KAAK,IAAI,IAAI,mBAAmB;AACrC,YAAM,IAAI;AAAA,QAAQ,CAAC,SACjB,WAAW,MAAM,oBAAoB,KAAK,IAAI,CAAC;AAAA,MACjD;AAAA,IACF;AACA,kBAAc;AACd,kBAAc;AACd,QAAI;AACF,YAAM,KAAK,GAAG,QAAQ;AAAA,IACxB,SAAS,OAAO;AACd,UAAI;AACF,gBAAQ,KAAK;AAAA,MACf,QAAQ;AAAA,MAAC;AAAA,IACX;AACA,wBAAoB,KAAK,IAAI,IAAI;AACjC,kBAAc;AAAA,EAChB;AACF;","names":[]}
|
|
@@ -2,6 +2,6 @@ interface AsyncThrottleOptions {
|
|
|
2
2
|
interval?: number;
|
|
3
3
|
onError?: (error: unknown) => void;
|
|
4
4
|
}
|
|
5
|
-
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => void
|
|
5
|
+
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => Promise<void>;
|
|
6
6
|
|
|
7
7
|
export { type AsyncThrottleOptions, asyncThrottle };
|
|
@@ -2,6 +2,6 @@ interface AsyncThrottleOptions {
|
|
|
2
2
|
interval?: number;
|
|
3
3
|
onError?: (error: unknown) => void;
|
|
4
4
|
}
|
|
5
|
-
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => void
|
|
5
|
+
declare function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(func: (...args: TArgs) => Promise<void>, { interval, onError }?: AsyncThrottleOptions): (...args: TArgs) => Promise<void>;
|
|
6
6
|
|
|
7
7
|
export { type AsyncThrottleOptions, asyncThrottle };
|
|
@@ -3,43 +3,35 @@ import { noop } from "./utils.js";
|
|
|
3
3
|
function asyncThrottle(func, { interval = 1e3, onError = noop } = {}) {
|
|
4
4
|
if (typeof func !== "function")
|
|
5
5
|
throw new Error("argument is not function.");
|
|
6
|
-
let
|
|
7
|
-
let
|
|
8
|
-
let
|
|
9
|
-
let
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
let nextExecutionTime = 0;
|
|
7
|
+
let lastArgs = null;
|
|
8
|
+
let isExecuting = false;
|
|
9
|
+
let isScheduled = false;
|
|
10
|
+
return async (...args) => {
|
|
11
|
+
lastArgs = args;
|
|
12
|
+
if (isScheduled)
|
|
13
|
+
return;
|
|
14
|
+
isScheduled = true;
|
|
15
|
+
while (isExecuting) {
|
|
16
|
+
await new Promise((done) => setTimeout(done, interval));
|
|
17
|
+
}
|
|
18
|
+
while (Date.now() < nextExecutionTime) {
|
|
19
|
+
await new Promise(
|
|
20
|
+
(done) => setTimeout(done, nextExecutionTime - Date.now())
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
isScheduled = false;
|
|
24
|
+
isExecuting = true;
|
|
25
|
+
try {
|
|
26
|
+
await func(...lastArgs);
|
|
27
|
+
} catch (error) {
|
|
14
28
|
try {
|
|
15
|
-
running = true;
|
|
16
|
-
await func(...args);
|
|
17
|
-
} catch (error) {
|
|
18
29
|
onError(error);
|
|
19
|
-
}
|
|
20
|
-
lastTime = Date.now();
|
|
21
|
-
running = false;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
const delayFunc = async () => {
|
|
26
|
-
clearTimeout(timeout);
|
|
27
|
-
timeout = setTimeout(() => {
|
|
28
|
-
if (running) {
|
|
29
|
-
delayFunc();
|
|
30
|
-
} else {
|
|
31
|
-
execFunc();
|
|
30
|
+
} catch {
|
|
32
31
|
}
|
|
33
|
-
}, interval);
|
|
34
|
-
};
|
|
35
|
-
return (...args) => {
|
|
36
|
-
currentArgs = args;
|
|
37
|
-
const tooSoon = Date.now() - lastTime < interval;
|
|
38
|
-
if (running || tooSoon) {
|
|
39
|
-
delayFunc();
|
|
40
|
-
} else {
|
|
41
|
-
execFunc();
|
|
42
32
|
}
|
|
33
|
+
nextExecutionTime = Date.now() + interval;
|
|
34
|
+
isExecuting = false;
|
|
43
35
|
};
|
|
44
36
|
}
|
|
45
37
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let
|
|
1
|
+
{"version":3,"sources":["../../src/asyncThrottle.ts"],"sourcesContent":["import { noop } from './utils'\n\nexport interface AsyncThrottleOptions {\n interval?: number\n onError?: (error: unknown) => void\n}\n\nexport function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(\n func: (...args: TArgs) => Promise<void>,\n { interval = 1000, onError = noop }: AsyncThrottleOptions = {},\n) {\n if (typeof func !== 'function') throw new Error('argument is not function.')\n\n let nextExecutionTime = 0\n let lastArgs = null\n let isExecuting = false\n let isScheduled = false\n\n return async (...args: TArgs) => {\n lastArgs = args\n if (isScheduled) return\n isScheduled = true\n while (isExecuting) {\n await new Promise((done) => setTimeout(done, interval))\n }\n while (Date.now() < nextExecutionTime) {\n await new Promise((done) =>\n setTimeout(done, nextExecutionTime - Date.now()),\n )\n }\n isScheduled = false\n isExecuting = true\n try {\n await func(...lastArgs)\n } catch (error) {\n try {\n onError(error)\n } catch {}\n }\n nextExecutionTime = Date.now() + interval\n isExecuting = false\n }\n}\n"],"mappings":";AAAA,SAAS,YAAY;AAOd,SAAS,cACd,MACA,EAAE,WAAW,KAAM,UAAU,KAAK,IAA0B,CAAC,GAC7D;AACA,MAAI,OAAO,SAAS;AAAY,UAAM,IAAI,MAAM,2BAA2B;AAE3E,MAAI,oBAAoB;AACxB,MAAI,WAAW;AACf,MAAI,cAAc;AAClB,MAAI,cAAc;AAElB,SAAO,UAAU,SAAgB;AAC/B,eAAW;AACX,QAAI;AAAa;AACjB,kBAAc;AACd,WAAO,aAAa;AAClB,YAAM,IAAI,QAAQ,CAAC,SAAS,WAAW,MAAM,QAAQ,CAAC;AAAA,IACxD;AACA,WAAO,KAAK,IAAI,IAAI,mBAAmB;AACrC,YAAM,IAAI;AAAA,QAAQ,CAAC,SACjB,WAAW,MAAM,oBAAoB,KAAK,IAAI,CAAC;AAAA,MACjD;AAAA,IACF;AACA,kBAAc;AACd,kBAAc;AACd,QAAI;AACF,YAAM,KAAK,GAAG,QAAQ;AAAA,IACxB,SAAS,OAAO;AACd,UAAI;AACF,gBAAQ,KAAK;AAAA,MACf,QAAQ;AAAA,MAAC;AAAA,IACX;AACA,wBAAoB,KAAK,IAAI,IAAI;AACjC,kBAAc;AAAA,EAChB;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/query-async-storage-persister",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.31.0",
|
|
4
4
|
"description": "A persister for asynchronous storages, to be used with TanStack/Query",
|
|
5
5
|
"author": "tannerlinsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
"src"
|
|
38
38
|
],
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@tanstack/query-persist-client-core": "5.
|
|
40
|
+
"@tanstack/query-persist-client-core": "5.31.0"
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"clean": "rimraf ./build && rimraf ./coverage",
|
package/src/asyncThrottle.ts
CHANGED
|
@@ -11,46 +11,33 @@ export function asyncThrottle<TArgs extends ReadonlyArray<unknown>>(
|
|
|
11
11
|
) {
|
|
12
12
|
if (typeof func !== 'function') throw new Error('argument is not function.')
|
|
13
13
|
|
|
14
|
-
let
|
|
15
|
-
let
|
|
16
|
-
let
|
|
17
|
-
let
|
|
14
|
+
let nextExecutionTime = 0
|
|
15
|
+
let lastArgs = null
|
|
16
|
+
let isExecuting = false
|
|
17
|
+
let isScheduled = false
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
return async (...args: TArgs) => {
|
|
20
|
+
lastArgs = args
|
|
21
|
+
if (isScheduled) return
|
|
22
|
+
isScheduled = true
|
|
23
|
+
while (isExecuting) {
|
|
24
|
+
await new Promise((done) => setTimeout(done, interval))
|
|
25
|
+
}
|
|
26
|
+
while (Date.now() < nextExecutionTime) {
|
|
27
|
+
await new Promise((done) =>
|
|
28
|
+
setTimeout(done, nextExecutionTime - Date.now()),
|
|
29
|
+
)
|
|
30
|
+
}
|
|
31
|
+
isScheduled = false
|
|
32
|
+
isExecuting = true
|
|
33
|
+
try {
|
|
34
|
+
await func(...lastArgs)
|
|
35
|
+
} catch (error) {
|
|
23
36
|
try {
|
|
24
|
-
running = true
|
|
25
|
-
await func(...args)
|
|
26
|
-
} catch (error) {
|
|
27
37
|
onError(error)
|
|
28
|
-
}
|
|
29
|
-
lastTime = Date.now() // this line must after 'func' executed to avoid two 'func' running in concurrent.
|
|
30
|
-
running = false
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const delayFunc = async () => {
|
|
36
|
-
clearTimeout(timeout)
|
|
37
|
-
timeout = setTimeout(() => {
|
|
38
|
-
if (running) {
|
|
39
|
-
delayFunc() // Will come here when 'func' execution time is greater than the interval.
|
|
40
|
-
} else {
|
|
41
|
-
execFunc()
|
|
42
|
-
}
|
|
43
|
-
}, interval)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return (...args: TArgs) => {
|
|
47
|
-
currentArgs = args
|
|
48
|
-
|
|
49
|
-
const tooSoon = Date.now() - lastTime < interval
|
|
50
|
-
if (running || tooSoon) {
|
|
51
|
-
delayFunc()
|
|
52
|
-
} else {
|
|
53
|
-
execFunc()
|
|
38
|
+
} catch {}
|
|
54
39
|
}
|
|
40
|
+
nextExecutionTime = Date.now() + interval
|
|
41
|
+
isExecuting = false
|
|
55
42
|
}
|
|
56
43
|
}
|