-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
timers: Implement conversion of timer argument #14817
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -500,8 +500,30 @@ function rearm(timer) { | |||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
function extractTimer(timer) { | ||||||||||||||||||||||||||||||||||
if (Timeout.prototype.isPrototypeOf(timer) || typeof timer !== 'object' || | ||||||||||||||||||||||||||||||||||
timer === null || timer === undefined) { | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a case that need considering: Lines 381 to 396 in 472a665
(this is something we want to refactor in the future Ref: #14209) |
||||||||||||||||||||||||||||||||||
return timer; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if (typeof timer[Symbol.toPrimitive] === 'function') { | ||||||||||||||||||||||||||||||||||
return timer[Symbol.toPrimitive]('default'); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if (timer.valueOf && Timeout.prototype.isPrototypeOf(timer.valueOf())) { | ||||||||||||||||||||||||||||||||||
return timer.valueOf(); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
if (timer.toString && Timeout.prototype.isPrototypeOf(timer.toString())) { | ||||||||||||||||||||||||||||||||||
return timer.toString(); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
return timer; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
const clearTimeout = exports.clearTimeout = function(timer) { | ||||||||||||||||||||||||||||||||||
timer = extractTimer(timer); | ||||||||||||||||||||||||||||||||||
if (timer && (timer[kOnTimeout] || timer._onTimeout)) { | ||||||||||||||||||||||||||||||||||
timer[kOnTimeout] = timer._onTimeout = null; | ||||||||||||||||||||||||||||||||||
if (timer instanceof Timeout) { | ||||||||||||||||||||||||||||||||||
|
@@ -550,6 +572,7 @@ function createRepeatTimeout(callback, repeat, args) { | |||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
exports.clearInterval = function(timer) { | ||||||||||||||||||||||||||||||||||
timer = extractTimer(timer); | ||||||||||||||||||||||||||||||||||
if (timer && timer._repeat) { | ||||||||||||||||||||||||||||||||||
timer._repeat = null; | ||||||||||||||||||||||||||||||||||
clearTimeout(timer); | ||||||||||||||||||||||||||||||||||
|
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,27 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
|
||
const cb = common.mustNotCall(() => { }); | ||
|
||
const timer1 = setTimeout(cb, 1000); | ||
clearTimeout({ | ||
valueOf() { return timer1; } | ||
}); | ||
|
||
const timer2 = setTimeout(cb, 1000); | ||
clearTimeout({ | ||
toString() { return timer2; } | ||
}); | ||
|
||
const timer3 = setTimeout(cb, 1000); | ||
clearTimeout({ | ||
[Symbol.toPrimitive](hint) { | ||
return timer3; | ||
} | ||
}); | ||
|
||
const timer4 = setTimeout(cb, 1000); | ||
clearTimeout({ | ||
valueOf() { return timer4; }, | ||
toString() { return {}; } | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could merge the last two as
timer == null
. It's a lite idiom used quite often in "core" to mean "something nully".Also BTW
typeof undefined !==
object`, so the second clause covers it.