@xylabs/timer 2.13.24 → 2.13.26

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.
@@ -20,25 +20,54 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ clearTimeoutEx: () => clearTimeoutEx,
23
24
  setTimeoutEx: () => setTimeoutEx
24
25
  });
25
26
  module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/setTimeoutEx.ts
29
+ var import_assert = require("@xylabs/assert");
26
30
  var timeouts = [];
27
- var timerFunc = () => {
28
- if (timeouts.length > 0) {
29
- setTimeout(timerFunc, 100);
31
+ var currentTimeout;
32
+ var interval = -1;
33
+ var reset = () => {
34
+ interval - 1;
35
+ clearTimeout(currentTimeout);
36
+ currentTimeout = void 0;
37
+ timeouts = [];
38
+ };
39
+ var update = (newTimeouts = timeouts, delayPassed = 0) => {
40
+ if (newTimeouts.length <= 0) {
41
+ reset();
42
+ } else {
43
+ const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay));
44
+ if (newInterval === interval && currentTimeout !== void 0) {
45
+ return;
46
+ } else {
47
+ clearTimeout(currentTimeout);
48
+ timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }));
49
+ interval = newInterval;
50
+ currentTimeout = setTimeout(timerFunc, interval);
51
+ }
30
52
  }
31
- const firing = timeouts.filter((timeout) => timeout.delay <= 100);
53
+ };
54
+ var timerFunc = () => {
55
+ const notFiring = timeouts.filter((timeout) => timeout.delay > interval);
56
+ const firing = timeouts.filter((timeout) => timeout.delay <= interval);
57
+ update(notFiring, interval);
32
58
  for (const timeout of firing) {
33
59
  timeout.func();
34
60
  }
35
- const notFiring = timeouts.filter((timeout) => timeout.delay > 100);
36
- timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }));
37
61
  };
38
62
  var setTimeoutEx = (func, delay) => {
39
- if (timeouts.length === 0) {
40
- setTimeout(timerFunc, 100);
41
- }
42
- timeouts.push({ delay, func });
63
+ (0, import_assert.assertEx)(delay >= 0, "delay must be >= 0");
64
+ const id = `${Date.now()}|${Math.random() * 9999999999}`;
65
+ timeouts.push({ delay, func, id });
66
+ update();
67
+ return id;
68
+ };
69
+ var clearTimeoutEx = (id) => {
70
+ timeouts = timeouts.filter((timeout) => timeout.id !== id);
71
+ update(timeouts);
43
72
  };
