-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timers: allow timers to be used as primitives
This allows timers to be matched to numeric Ids and therefore used as keys of an Object, passed and stored without storing the Timer instance. clearTimeout/clearInterval is modified to support numeric/string Ids. Co-authored-by: Bradley Farias <bradley.meck@gmail.com> Co-authored-by: Anatoli Papirovski <apapirovski@mac.com> Refs: #21152 Backport-PR-URL: #34482 PR-URL: #34017 Backport-PR-URL: #34482 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Signed-off-by: Denys Otrishko <shishugi@gmail.com>
- Loading branch information
1 parent
14d4bfa
commit 987e0cb
Showing
4 changed files
with
77 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
[ | ||
setTimeout(common.mustNotCall(), 1), | ||
setInterval(common.mustNotCall(), 1), | ||
].forEach((timeout) => { | ||
assert.strictEqual(Number.isNaN(+timeout), false); | ||
assert.strictEqual(+timeout, timeout[Symbol.toPrimitive]()); | ||
assert.strictEqual(`${timeout}`, timeout[Symbol.toPrimitive]().toString()); | ||
assert.deepStrictEqual(Object.keys({ [timeout]: timeout }), [`${timeout}`]); | ||
clearTimeout(+timeout); | ||
}); | ||
|
||
{ | ||
// Check that clearTimeout works with number id. | ||
const timeout = setTimeout(common.mustNotCall(), 1); | ||
const id = +timeout; | ||
clearTimeout(id); | ||
} | ||
|
||
{ | ||
// Check that clearTimeout works with string id. | ||
const timeout = setTimeout(common.mustNotCall(), 1); | ||
const id = `${timeout}`; | ||
clearTimeout(id); | ||
} |