@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.
@@ -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 running = false;
31
- let lastTime = 0;
32
- let timeout;
33
- let currentArgs = null;
34
- const execFunc = async () => {
35
- if (currentArgs) {
36
- const args = currentArgs;
37
- currentArgs = null;
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
- } finally {
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 running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: TArgs | 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: TArgs) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;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,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA4B;AAEhC,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAgB;AACzB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
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 running = false;
7
- let lastTime = 0;
8
- let timeout;
9
- let currentArgs = null;
10
- const execFunc = async () => {
11
- if (currentArgs) {
12
- const args = currentArgs;
13
- currentArgs = null;
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
- } finally {
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 running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: TArgs | 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: TArgs) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":";AAAA,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,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA4B;AAEhC,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAgB;AACzB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
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 running = false;
31
- let lastTime = 0;
32
- let timeout;
33
- let currentArgs = null;
34
- const execFunc = async () => {
35
- if (currentArgs) {
36
- const args = currentArgs;
37
- currentArgs = null;
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
- } finally {
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 running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: TArgs | 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: TArgs) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;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,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA4B;AAEhC,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAgB;AACzB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
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 running = false;
7
- let lastTime = 0;
8
- let timeout;
9
- let currentArgs = null;
10
- const execFunc = async () => {
11
- if (currentArgs) {
12
- const args = currentArgs;
13
- currentArgs = null;
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
- } finally {
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 running = false\n let lastTime = 0\n let timeout: ReturnType<typeof setTimeout>\n let currentArgs: TArgs | 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: TArgs) => {\n currentArgs = args\n\n const tooSoon = Date.now() - lastTime < interval\n if (running || tooSoon) {\n delayFunc()\n } else {\n execFunc()\n }\n }\n}\n"],"mappings":";AAAA,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,UAAU;AACd,MAAI,WAAW;AACf,MAAI;AACJ,MAAI,cAA4B;AAEhC,QAAM,WAAW,YAAY;AAC3B,QAAI,aAAa;AACf,YAAM,OAAO;AACb,oBAAc;AACd,UAAI;AACF,kBAAU;AACV,cAAM,KAAK,GAAG,IAAI;AAAA,MACpB,SAAS,OAAO;AACd,gBAAQ,KAAK;AAAA,MACf,UAAE;AACA,mBAAW,KAAK,IAAI;AACpB,kBAAU;AAAA,MACZ;AAAA,IACF;AAAA,EACF;AAEA,QAAM,YAAY,YAAY;AAC5B,iBAAa,OAAO;AACpB,cAAU,WAAW,MAAM;AACzB,UAAI,SAAS;AACX,kBAAU;AAAA,MACZ,OAAO;AACL,iBAAS;AAAA,MACX;AAAA,IACF,GAAG,QAAQ;AAAA,EACb;AAEA,SAAO,IAAI,SAAgB;AACzB,kBAAc;AAEd,UAAM,UAAU,KAAK,IAAI,IAAI,WAAW;AACxC,QAAI,WAAW,SAAS;AACtB,gBAAU;AAAA,IACZ,OAAO;AACL,eAAS;AAAA,IACX;AAAA,EACF;AACF;","names":[]}
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.29.0",
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.29.0"
40
+ "@tanstack/query-persist-client-core": "5.31.0"
41
41
  },
42
42
  "scripts": {
43
43
  "clean": "rimraf ./build && rimraf ./coverage",
@@ -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 running = false
15
- let lastTime = 0
16
- let timeout: ReturnType<typeof setTimeout>
17
- let currentArgs: TArgs | null = null
14
+ let nextExecutionTime = 0
15
+ let lastArgs = null
16
+ let isExecuting = false
17
+ let isScheduled = false
18
18
 
19
- const execFunc = async () => {
20
- if (currentArgs) {
21
- const args = currentArgs
22
- currentArgs = null
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
- } finally {
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
  }