44
73
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["interface TimeoutInfo {\n delay: number\n func: () => void\n}\n\nlet timeouts: TimeoutInfo[] = []\n\nconst timerFunc = () => {\n if (timeouts.length > 0) {\n setTimeout(timerFunc, 100)\n }\n const firing = timeouts.filter((timeout) => timeout.delay <= 100)\n for (const timeout of firing) {\n timeout.func()\n }\n const notFiring = timeouts.filter((timeout) => timeout.delay > 100)\n timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }))\n}\n\nexport const setTimeoutEx = (func: () => void, delay: number) => {\n if (timeouts.length === 0) {\n setTimeout(timerFunc, 100)\n }\n timeouts.push({ delay, func })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,IAAI,WAA0B,CAAC;AAE/B,IAAM,YAAY,MAAM;AACtB,MAAI,SAAS,SAAS,GAAG;AACvB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,GAAG;AAChE,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACA,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,GAAG;AAClE,aAAW,UAAU,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,KAAK,MAAM,QAAQ,KAAK,EAAE;AAC5F;AAEO,IAAM,eAAe,CAAC,MAAkB,UAAkB;AAC/D,MAAI,SAAS,WAAW,GAAG;AACzB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,WAAS,KAAK,EAAE,OAAO,KAAK,CAAC;AAC/B;","names":[]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/setTimeoutEx.ts"],"sourcesContent":["export * from './setTimeoutEx'\n","import { assertEx } from '@xylabs/assert'\n\ninterface TimeoutInfo {\n delay: number\n // eslint-disable-next-line @typescript-eslint/ban-types\n func: Function\n id: string\n}\n\nlet timeouts: TimeoutInfo[] = []\nlet currentTimeout: number | undefined\nlet interval = -1\n\nconst reset = () => {\n interval - 1\n clearTimeout(currentTimeout)\n currentTimeout = undefined\n timeouts = []\n}\n\nconst update = (newTimeouts = timeouts, delayPassed = 0) => {\n //if no more timeouts, set back to initial state\n if (newTimeouts.length <= 0) {\n reset()\n } else {\n const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay))\n\n if (newInterval === interval && currentTimeout !== undefined) {\n //since nothing changed, just return\n return\n } else {\n clearTimeout(currentTimeout)\n timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }))\n //restart timeout since it needs to be different\n interval = newInterval\n currentTimeout = setTimeout(timerFunc, interval)\n }\n }\n}\n\nconst timerFunc = () => {\n const notFiring = timeouts.filter((timeout) => timeout.delay > interval)\n const firing = timeouts.filter((timeout) => timeout.delay <= interval)\n\n //call this after getting notFiring and firing since set will change in this call\n update(notFiring, interval)\n\n //trigger the ones that need to be triggered\n for (const timeout of firing) {\n timeout.func()\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport const setTimeoutEx = (func: Function, delay: number) => {\n assertEx(delay >= 0, 'delay must be >= 0')\n const id = `${Date.now()}|${Math.random() * 9_999_999_999}`\n timeouts.push({ delay, func, id })\n update()\n return id\n}\n\nexport const clearTimeoutEx = (id: string) => {\n timeouts = timeouts.filter((timeout) => timeout.id !== id)\n update(timeouts)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AASzB,IAAI,WAA0B,CAAC;AAC/B,IAAI;AACJ,IAAI,WAAW;AAEf,IAAM,QAAQ,MAAM;AAClB,aAAW;AACX,eAAa,cAAc;AAC3B,mBAAiB;AACjB,aAAW,CAAC;AACd;AAEA,IAAM,SAAS,CAAC,cAAc,UAAU,cAAc,MAAM;AAE1D,MAAI,YAAY,UAAU,GAAG;AAC3B,UAAM;AAAA,EACR,OAAO;AACL,UAAM,cAAc,KAAK,IAAI,GAAG,YAAY,IAAI,CAAC,YAAY,QAAQ,KAAK,CAAC;AAE3E,QAAI,gBAAgB,YAAY,mBAAmB,QAAW;AAE5D;AAAA,IACF,OAAO;AACL,mBAAa,cAAc;AAC3B,iBAAW,YAAY,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,aAAa,MAAM,QAAQ,MAAM,IAAI,QAAQ,GAAG,EAAE;AAEpH,iBAAW;AACX,uBAAiB,WAAW,WAAW,QAAQ;AAAA,IACjD;AAAA,EACF;AACF;AAEA,IAAM,YAAY,MAAM;AACtB,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,QAAQ;AACvE,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,QAAQ;AAGrE,SAAO,WAAW,QAAQ;AAG1B,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACF;AAGO,IAAM,eAAe,CAAC,MAAgB,UAAkB;AAC7D,8BAAS,SAAS,GAAG,oBAAoB;AACzC,QAAM,KAAK,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,UAAa;AACzD,WAAS,KAAK,EAAE,OAAO,MAAM,GAAG,CAAC;AACjC,SAAO;AACP,SAAO;AACT;AAEO,IAAM,iBAAiB,CAAC,OAAe;AAC5C,aAAW,SAAS,OAAO,CAAC,YAAY,QAAQ,OAAO,EAAE;AACzD,SAAO,QAAQ;AACjB;","names":[]}
@@ -1,2 +1,2 @@
1
- export declare const setTimeoutEx: (func: () => void, delay: number) => void;
1
+ export * from './setTimeoutEx';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,YAAY,SAAU,MAAM,IAAI,SAAS,MAAM,SAK3D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -1,2 +1,2 @@
1
- export declare const setTimeoutEx: (func: () => void, delay: number) => void;
1
+ export * from './setTimeoutEx';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,YAAY,SAAU,MAAM,IAAI,SAAS,MAAM,SAK3D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -1,2 +1,2 @@
1
- export declare const setTimeoutEx: (func: () => void, delay: number) => void;
1
+ export * from './setTimeoutEx';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,YAAY,SAAU,MAAM,IAAI,SAAS,MAAM,SAK3D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -1,23 +1,50 @@
1
- // src/index.ts
1
+ // src/setTimeoutEx.ts
2
+ import { assertEx } from "@xylabs/assert";
2
3
  var timeouts = [];
3
- var timerFunc = () => {
4
- if (timeouts.length > 0) {
5
- setTimeout(timerFunc, 100);
4
+ var currentTimeout;
5
+ var interval = -1;
6
+ var reset = () => {
7
+ interval - 1;
8
+ clearTimeout(currentTimeout);
9
+ currentTimeout = void 0;
10
+ timeouts = [];
11
+ };
12
+ var update = (newTimeouts = timeouts, delayPassed = 0) => {
13
+ if (newTimeouts.length <= 0) {
14
+ reset();
15
+ } else {
16
+ const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay));
17
+ if (newInterval === interval && currentTimeout !== void 0) {
18
+ return;
19
+ } else {
20
+ clearTimeout(currentTimeout);
21
+ timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }));
22
+ interval = newInterval;
23
+ currentTimeout = setTimeout(timerFunc, interval);
24
+ }
6
25
  }
7
- const firing = timeouts.filter((timeout) => timeout.delay <= 100);
26
+ };
27
+ var timerFunc = () => {
28
+ const notFiring = timeouts.filter((timeout) => timeout.delay > interval);
29
+ const firing = timeouts.filter((timeout) => timeout.delay <= interval);
30
+ update(notFiring, interval);
8
31
  for (const timeout of firing) {
9
32
  timeout.func();
10
33
  }
11
- const notFiring = timeouts.filter((timeout) => timeout.delay > 100);
12
- timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }));
13
34
  };
