-
Notifications
You must be signed in to change notification settings - Fork 597
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: refreshTimeout * perf: fast timers * fixup * Revert "fixup" This reverts commit 2fc8eaa7195c931481d902a3f2583f38317td0be8. * fixup * fixup * fixup * fixup * fixup * fixup * fixup * fixup * fixup * Update lib/timers.js * Update lib/timers.js * Update lib/timers.js
- Loading branch information
Showing
8 changed files
with
244 additions
and
4 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
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
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
Oops, something went wrong.