@xylabs/timer 5.0.80 → 5.0.82
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/package.json +6 -9
- package/src/index.ts +0 -1
- package/src/setTimeoutEx.ts +0 -70
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@xylabs/timer",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.82",
|
|
4
4
|
"description": "Base functionality used throughout XY Labs TypeScript/JavaScript libraries",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"xylabs",
|
|
@@ -30,29 +30,26 @@
|
|
|
30
30
|
"exports": {
|
|
31
31
|
".": {
|
|
32
32
|
"types": "./dist/neutral/index.d.ts",
|
|
33
|
-
"source": "./src/index.ts",
|
|
34
33
|
"default": "./dist/neutral/index.mjs"
|
|
35
34
|
},
|
|
36
35
|
"./package.json": "./package.json"
|
|
37
36
|
},
|
|
38
37
|
"module": "./dist/neutral/index.mjs",
|
|
39
|
-
"source": "./src/index.ts",
|
|
40
38
|
"types": "./dist/neutral/index.d.ts",
|
|
41
39
|
"files": [
|
|
42
40
|
"dist",
|
|
43
|
-
"src",
|
|
44
41
|
"!**/*.bench.*",
|
|
45
42
|
"!**/*.spec.*",
|
|
46
43
|
"!**/*.test.*"
|
|
47
44
|
],
|
|
48
45
|
"dependencies": {
|
|
49
|
-
"@xylabs/assert": "~5.0.
|
|
46
|
+
"@xylabs/assert": "~5.0.82"
|
|
50
47
|
},
|
|
51
48
|
"devDependencies": {
|
|
52
|
-
"@
|
|
53
|
-
"@xylabs/
|
|
54
|
-
"
|
|
55
|
-
"
|
|
49
|
+
"@xylabs/ts-scripts-yarn3": "~7.4.11",
|
|
50
|
+
"@xylabs/tsconfig": "~7.4.11",
|
|
51
|
+
"typescript": "~5.9.3",
|
|
52
|
+
"vitest": "^4.0.18"
|
|
56
53
|
},
|
|
57
54
|
"engines": {
|
|
58
55
|
"node": ">=18"
|
package/src/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './setTimeoutEx.ts'
|
package/src/setTimeoutEx.ts
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { assertEx } from '@xylabs/assert'
|
|
2
|
-
|
|
3
|
-
interface TimeoutInfo {
|
|
4
|
-
delay: number
|
|
5
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
6
|
-
func: Function
|
|
7
|
-
id: string
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
let timeouts: TimeoutInfo[] = []
|
|
11
|
-
let currentTimeout: NodeJS.Timeout | 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 => ({
|
|
34
|
-
delay: timeout.delay - delayPassed, func: timeout.func, id: timeout.id,
|
|
35
|
-
}))
|
|
36
|
-
// restart timeout since it needs to be different
|
|
37
|
-
interval = newInterval
|
|
38
|
-
currentTimeout = setTimeout(timerFunc, interval)
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const timerFunc = () => {
|
|
44
|
-
const notFiring = timeouts.filter(timeout => timeout.delay > interval)
|
|
45
|
-
const firing = timeouts.filter(timeout => timeout.delay <= interval)
|
|
46
|
-
|
|
47
|
-
// call this after getting notFiring and firing since set will change in this call
|
|
48
|
-
update(notFiring, interval)
|
|
49
|
-
|
|
50
|
-
// trigger the ones that need to be triggered
|
|
51
|
-
for (const timeout of firing) {
|
|
52
|
-
timeout.func()
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
57
|
-
export const setTimeoutEx = (func: Function, delay: number) => {
|
|
58
|
-
assertEx(delay >= 0, () => 'delay must be >= 0')
|
|
59
|
-
const id = `${Date.now()}|${Math.random() * 9_999_999_999}`
|
|
60
|
-
timeouts.push({
|
|
61
|
-
delay, func, id,
|
|
62
|
-
})
|
|
63
|
-
update()
|
|
64
|
-
return id
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const clearTimeoutEx = (id: string) => {
|
|
68
|
-
timeouts = timeouts.filter(timeout => timeout.id !== id)
|
|
69
|
-
update(timeouts)
|
|
70
|
-
}
|