14
35
  var setTimeoutEx = (func, delay) => {
15
- if (timeouts.length === 0) {
16
- setTimeout(timerFunc, 100);
17
- }
18
- timeouts.push({ delay, func });
36
+ assertEx(delay >= 0, "delay must be >= 0");
37
+ const id = `${Date.now()}|${Math.random() * 9999999999}`;
38
+ timeouts.push({ delay, func, id });
39
+ update();
40
+ return id;
41
+ };
42
+ var clearTimeoutEx = (id) => {
43
+ timeouts = timeouts.filter((timeout) => timeout.id !== id);
44
+ update(timeouts);
19
45
  };
20
46
  export {
47
+ clearTimeoutEx,
21
48
  setTimeoutEx
22
49
  };
23
50
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["interface TimeoutInfo {\n delay: number\n func: () => void\n}\n\nlet timeouts: TimeoutInfo[] = []\n\nconst timerFunc = () => {\n if (timeouts.length > 0) {\n setTimeout(timerFunc, 100)\n }\n const firing = timeouts.filter((timeout) => timeout.delay <= 100)\n for (const timeout of firing) {\n timeout.func()\n }\n const notFiring = timeouts.filter((timeout) => timeout.delay > 100)\n timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }))\n}\n\nexport const setTimeoutEx = (func: () => void, delay: number) => {\n if (timeouts.length === 0) {\n setTimeout(timerFunc, 100)\n }\n timeouts.push({ delay, func })\n}\n"],"mappings":";AAKA,IAAI,WAA0B,CAAC;AAE/B,IAAM,YAAY,MAAM;AACtB,MAAI,SAAS,SAAS,GAAG;AACvB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,GAAG;AAChE,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACA,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,GAAG;AAClE,aAAW,UAAU,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,KAAK,MAAM,QAAQ,KAAK,EAAE;AAC5F;AAEO,IAAM,eAAe,CAAC,MAAkB,UAAkB;AAC/D,MAAI,SAAS,WAAW,GAAG;AACzB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,WAAS,KAAK,EAAE,OAAO,KAAK,CAAC;AAC/B;","names":[]}
1
+ {"version":3,"sources":["../../src/setTimeoutEx.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\n\ninterface TimeoutInfo {\n delay: number\n // eslint-disable-next-line @typescript-eslint/ban-types\n func: Function\n id: string\n}\n\nlet timeouts: TimeoutInfo[] = []\nlet currentTimeout: number | undefined\nlet interval = -1\n\nconst reset = () => {\n interval - 1\n clearTimeout(currentTimeout)\n currentTimeout = undefined\n timeouts = []\n}\n\nconst update = (newTimeouts = timeouts, delayPassed = 0) => {\n //if no more timeouts, set back to initial state\n if (newTimeouts.length <= 0) {\n reset()\n } else {\n const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay))\n\n if (newInterval === interval && currentTimeout !== undefined) {\n //since nothing changed, just return\n return\n } else {\n clearTimeout(currentTimeout)\n timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }))\n //restart timeout since it needs to be different\n interval = newInterval\n currentTimeout = setTimeout(timerFunc, interval)\n }\n }\n}\n\nconst timerFunc = () => {\n const notFiring = timeouts.filter((timeout) => timeout.delay > interval)\n const firing = timeouts.filter((timeout) => timeout.delay <= interval)\n\n //call this after getting notFiring and firing since set will change in this call\n update(notFiring, interval)\n\n //trigger the ones that need to be triggered\n for (const timeout of firing) {\n timeout.func()\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport const setTimeoutEx = (func: Function, delay: number) => {\n assertEx(delay >= 0, 'delay must be >= 0')\n const id = `${Date.now()}|${Math.random() * 9_999_999_999}`\n timeouts.push({ delay, func, id })\n update()\n return id\n}\n\nexport const clearTimeoutEx = (id: string) => {\n timeouts = timeouts.filter((timeout) => timeout.id !== id)\n update(timeouts)\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AASzB,IAAI,WAA0B,CAAC;AAC/B,IAAI;AACJ,IAAI,WAAW;AAEf,IAAM,QAAQ,MAAM;AAClB,aAAW;AACX,eAAa,cAAc;AAC3B,mBAAiB;AACjB,aAAW,CAAC;AACd;AAEA,IAAM,SAAS,CAAC,cAAc,UAAU,cAAc,MAAM;AAE1D,MAAI,YAAY,UAAU,GAAG;AAC3B,UAAM;AAAA,EACR,OAAO;AACL,UAAM,cAAc,KAAK,IAAI,GAAG,YAAY,IAAI,CAAC,YAAY,QAAQ,KAAK,CAAC;AAE3E,QAAI,gBAAgB,YAAY,mBAAmB,QAAW;AAE5D;AAAA,IACF,OAAO;AACL,mBAAa,cAAc;AAC3B,iBAAW,YAAY,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,aAAa,MAAM,QAAQ,MAAM,IAAI,QAAQ,GAAG,EAAE;AAEpH,iBAAW;AACX,uBAAiB,WAAW,WAAW,QAAQ;AAAA,IACjD;AAAA,EACF;AACF;AAEA,IAAM,YAAY,MAAM;AACtB,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,QAAQ;AACvE,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,QAAQ;AAGrE,SAAO,WAAW,QAAQ;AAG1B,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACF;AAGO,IAAM,eAAe,CAAC,MAAgB,UAAkB;AAC7D,WAAS,SAAS,GAAG,oBAAoB;AACzC,QAAM,KAAK,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,UAAa;AACzD,WAAS,KAAK,EAAE,OAAO,MAAM,GAAG,CAAC;AACjC,SAAO;AACP,SAAO;AACT;AAEO,IAAM,iBAAiB,CAAC,OAAe;AAC5C,aAAW,SAAS,OAAO,CAAC,YAAY,QAAQ,OAAO,EAAE;AACzD,SAAO,QAAQ;AACjB;","names":[]}
@@ -0,0 +1,3 @@
1
+ export declare const setTimeoutEx: (func: Function, delay: number) => string;
2
+ export declare const clearTimeoutEx: (id: string) => void;
3
+ //# sourceMappingURL=setTimeoutEx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,YAAY,SAAU,QAAQ,SAAS,MAAM,WAMzD,CAAA;AAED,eAAO,MAAM,cAAc,OAAQ,MAAM,SAGxC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export declare const setTimeoutEx: (func: Function, delay: number) => string;
2
+ export declare const clearTimeoutEx: (id: string) => void;
3
+ //# sourceMappingURL=setTimeoutEx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,YAAY,SAAU,QAAQ,SAAS,MAAM,WAMzD,CAAA;AAED,eAAO,MAAM,cAAc,OAAQ,MAAM,SAGxC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export declare const setTimeoutEx: (func: Function, delay: number) => string;
2
+ export declare const clearTimeoutEx: (id: string) => void;
3
+ //# sourceMappingURL=setTimeoutEx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,YAAY,SAAU,QAAQ,SAAS,MAAM,WAMzD,CAAA;AAED,eAAO,MAAM,cAAc,OAAQ,MAAM,SAGxC,CAAA"}
@@ -20,29 +20,59 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
+ clearTimeoutEx: () => clearTimeoutEx,
23
24
  setTimeoutEx: () => setTimeoutEx
24
25
  });
