-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PR-URL: #46502 Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de>
- Loading branch information
1 parent
db93ee4
commit c26a34c
Showing
8 changed files
with
202 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict' | ||
|
||
let fastNow = Date.now() | ||
let fastNowTimeout | ||
|
||
const fastTimers = [] | ||
|
||
function onTimeout () { | ||
fastNow = Date.now() | ||
|
||
let len = fastTimers.length | ||
let idx = 0 | ||
while (idx < len) { | ||
const timer = fastTimers[idx] | ||
|
||
if (timer.expires && fastNow >= timer.expires) { | ||
timer.expires = 0 | ||
timer.callback(timer.opaque) | ||
} | ||
|
||
if (timer.expires === 0) { | ||
timer.active = false | ||
if (idx !== len - 1) { | ||
fastTimers[idx] = fastTimers.pop() | ||
} else { | ||
fastTimers.pop() | ||
} | ||
len -= 1 | ||
} else { | ||
idx += 1 | ||
} | ||
} | ||
|
||
if (fastTimers.length > 0) { | ||
refreshTimeout() | ||
} | ||
} | ||
|
||
function refreshTimeout () { | ||
if (fastNowTimeout && fastNowTimeout.refresh) { | ||
fastNowTimeout.refresh() | ||
} else { | ||
clearTimeout(fastNowTimeout) | ||
fastNowTimeout = setTimeout(onTimeout, 1e3) | ||
if (fastNowTimeout.unref) { | ||
fastNowTimeout.unref() | ||
} | ||
} | ||
} | ||
|
||
class Timeout { | ||
constructor (callback, delay, opaque) { | ||
this.callback = callback | ||
this.delay = delay | ||
this.opaque = opaque | ||
this.expires = 0 | ||
this.active = false | ||
|
||
this.refresh() | ||
} | ||
|
||
refresh () { | ||
if (!this.active) { | ||
this.active = true | ||
fastTimers.push(this) | ||
if (!fastNowTimeout || fastTimers.length === 1) { | ||
refreshTimeout() | ||
fastNow = Date.now() | ||
} | ||
} | ||
|
||
this.expires = fastNow + this.delay | ||
} | ||
|
||
clear () { | ||
this.expires = 0 | ||
} | ||
} | ||
|
||
module.exports = { | ||
setTimeout (callback, delay, opaque) { | ||
return new Timeout(callback, delay, opaque) | ||
}, | ||
clearTimeout (timeout) { | ||
if (timeout && timeout.clear) { | ||
timeout.clear() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters