Skip to content

Commit 82bb3da

Browse files
committed
fix: fast timers and event loop lag
1 parent 2f463fc commit 82bb3da

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

lib/timers.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ function onTimeout () {
1313
while (idx < len) {
1414
const timer = fastTimers[idx]
1515

16-
if (timer.expires && fastNow >= timer.expires) {
17-
timer.expires = 0
16+
if (timer.expires === 0) {
17+
timer.expires = fastNow + timer.delay
18+
} else if (timer.expires >= 0 && fastNow >= timer.expires) {
19+
timer.expires = -1
1820
timer.callback(timer.opaque)
1921
}
2022

21-
if (timer.expires === 0) {
22-
timer.active = false
23+
if (timer.expires === -1) {
24+
timer.expires = -2
2325
if (idx !== len - 1) {
2426
fastTimers[idx] = fastTimers.pop()
2527
} else {
@@ -53,27 +55,24 @@ class Timeout {
5355
this.callback = callback
5456
this.delay = delay
5557
this.opaque = opaque
56-
this.expires = 0
57-
this.active = false
58+
this.expires = -2
5859

5960
this.refresh()
6061
}
6162

6263
refresh () {
63-
if (!this.active) {
64-
this.active = true
64+
if (this.expires === -2) {
6565
fastTimers.push(this)
6666
if (!fastNowTimeout || fastTimers.length === 1) {
6767
refreshTimeout()
68-
fastNow = Date.now()
6968
}
7069
}
7170

72-
this.expires = fastNow + this.delay
71+
this.expires = 0
7372
}
7473

7574
clear () {
76-
this.expires = 0
75+
this.expires = -1
7776
}
7877
}
7978

0 commit comments

Comments
 (0)