25
26
  module.exports = __toCommonJS(src_exports);
27
+
28
+ // src/setTimeoutEx.ts
29
+ var import_assert = require("@xylabs/assert");
26
30
  var timeouts = [];
27
- var timerFunc = () => {
28
- if (timeouts.length > 0) {
29
- setTimeout(timerFunc, 100);
31
+ var currentTimeout;
32
+ var interval = -1;
33
+ var reset = () => {
34
+ interval - 1;
35
+ clearTimeout(currentTimeout);
36
+ currentTimeout = void 0;
37
+ timeouts = [];
38
+ };
39
+ var update = (newTimeouts = timeouts, delayPassed = 0) => {
40
+ if (newTimeouts.length <= 0) {
41
+ reset();
42
+ } else {
43
+ const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay));
44
+ if (newInterval === interval && currentTimeout !== void 0) {
45
+ return;
46
+ } else {
47
+ clearTimeout(currentTimeout);
48
+ timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }));
49
+ interval = newInterval;
50
+ currentTimeout = setTimeout(timerFunc, interval);
51
+ }
30
52
  }
31
- const firing = timeouts.filter((timeout) => timeout.delay <= 100);
53
+ };
54
+ var timerFunc = () => {
55
+ const notFiring = timeouts.filter((timeout) => timeout.delay > interval);
56
+ const firing = timeouts.filter((timeout) => timeout.delay <= interval);
57
+ update(notFiring, interval);
32
58
  for (const timeout of firing) {
33
59
  timeout.func();
34
60
  }
35
- const notFiring = timeouts.filter((timeout) => timeout.delay > 100);
36
- timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }));
37
61
  };
38
62
  var setTimeoutEx = (func, delay) => {
39
- if (timeouts.length === 0) {
40
- setTimeout(timerFunc, 100);
41
- }
42
- timeouts.push({ delay, func });
63
+ (0, import_assert.assertEx)(delay >= 0, "delay must be >= 0");
64
+ const id = `${Date.now()}|${Math.random() * 9999999999}`;
65
+ timeouts.push({ delay, func, id });
66
+ update();
67
+ return id;
68
+ };
69
+ var clearTimeoutEx = (id) => {
70
+ timeouts = timeouts.filter((timeout) => timeout.id !== id);
71
+ update(timeouts);
43
72
  };
44
73
  // Annotate the CommonJS export names for ESM import in node:
45
74
  0 && (module.exports = {
75
+ clearTimeoutEx,
46
76
  setTimeoutEx
47
77
  });
