@xylabs/timer 2.13.25 → 2.13.27
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/dist/browser/index.cjs +32 -9
- package/dist/browser/index.cjs.map +1 -1
- package/dist/browser/index.d.cts +1 -2
- package/dist/browser/index.d.cts.map +1 -1
- package/dist/browser/index.d.mts +1 -2
- package/dist/browser/index.d.mts.map +1 -1
- package/dist/browser/index.d.ts +1 -2
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js +31 -10
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/setTimeoutEx.d.cts +3 -0
- package/dist/browser/setTimeoutEx.d.cts.map +1 -0
- package/dist/browser/setTimeoutEx.d.mts +3 -0
- package/dist/browser/setTimeoutEx.d.mts.map +1 -0
- package/dist/browser/setTimeoutEx.d.ts +3 -0
- package/dist/browser/setTimeoutEx.d.ts.map +1 -0
- package/dist/node/index.cjs +32 -9
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.d.cts +1 -2
- package/dist/node/index.d.cts.map +1 -1
- package/dist/node/index.d.mts +1 -2
- package/dist/node/index.d.mts.map +1 -1
- package/dist/node/index.d.ts +1 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +31 -10
- package/dist/node/index.js.map +1 -1
- package/dist/node/setTimeoutEx.d.cts +3 -0
- package/dist/node/setTimeoutEx.d.cts.map +1 -0
- package/dist/node/setTimeoutEx.d.mts +3 -0
- package/dist/node/setTimeoutEx.d.mts.map +1 -0
- package/dist/node/setTimeoutEx.d.ts +3 -0
- package/dist/node/setTimeoutEx.d.ts.map +1 -0
- package/package.json +6 -3
- package/src/index.ts +1 -32
- package/src/setTimeoutEx.ts +66 -0
package/dist/browser/index.cjs
CHANGED
|
@@ -24,27 +24,50 @@ __export(src_exports, {
|
|
|
24
24
|
setTimeoutEx: () => setTimeoutEx
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/setTimeoutEx.ts
|
|
29
|
+
var import_assert = require("@xylabs/assert");
|
|
27
30
|
var timeouts = [];
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
|
|
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
|
+
}
|
|
31
52
|
}
|
|
32
|
-
|
|
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);
|
|
33
58
|
for (const timeout of firing) {
|
|
34
59
|
timeout.func();
|
|
35
60
|
}
|
|
36
|
-
const notFiring = timeouts.filter((timeout) => timeout.delay > 100);
|
|
37
|
-
timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func, id: timeout.id }));
|
|
38
61
|
};
|
|
39
62
|
var setTimeoutEx = (func, delay) => {
|
|
40
|
-
|
|
41
|
-
setTimeout(timerFunc, 100);
|
|
42
|
-
}
|
|
63
|
+
(0, import_assert.assertEx)(delay >= 0, "delay must be >= 0");
|
|
43
64
|
const id = `${Date.now()}|${Math.random() * 9999999999}`;
|
|
44
65
|
timeouts.push({ delay, func, id });
|
|
66
|
+
update();
|
|
45
67
|
return id;
|
|
46
68
|
};
|
|
47
69
|
var clearTimeoutEx = (id) => {
|
|
48
70
|
timeouts = timeouts.filter((timeout) => timeout.id !== id);
|
|
71
|
+
update(timeouts);
|
|
49
72
|
};
|
|
50
73
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["
|
|
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":[]}
|
package/dist/browser/index.d.cts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
package/dist/browser/index.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
package/dist/browser/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
package/dist/browser/index.js
CHANGED
|
@@ -1,26 +1,47 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/setTimeoutEx.ts
|
|
2
|
+
import { assertEx } from "@xylabs/assert";
|
|
2
3
|
var timeouts = [];
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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, id: timeout.id }));
|
|
13
34
|
};
|
|
14
35
|
var setTimeoutEx = (func, delay) => {
|
|
15
|
-
|
|
16
|
-
setTimeout(timerFunc, 100);
|
|
17
|
-
}
|
|
36
|
+
assertEx(delay >= 0, "delay must be >= 0");
|
|
18
37
|
const id = `${Date.now()}|${Math.random() * 9999999999}`;
|
|
19
38
|
timeouts.push({ delay, func, id });
|
|
39
|
+
update();
|
|
20
40
|
return id;
|
|
21
41
|
};
|
|
22
42
|
var clearTimeoutEx = (id) => {
|
|
23
43
|
timeouts = timeouts.filter((timeout) => timeout.id !== id);
|
|
44
|
+
update(timeouts);
|
|
24
45
|
};
|
|
25
46
|
export {
|
|
26
47
|
clearTimeoutEx,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/
|
|
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;AAS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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 @@
|
|
|
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 @@
|
|
|
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/dist/node/index.cjs
CHANGED
|
@@ -24,28 +24,51 @@ __export(src_exports, {
|
|
|
24
24
|
setTimeoutEx: () => setTimeoutEx
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/setTimeoutEx.ts
|
|
29
|
+
var import_assert = require("@xylabs/assert");
|
|
27
30
|
var timeouts = [];
|
|
28
|
-
var
|
|
29
|
-
|
|
30
|
-
|
|
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
|
+
}
|
|
31
52
|
}
|
|
32
|
-
|
|
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);
|
|
33
58
|
for (const timeout of firing) {
|
|
34
59
|
timeout.func();
|
|
35
60
|
}
|
|
36
|
-
const notFiring = timeouts.filter((timeout) => timeout.delay > 100);
|
|
37
|
-
timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func, id: timeout.id }));
|
|
38
61
|
};
|
|
39
62
|
var setTimeoutEx = (func, delay) => {
|
|
40
|
-
|
|
41
|
-
setTimeout(timerFunc, 100);
|
|
42
|
-
}
|
|
63
|
+
(0, import_assert.assertEx)(delay >= 0, "delay must be >= 0");
|
|
43
64
|
const id = `${Date.now()}|${Math.random() * 9999999999}`;
|
|
44
65
|
timeouts.push({ delay, func, id });
|
|
66
|
+
update();
|
|
45
67
|
return id;
|
|
46
68
|
};
|
|
47
69
|
var clearTimeoutEx = (id) => {
|
|
48
70
|
timeouts = timeouts.filter((timeout) => timeout.id !== id);
|
|
71
|
+
update(timeouts);
|
|
49
72
|
};
|
|
50
73
|
// Annotate the CommonJS export names for ESM import in node:
|
|
51
74
|
0 && (module.exports = {
|
package/dist/node/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["
|
|
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,WAA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":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
package/dist/node/index.d.mts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
package/dist/node/index.d.ts
CHANGED
package/dist/node/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAA"}
|
package/dist/node/index.js
CHANGED
|
@@ -1,26 +1,47 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/setTimeoutEx.ts
|
|
2
|
+
import { assertEx } from "@xylabs/assert";
|
|
2
3
|
var timeouts = [];
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
|
|
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, id: timeout.id }));
|
|
13
34
|
};
|
|
14
35
|
var setTimeoutEx = (func, delay) => {
|
|
15
|
-
|
|
16
|
-
setTimeout(timerFunc, 100);
|
|
17
|
-
}
|
|
36
|
+
assertEx(delay >= 0, "delay must be >= 0");
|
|
18
37
|
const id = `${Date.now()}|${Math.random() * 9999999999}`;
|
|
19
38
|
timeouts.push({ delay, func, id });
|
|
39
|
+
update();
|
|
20
40
|
return id;
|
|
21
41
|
};
|
|
22
42
|
var clearTimeoutEx = (id) => {
|
|
23
43
|
timeouts = timeouts.filter((timeout) => timeout.id !== id);
|
|
44
|
+
update(timeouts);
|
|
24
45
|
};
|
|
25
46
|
export {
|
|
26
47
|
clearTimeoutEx,
|
package/dist/node/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/
|
|
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 @@
|
|
|
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 @@
|
|
|
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 @@
|
|
|
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.27"
|
|
32
|
+
},
|
|
30
33
|
"devDependencies": {
|
|
31
|
-
"@xylabs/ts-scripts-yarn3": "^3.2.
|
|
32
|
-
"@xylabs/tsconfig": "^3.2.
|
|
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.
|
|
58
|
+
"version": "2.13.27",
|
|
56
59
|
"type": "module"
|
|
57
60
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,32 +1 @@
|
|
|
1
|
-
|
|
2
|
-
delay: number
|
|
3
|
-
func: () => void
|
|
4
|
-
id: string
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
let timeouts: TimeoutInfo[] = []
|
|
8
|
-
|
|
9
|
-
const timerFunc = () => {
|
|
10
|
-
if (timeouts.length > 0) {
|
|
11
|
-
setTimeout(timerFunc, 100)
|
|
12
|
-
}
|
|
13
|
-
const firing = timeouts.filter((timeout) => timeout.delay <= 100)
|
|
14
|
-
for (const timeout of firing) {
|
|
15
|
-
timeout.func()
|
|
16
|
-
}
|
|
17
|
-
const notFiring = timeouts.filter((timeout) => timeout.delay > 100)
|
|
18
|
-
timeouts = notFiring.map((timeout) => ({ delay: timeout.delay - 100, func: timeout.func, id: timeout.id }))
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export const setTimeoutEx = (func: () => void, delay: number) => {
|
|
22
|
-
if (timeouts.length === 0) {
|
|
23
|
-
setTimeout(timerFunc, 100)
|
|
24
|
-
}
|
|
25
|
-
const id = `${Date.now()}|${Math.random() * 9_999_999_999}`
|
|
26
|
-
timeouts.push({ delay, func, id })
|
|
27
|
-
return id
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export const clearTimeoutEx = (id: string) => {
|
|
31
|
-
timeouts = timeouts.filter((timeout) => timeout.id !== id)
|
|
32
|
-
}
|
|
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
|
+
}
|