Skip to content
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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,30 @@ function rearm(timer) {
}
}

function extractTimer(timer) {
if (Timeout.prototype.isPrototypeOf(timer) || typeof timer !== 'object' ||
timer === null || timer === undefined) {
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a case that need considering:

node/lib/net.js

Lines 381 to 396 in 472a665

Socket.prototype.setTimeout = function(msecs, callback) {
if (msecs === 0) {
timers.unenroll(this);
if (callback) {
this.removeListener('timeout', callback);
}
} else {
timers.enroll(this, msecs);
timers._unrefActive(this);
if (callback) {
this.once('timeout', callback);
}
}
return this;
};

(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) {
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-timers-timer-argument-convert.js
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 {}; }
});