48
78
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["interface TimeoutInfo {\n delay: number\n func: () => void\n}\n\nlet timeouts: TimeoutInfo[] = []\n\nconst timerFunc = () => {\n if (timeouts.length > 0) {\n setTimeout(timerFunc, 100)\n }\n const firing = timeouts.filter((timeout) => timeout.delay <= 100)\n for (const timeout of firing) {\n timeout.func()\n }\n const notFiring = timeouts.filter((timeout) => timeout.delay > 100)\n timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }))\n}\n\nexport const setTimeoutEx = (func: () => void, delay: number) => {\n if (timeouts.length === 0) {\n setTimeout(timerFunc, 100)\n }\n timeouts.push({ delay, func })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,IAAI,WAA0B,CAAC;AAE/B,IAAM,YAAY,MAAM;AACtB,MAAI,SAAS,SAAS,GAAG;AACvB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,GAAG;AAChE,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACA,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,GAAG;AAClE,aAAW,UAAU,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,KAAK,MAAM,QAAQ,KAAK,EAAE;AAC5F;AAEO,IAAM,eAAe,CAAC,MAAkB,UAAkB;AAC/D,MAAI,SAAS,WAAW,GAAG;AACzB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,WAAS,KAAK,EAAE,OAAO,KAAK,CAAC;AAC/B;","names":[]}
1
+ {"version":3,"sources":["../../src/index.ts","../../src/setTimeoutEx.ts"],"sourcesContent":["export * from './setTimeoutEx'\n","import { assertEx } from '@xylabs/assert'\n\ninterface TimeoutInfo {\n delay: number\n // eslint-disable-next-line @typescript-eslint/ban-types\n func: Function\n id: string\n}\n\nlet timeouts: TimeoutInfo[] = []\nlet currentTimeout: number | undefined\nlet interval = -1\n\nconst reset = () => {\n interval - 1\n clearTimeout(currentTimeout)\n currentTimeout = undefined\n timeouts = []\n}\n\nconst update = (newTimeouts = timeouts, delayPassed = 0) => {\n //if no more timeouts, set back to initial state\n if (newTimeouts.length <= 0) {\n reset()\n } else {\n const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay))\n\n if (newInterval === interval && currentTimeout !== undefined) {\n //since nothing changed, just return\n return\n } else {\n clearTimeout(currentTimeout)\n timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }))\n //restart timeout since it needs to be different\n interval = newInterval\n currentTimeout = setTimeout(timerFunc, interval)\n }\n }\n}\n\nconst timerFunc = () => {\n const notFiring = timeouts.filter((timeout) => timeout.delay > interval)\n const firing = timeouts.filter((timeout) => timeout.delay <= interval)\n\n //call this after getting notFiring and firing since set will change in this call\n update(notFiring, interval)\n\n //trigger the ones that need to be triggered\n for (const timeout of firing) {\n timeout.func()\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport const setTimeoutEx = (func: Function, delay: number) => {\n assertEx(delay >= 0, 'delay must be >= 0')\n const id = `${Date.now()}|${Math.random() * 9_999_999_999}`\n timeouts.push({ delay, func, id })\n update()\n return id\n}\n\nexport const clearTimeoutEx = (id: string) => {\n timeouts = timeouts.filter((timeout) => timeout.id !== id)\n update(timeouts)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,oBAAyB;AASzB,IAAI,WAA0B,CAAC;AAC/B,IAAI;AACJ,IAAI,WAAW;AAEf,IAAM,QAAQ,MAAM;AAClB,aAAW;AACX,eAAa,cAAc;AAC3B,mBAAiB;AACjB,aAAW,CAAC;AACd;AAEA,IAAM,SAAS,CAAC,cAAc,UAAU,cAAc,MAAM;AAE1D,MAAI,YAAY,UAAU,GAAG;AAC3B,UAAM;AAAA,EACR,OAAO;AACL,UAAM,cAAc,KAAK,IAAI,GAAG,YAAY,IAAI,CAAC,YAAY,QAAQ,KAAK,CAAC;AAE3E,QAAI,gBAAgB,YAAY,mBAAmB,QAAW;AAE5D;AAAA,IACF,OAAO;AACL,mBAAa,cAAc;AAC3B,iBAAW,YAAY,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,aAAa,MAAM,QAAQ,MAAM,IAAI,QAAQ,GAAG,EAAE;AAEpH,iBAAW;AACX,uBAAiB,WAAW,WAAW,QAAQ;AAAA,IACjD;AAAA,EACF;AACF;AAEA,IAAM,YAAY,MAAM;AACtB,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,QAAQ;AACvE,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,QAAQ;AAGrE,SAAO,WAAW,QAAQ;AAG1B,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACF;AAGO,IAAM,eAAe,CAAC,MAAgB,UAAkB;AAC7D,8BAAS,SAAS,GAAG,oBAAoB;AACzC,QAAM,KAAK,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,UAAa;AACzD,WAAS,KAAK,EAAE,OAAO,MAAM,GAAG,CAAC;AACjC,SAAO;AACP,SAAO;AACT;AAEO,IAAM,iBAAiB,CAAC,OAAe;AAC5C,aAAW,SAAS,OAAO,CAAC,YAAY,QAAQ,OAAO,EAAE;AACzD,SAAO,QAAQ;AACjB;","names":[]}
@@ -1,2 +1,2 @@
1
- export declare const setTimeoutEx: (func: () => void, delay: number) => void;
1
+ export * from './setTimeoutEx';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,YAAY,SAAU,MAAM,IAAI,SAAS,MAAM,SAK3D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -1,2 +1,2 @@
1
- export declare const setTimeoutEx: (func: () => void, delay: number) => void;
1
+ export * from './setTimeoutEx';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,YAAY,SAAU,MAAM,IAAI,SAAS,MAAM,SAK3D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -1,2 +1,2 @@
1
- export declare const setTimeoutEx: (func: () => void, delay: number) => void;
1
+ export * from './setTimeoutEx';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,YAAY,SAAU,MAAM,IAAI,SAAS,MAAM,SAK3D,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
@@ -1,23 +1,50 @@
1
- // src/index.ts
1
+ // src/setTimeoutEx.ts
2
+ import { assertEx } from "@xylabs/assert";
2
3
  var timeouts = [];
3
- var timerFunc = () => {
4
- if (timeouts.length > 0) {
5
- setTimeout(timerFunc, 100);
4
+ var currentTimeout;
5
+ var interval = -1;
6
+ var reset = () => {
7
+ interval - 1;
8
+ clearTimeout(currentTimeout);
9
+ currentTimeout = void 0;
10
+ timeouts = [];
11
+ };
12
+ var update = (newTimeouts = timeouts, delayPassed = 0) => {
13
+ if (newTimeouts.length <= 0) {
14
+ reset();
15
+ } else {
16
+ const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay));
17
+ if (newInterval === interval && currentTimeout !== void 0) {
18
+ return;
19
+ } else {
20
+ clearTimeout(currentTimeout);
21
+ timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }));
22
+ interval = newInterval;
23
+ currentTimeout = setTimeout(timerFunc, interval);
24
+ }
6
25
  }
