@nxtedition/timers 1.1.5 → 1.1.6
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/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +98 -114
- package/lib/index.js.map +1 -0
- package/package.json +3 -4
package/lib/index.d.ts
CHANGED
|
@@ -10,3 +10,4 @@ declare class Timeout {
|
|
|
10
10
|
export declare function setTimeout<T>(callback: (opaque: T) => void, delay: number, opaque: T): Timeout | NodeJS.Timeout;
|
|
11
11
|
export declare function clearTimeout(timeout: Timeout | NodeJS.Timeout | null | undefined): void;
|
|
12
12
|
export {};
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyEA,cAAM,OAAO;IACX,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,CAAA;IACnC,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,EAAE,OAAO,CAAA;IAKf,KAAK,EAAE,MAAM,CAAK;gBAEN,QAAQ,EAAE,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO;IAO/E,OAAO,IAAI,IAAI;IAef,KAAK,IAAI,IAAI;CAKd;AAED,wBAAgB,UAAU,CAAC,CAAC,EAC1B,QAAQ,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,EAC7B,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,CAAC,GACR,OAAO,GAAG,MAAM,CAAC,OAAO,CAI1B;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,MAAM,CAAC,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,IAAI,CAMvF"}
|
package/lib/index.js
CHANGED
|
@@ -1,129 +1,113 @@
|
|
|
1
|
-
import { doYield, shouldYield } from '@nxtedition/yield'
|
|
2
|
-
|
|
3
|
-
let fastNow = 0
|
|
1
|
+
import { doYield, shouldYield } from '@nxtedition/yield';
|
|
2
|
+
let fastNow = 0;
|
|
4
3
|
// eslint-disable-next-line typescript-eslint/no-redundant-type-constituents
|
|
5
|
-
let fastNowTimeout
|
|
6
|
-
let fastIndex = 0
|
|
7
|
-
|
|
8
|
-
const fastTimers = []
|
|
9
|
-
|
|
4
|
+
let fastNowTimeout = null;
|
|
5
|
+
let fastIndex = 0;
|
|
6
|
+
const fastTimers = [];
|
|
10
7
|
function dispatch() {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
8
|
+
let len = fastTimers.length;
|
|
9
|
+
while (fastIndex < len) {
|
|
10
|
+
const timer = fastTimers[fastIndex];
|
|
11
|
+
if (timer.state > 0 && fastNow >= timer.state) {
|
|
12
|
+
timer.state = -1;
|
|
13
|
+
try {
|
|
14
|
+
timer.callback(timer.opaque);
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
// Isolate the throw: rethrow asynchronously so dispatch continues.
|
|
18
|
+
// Otherwise fastIndex stays mid-loop and every prior timer gets
|
|
19
|
+
// delayed by a full tick on the next onTimeout().
|
|
20
|
+
// Materialize the stack now — queueMicrotask severs the sync frame.
|
|
21
|
+
if (err instanceof Error) {
|
|
22
|
+
void err.stack;
|
|
23
|
+
}
|
|
24
|
+
queueMicrotask(() => {
|
|
25
|
+
throw err;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
if (timer.state === -1) {
|
|
30
|
+
timer.state = -2;
|
|
31
|
+
const tmp = fastTimers.pop();
|
|
32
|
+
if (tmp !== timer) {
|
|
33
|
+
fastTimers[fastIndex] = tmp;
|
|
34
|
+
}
|
|
35
|
+
len -= 1;
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
fastIndex += 1;
|
|
39
|
+
}
|
|
40
|
+
if ((fastIndex & 0xff) === 0 && shouldYield()) {
|
|
41
|
+
doYield(dispatch);
|
|
42
|
+
return;
|
|
26
43
|
}
|
|
27
|
-
queueMicrotask(() => {
|
|
28
|
-
throw err
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
44
|
}
|
|
32
|
-
|
|
33
|
-
if (
|
|
34
|
-
|
|
35
|
-
const tmp = fastTimers.pop()
|
|
36
|
-
if (tmp !== timer) {
|
|
37
|
-
fastTimers[fastIndex] = tmp
|
|
38
|
-
}
|
|
39
|
-
len -= 1
|
|
40
|
-
} else {
|
|
41
|
-
fastIndex += 1
|
|
45
|
+
fastIndex = 0;
|
|
46
|
+
if (fastTimers.length > 0) {
|
|
47
|
+
refreshTimeout();
|
|
42
48
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
return
|
|
49
|
+
else if (fastNowTimeout) {
|
|
50
|
+
globalThis.clearTimeout(fastNowTimeout);
|
|
51
|
+
fastNowTimeout = null;
|
|
47
52
|
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
fastIndex = 0
|
|
51
|
-
|
|
52
|
-
if (fastTimers.length > 0) {
|
|
53
|
-
refreshTimeout()
|
|
54
|
-
} else if (fastNowTimeout) {
|
|
55
|
-
globalThis.clearTimeout(fastNowTimeout)
|
|
56
|
-
fastNowTimeout = null
|
|
57
|
-
}
|
|
58
53
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
dispatch()
|
|
54
|
+
function onTimeout() {
|
|
55
|
+
fastNow += 500;
|
|
56
|
+
dispatch();
|
|
63
57
|
}
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
58
|
+
function refreshTimeout() {
|
|
59
|
+
if (fastNowTimeout) {
|
|
60
|
+
fastNowTimeout.refresh();
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
fastNowTimeout = globalThis.setTimeout(onTimeout, 500);
|
|
64
|
+
fastNowTimeout.unref();
|
|
65
|
+
}
|
|
72
66
|
}
|
|
73
|
-
|
|
74
67
|
class Timeout {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
this.opaque = opaque
|
|
88
|
-
this.refresh()
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
refresh() {
|
|
92
|
-
if (this.state === -2) {
|
|
93
|
-
fastTimers.push(this)
|
|
94
|
-
if (!fastNowTimeout || fastTimers.length === 1) {
|
|
95
|
-
refreshTimeout()
|
|
96
|
-
}
|
|
68
|
+
callback;
|
|
69
|
+
delay;
|
|
70
|
+
opaque;
|
|
71
|
+
// -2 not in timer list
|
|
72
|
+
// -1 in timer list but inactive
|
|
73
|
+
// > 0 in timer list waiting for time to expire
|
|
74
|
+
state = -2;
|
|
75
|
+
constructor(callback, delay, opaque) {
|
|
76
|
+
this.callback = callback;
|
|
77
|
+
this.delay = delay;
|
|
78
|
+
this.opaque = opaque;
|
|
79
|
+
this.refresh();
|
|
97
80
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
81
|
+
refresh() {
|
|
82
|
+
if (this.state === -2) {
|
|
83
|
+
fastTimers.push(this);
|
|
84
|
+
if (!fastNowTimeout || fastTimers.length === 1) {
|
|
85
|
+
refreshTimeout();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// Offset by half the tick interval to center the timing error.
|
|
89
|
+
// fastNow only advances every 500ms so the actual fire time can
|
|
90
|
+
// deviate from the requested delay. Without this offset the error
|
|
91
|
+
// is up to -500ms (early); with it the worst case is ±250ms.
|
|
92
|
+
this.state = fastNow + this.delay + 250;
|
|
93
|
+
}
|
|
94
|
+
clear() {
|
|
95
|
+
if (this.state !== -2) {
|
|
96
|
+
this.state = -1;
|
|
97
|
+
}
|
|
109
98
|
}
|
|
110
|
-
}
|
|
111
99
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
opaque ,
|
|
117
|
-
) {
|
|
118
|
-
return delay < 1e3
|
|
119
|
-
? globalThis.setTimeout(callback, delay, opaque)
|
|
120
|
-
: new Timeout(callback , delay, opaque)
|
|
100
|
+
export function setTimeout(callback, delay, opaque) {
|
|
101
|
+
return delay < 1e3
|
|
102
|
+
? globalThis.setTimeout(callback, delay, opaque)
|
|
103
|
+
: new Timeout(callback, delay, opaque);
|
|
121
104
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
105
|
+
export function clearTimeout(timeout) {
|
|
106
|
+
if (timeout instanceof Timeout) {
|
|
107
|
+
timeout.clear();
|
|
108
|
+
}
|
|
109
|
+
else if (timeout != null) {
|
|
110
|
+
globalThis.clearTimeout(timeout);
|
|
111
|
+
}
|
|
129
112
|
}
|
|
113
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAExD,IAAI,OAAO,GAAG,CAAC,CAAA;AACf,4EAA4E;AAC5E,IAAI,cAAc,GAA0B,IAAI,CAAA;AAChD,IAAI,SAAS,GAAG,CAAC,CAAA;AAEjB,MAAM,UAAU,GAAc,EAAE,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,CAAC,KAAK,GAAG,OAAO,GAAG,IAAI,CAAC,KAAK,GAAG,GAAG,CAAA;IACzC,CAAC;IAED,KAAK;QACH,IAAI,IAAI,CAAC,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;YACtB,IAAI,CAAC,KAAK,GAAG,CAAC,CAAC,CAAA;QACjB,CAAC;IACH,CAAC;CACF;AAED,MAAM,UAAU,UAAU,CACxB,QAA6B,EAC7B,KAAa,EACb,MAAS;IAET,OAAO,KAAK,GAAG,GAAG;QAChB,CAAC,CAAC,UAAU,CAAC,UAAU,CAAC,QAAQ,EAAE,KAAK,EAAE,MAAM,CAAC;QAChD,CAAC,CAAC,IAAI,OAAO,CAAC,QAAqC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAA;AACvE,CAAC;AAED,MAAM,UAAU,YAAY,CAAC,OAAoD;IAC/E,IAAI,OAAO,YAAY,OAAO,EAAE,CAAC;QAC/B,OAAO,CAAC,KAAK,EAAE,CAAA;IACjB,CAAC;SAAM,IAAI,OAAO,IAAI,IAAI,EAAE,CAAC;QAC3B,UAAU,CAAC,YAAY,CAAC,OAAO,CAAC,CAAA;IAClC,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/timers",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
|
-
"build": "rimraf lib && tsc -p tsconfig.build.json
|
|
17
|
+
"build": "rimraf lib && tsc -p tsconfig.build.json",
|
|
18
18
|
"prepublishOnly": "yarn build",
|
|
19
19
|
"typecheck": "tsc --noEmit",
|
|
20
20
|
"test": "node --test",
|
|
@@ -25,14 +25,13 @@
|
|
|
25
25
|
"@sinonjs/fake-timers": "^15.3.2",
|
|
26
26
|
"@types/node": "^25.5.0",
|
|
27
27
|
"@types/sinonjs__fake-timers": "^15.0.1",
|
|
28
|
-
"amaroc": "^1.0.1",
|
|
29
28
|
"mitata": "^1.0.34",
|
|
30
29
|
"oxlint-tsgolint": "^0.17.0",
|
|
31
30
|
"rimraf": "^6.1.3",
|
|
32
31
|
"typescript": "^5.9.3"
|
|
33
32
|
},
|
|
34
33
|
"dependencies": {
|
|
35
|
-
"@nxtedition/yield": "^1.0.
|
|
34
|
+
"@nxtedition/yield": "^1.0.19"
|
|
36
35
|
},
|
|
37
36
|
"gitHead": "7c9c7457c885c644c7a1e70ef894d4727ce240d6"
|
|
38
37
|
}
|