7
- const firing = timeouts.filter((timeout) => timeout.delay <= 100);
26
+ };
27
+ var timerFunc = () => {
28
+ const notFiring = timeouts.filter((timeout) => timeout.delay > interval);
29
+ const firing = timeouts.filter((timeout) => timeout.delay <= interval);
30
+ update(notFiring, interval);
8
31
  for (const timeout of firing) {
9
32
  timeout.func();
10
33
  }
11
- const notFiring = timeouts.filter((timeout) => timeout.delay > 100);
12
- timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }));
13
34
  };
14
35
  var setTimeoutEx = (func, delay) => {
15
- if (timeouts.length === 0) {
16
- setTimeout(timerFunc, 100);
17
- }
18
- timeouts.push({ delay, func });
36
+ assertEx(delay >= 0, "delay must be >= 0");
37
+ const id = `${Date.now()}|${Math.random() * 9999999999}`;
38
+ timeouts.push({ delay, func, id });
39
+ update();
40
+ return id;
41
+ };
42
+ var clearTimeoutEx = (id) => {
43
+ timeouts = timeouts.filter((timeout) => timeout.id !== id);
44
+ update(timeouts);
19
45
  };
20
46
  export {
47
+ clearTimeoutEx,
21
48
  setTimeoutEx
22
49
  };
23
50
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["interface TimeoutInfo {\n delay: number\n func: () => void\n}\n\nlet timeouts: TimeoutInfo[] = []\n\nconst timerFunc = () => {\n if (timeouts.length > 0) {\n setTimeout(timerFunc, 100)\n }\n const firing = timeouts.filter((timeout) => timeout.delay <= 100)\n for (const timeout of firing) {\n timeout.func()\n }\n const notFiring = timeouts.filter((timeout) => timeout.delay > 100)\n timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }))\n}\n\nexport const setTimeoutEx = (func: () => void, delay: number) => {\n if (timeouts.length === 0) {\n setTimeout(timerFunc, 100)\n }\n timeouts.push({ delay, func })\n}\n"],"mappings":";AAKA,IAAI,WAA0B,CAAC;AAE/B,IAAM,YAAY,MAAM;AACtB,MAAI,SAAS,SAAS,GAAG;AACvB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,GAAG;AAChE,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACA,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,GAAG;AAClE,aAAW,UAAU,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,KAAK,MAAM,QAAQ,KAAK,EAAE;AAC5F;AAEO,IAAM,eAAe,CAAC,MAAkB,UAAkB;AAC/D,MAAI,SAAS,WAAW,GAAG;AACzB,eAAW,WAAW,GAAG;AAAA,EAC3B;AACA,WAAS,KAAK,EAAE,OAAO,KAAK,CAAC;AAC/B;","names":[]}
1
+ {"version":3,"sources":["../../src/setTimeoutEx.ts"],"sourcesContent":["import { assertEx } from '@xylabs/assert'\n\ninterface TimeoutInfo {\n delay: number\n // eslint-disable-next-line @typescript-eslint/ban-types\n func: Function\n id: string\n}\n\nlet timeouts: TimeoutInfo[] = []\nlet currentTimeout: number | undefined\nlet interval = -1\n\nconst reset = () => {\n interval - 1\n clearTimeout(currentTimeout)\n currentTimeout = undefined\n timeouts = []\n}\n\nconst update = (newTimeouts = timeouts, delayPassed = 0) => {\n //if no more timeouts, set back to initial state\n if (newTimeouts.length <= 0) {\n reset()\n } else {\n const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay))\n\n if (newInterval === interval && currentTimeout !== undefined) {\n //since nothing changed, just return\n return\n } else {\n clearTimeout(currentTimeout)\n timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }))\n //restart timeout since it needs to be different\n interval = newInterval\n currentTimeout = setTimeout(timerFunc, interval)\n }\n }\n}\n\nconst timerFunc = () => {\n const notFiring = timeouts.filter((timeout) => timeout.delay > interval)\n const firing = timeouts.filter((timeout) => timeout.delay <= interval)\n\n //call this after getting notFiring and firing since set will change in this call\n update(notFiring, interval)\n\n //trigger the ones that need to be triggered\n for (const timeout of firing) {\n timeout.func()\n }\n}\n\n// eslint-disable-next-line @typescript-eslint/ban-types\nexport const setTimeoutEx = (func: Function, delay: number) => {\n assertEx(delay >= 0, 'delay must be >= 0')\n const id = `${Date.now()}|${Math.random() * 9_999_999_999}`\n timeouts.push({ delay, func, id })\n update()\n return id\n}\n\nexport const clearTimeoutEx = (id: string) => {\n timeouts = timeouts.filter((timeout) => timeout.id !== id)\n update(timeouts)\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AASzB,IAAI,WAA0B,CAAC;AAC/B,IAAI;AACJ,IAAI,WAAW;AAEf,IAAM,QAAQ,MAAM;AAClB,aAAW;AACX,eAAa,cAAc;AAC3B,mBAAiB;AACjB,aAAW,CAAC;AACd;AAEA,IAAM,SAAS,CAAC,cAAc,UAAU,cAAc,MAAM;AAE1D,MAAI,YAAY,UAAU,GAAG;AAC3B,UAAM;AAAA,EACR,OAAO;AACL,UAAM,cAAc,KAAK,IAAI,GAAG,YAAY,IAAI,CAAC,YAAY,QAAQ,KAAK,CAAC;AAE3E,QAAI,gBAAgB,YAAY,mBAAmB,QAAW;AAE5D;AAAA,IACF,OAAO;AACL,mBAAa,cAAc;AAC3B,iBAAW,YAAY,IAAI,CAAC,aAAa,EAAE,OAAO,QAAQ,QAAQ,aAAa,MAAM,QAAQ,MAAM,IAAI,QAAQ,GAAG,EAAE;AAEpH,iBAAW;AACX,uBAAiB,WAAW,WAAW,QAAQ;AAAA,IACjD;AAAA,EACF;AACF;AAEA,IAAM,YAAY,MAAM;AACtB,QAAM,YAAY,SAAS,OAAO,CAAC,YAAY,QAAQ,QAAQ,QAAQ;AACvE,QAAM,SAAS,SAAS,OAAO,CAAC,YAAY,QAAQ,SAAS,QAAQ;AAGrE,SAAO,WAAW,QAAQ;AAG1B,aAAW,WAAW,QAAQ;AAC5B,YAAQ,KAAK;AAAA,EACf;AACF;AAGO,IAAM,eAAe,CAAC,MAAgB,UAAkB;AAC7D,WAAS,SAAS,GAAG,oBAAoB;AACzC,QAAM,KAAK,GAAG,KAAK,IAAI,CAAC,IAAI,KAAK,OAAO,IAAI,UAAa;AACzD,WAAS,KAAK,EAAE,OAAO,MAAM,GAAG,CAAC;AACjC,SAAO;AACP,SAAO;AACT;AAEO,IAAM,iBAAiB,CAAC,OAAe;AAC5C,aAAW,SAAS,OAAO,CAAC,YAAY,QAAQ,OAAO,EAAE;AACzD,SAAO,QAAQ;AACjB;","names":[]}
@@ -0,0 +1,3 @@
1
+ export declare const setTimeoutEx: (func: Function, delay: number) => string;
2
+ export declare const clearTimeoutEx: (id: string) => void;
3
+ //# sourceMappingURL=setTimeoutEx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,YAAY,SAAU,QAAQ,SAAS,MAAM,WAMzD,CAAA;AAED,eAAO,MAAM,cAAc,OAAQ,MAAM,SAGxC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export declare const setTimeoutEx: (func: Function, delay: number) => string;
2
+ export declare const clearTimeoutEx: (id: string) => void;
3
+ //# sourceMappingURL=setTimeoutEx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,YAAY,SAAU,QAAQ,SAAS,MAAM,WAMzD,CAAA;AAED,eAAO,MAAM,cAAc,OAAQ,MAAM,SAGxC,CAAA"}
@@ -0,0 +1,3 @@
1
+ export declare const setTimeoutEx: (func: Function, delay: number) => string;
2
+ export declare const clearTimeoutEx: (id: string) => void;
3
+ //# sourceMappingURL=setTimeoutEx.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setTimeoutEx.d.ts","sourceRoot":"","sources":["../../src/setTimeoutEx.ts"],"names":[],"mappings":"AAsDA,eAAO,MAAM,YAAY,SAAU,QAAQ,SAAS,MAAM,WAMzD,CAAA;AAED,eAAO,MAAM,cAAc,OAAQ,MAAM,SAGxC,CAAA"}
package/package.json CHANGED
@@ -27,9 +27,12 @@
27
27
  "main": "./dist/node/index.cjs",
28
28
  "types": "./dist/node/index.d.ts",
29
29
  "module": "./dist/node/index.js",
30
+ "dependencies": {
31
+ "@xylabs/assert": "~2.13.26"
32
+ },
30
33
  "devDependencies": {
31
- "@xylabs/ts-scripts-yarn3": "^3.2.41",
32
- "@xylabs/tsconfig": "^3.2.41",
34
+ "@xylabs/ts-scripts-yarn3": "^3.2.42",
35
+ "@xylabs/tsconfig": "^3.2.42",
33
36
  "typescript": "^5.3.3"
34
37
  },
35
38
  "engines": {
@@ -52,6 +55,6 @@
52
55
  "url": "https://github.com/xylabs/sdk-js.git"
53
56
  },
54
57
  "sideEffects": false,
55
- "version": "2.13.24",
58
+ "version": "2.13.26",
56
59
  "type": "module"
57
60
  }
package/src/index.ts CHANGED
@@ -1,25 +1 @@
1
- interface TimeoutInfo {
2
- delay: number
3
- func: () => void
4
- }
5
-
6
- let timeouts: TimeoutInfo[] = []
7
-
8
- const timerFunc = () => {
9
- if (timeouts.length > 0) {
10
- setTimeout(timerFunc, 100)
11
- }
12
- const firing = timeouts.filter((timeout) => timeout.delay <= 100)
13
- for (const timeout of firing) {
14
- timeout.func()
15
- }
16
- const notFiring = timeouts.filter((timeout) => timeout.delay > 100)
17
- timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func }))
18
- }
19
-
20
- export const setTimeoutEx = (func: () => void, delay: number) => {
21
- if (timeouts.length === 0) {
22
- setTimeout(timerFunc, 100)
23
- }
24
- timeouts.push({ delay, func })
25
- }
1
+ export * from './setTimeoutEx'
@@ -0,0 +1,66 @@
1
+ import { assertEx } from '@xylabs/assert'
2
+
3
+ interface TimeoutInfo {
4
+ delay: number
5
+ // eslint-disable-next-line @typescript-eslint/ban-types
6
+ func: Function
7
+ id: string
8
+ }
9
+
10
+ let timeouts: TimeoutInfo[] = []
11
+ let currentTimeout: number | undefined
12
+ let interval = -1
13
+
14
+ const reset = () => {
15
+ interval - 1
16
+ clearTimeout(currentTimeout)
17
+ currentTimeout = undefined
18
+ timeouts = []
19
+ }
20
+
21
+ const update = (newTimeouts = timeouts, delayPassed = 0) => {
22
+ //if no more timeouts, set back to initial state
23
+ if (newTimeouts.length <= 0) {
24
+ reset()
25
+ } else {
26
+ const newInterval = Math.min(...newTimeouts.map((timeout) => timeout.delay))
27
+
28
+ if (newInterval === interval && currentTimeout !== undefined) {
29
+ //since nothing changed, just return
30
+ return
31
+ } else {
32
+ clearTimeout(currentTimeout)
33
+ timeouts = newTimeouts.map((timeout) => ({ delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id }))
34
+ //restart timeout since it needs to be different
35
+ interval = newInterval
36
+ currentTimeout = setTimeout(timerFunc, interval)
37
+ }
38
+ }
39
+ }
40
+
41
+ const timerFunc = () => {
42
+ const notFiring = timeouts.filter((timeout) => timeout.delay > interval)
43
+ const firing = timeouts.filter((timeout) => timeout.delay <= interval)
44
+
45
+ //call this after getting notFiring and firing since set will change in this call
46
+ update(notFiring, interval)
47
+
48
+ //trigger the ones that need to be triggered
49
+ for (const timeout of firing) {
50
+ timeout.func()
51
+ }
52
+ }
53
+
54
+ // eslint-disable-next-line @typescript-eslint/ban-types
55
+ export const setTimeoutEx = (func: Function, delay: number) => {
56
+ assertEx(delay >= 0, 'delay must be >= 0')
57
+ const id = `${Date.now()}|${Math.random() * 9_999_999_999}`
58
+ timeouts.push({ delay, func, id })
59
+ update()
60
+ return id
61
+ }
62
+
63
+ export const clearTimeoutEx = (id: string) => {
64
+ timeouts = timeouts.filter((timeout) => timeout.id !== id)
65
+ update(timeouts)
66
+ }