-
Notifications
You must be signed in to change notification settings - Fork 29.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: enable timers to be used as primitives #19683
Changes from 3 commits
9ac2bd4
6101d52
e32b969
c4ec0e5
d963047
54b8894
d7ae747
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,6 +90,12 @@ of the Node.js application. | |
|
||
Returns a reference to the `Timeout`. | ||
|
||
### timeout[Symbol.toPrimitive]() | ||
|
||
When coercing a `Timeout` to a primitive, a primitive will be generated that can be used with the appropriate function that can be used to clear the `Timeout`. This allows enhanced compatibility with browser `setTimeout`, and `setInterval` implementations. | ||
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. Wrap at 80 cols. 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. fixed |
||
|
||
Returns a `number`. | ||
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. Put * Returns: {integer} right below the heading. 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. fixed |
||
|
||
## Scheduling Timers | ||
|
||
A timer in Node.js is an internal construct that calls a given function after | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,6 +145,8 @@ const kRefed = Symbol('refed'); | |
const refedLists = Object.create(null); | ||
const unrefedLists = Object.create(null); | ||
|
||
const KNOWN_TIMERS = Object.create(null); | ||
|
||
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. Extraneous line. 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. fixed |
||
|
||
// Schedule or re-schedule a timer. | ||
// The item must have been enroll()'d first. | ||
|
@@ -453,6 +455,12 @@ function rearm(timer, start = TimerWrap.now()) { | |
|
||
|
||
const clearTimeout = exports.clearTimeout = function(timer) { | ||
if (typeof timer === 'number' || typeof timer === 'string') { | ||
if (timer in KNOWN_TIMERS) { | ||
clearTimeout(KNOWN_TIMERS[timer]); | ||
} | ||
return; | ||
} | ||
if (timer && timer._onTimeout) { | ||
timer._onTimeout = null; | ||
if (timer instanceof Timeout) { | ||
|
@@ -496,7 +504,13 @@ exports.setInterval = function(callback, repeat, arg1, arg2, arg3) { | |
return timeout; | ||
}; | ||
|
||
exports.clearInterval = function(timer) { | ||
const clearInterval = exports.clearInterval = function(timer) { | ||
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. Why the addition of 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. was added in e32b969 as a bug fix to previous iteration |
||
if (typeof timer === 'number' || typeof timer === 'string') { | ||
if (timer in KNOWN_TIMERS) { | ||
clearInterval(KNOWN_TIMERS[timer]); | ||
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. I think we may want to avoid accessing 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. ah yes, that would be a bug if that happened. I'm not 100% sure on why these functions are not named looking at them... 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. Would 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. fixed |
||
} | ||
return; | ||
} | ||
if (timer && timer._repeat) { | ||
timer._repeat = null; | ||
clearTimeout(timer); | ||
|
@@ -521,6 +535,11 @@ function unrefdHandle(timer, now) { | |
return true; | ||
} | ||
|
||
Timeout.prototype[Symbol.toPrimitive] = function() { | ||
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. Why 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. see https://codepen.io/bradleymeck/pen/eMMpdR?editors=0012 , .valueOf is not safe for using as a key. toPrimitive handles both string and number coercion. |
||
const id = this[async_id_symbol]; | ||
KNOWN_TIMERS[id] = this; | ||
return id; | ||
}; | ||
|
||
Timeout.prototype.unref = function() { | ||
if (this._handle) { | ||
|
@@ -558,6 +577,9 @@ Timeout.prototype.ref = function() { | |
|
||
Timeout.prototype.close = function() { | ||
this._onTimeout = null; | ||
if (typeof this[async_id_symbol] === 'number') { | ||
delete KNOWN_TIMERS[this[async_id_symbol]]; | ||
} | ||
if (this._handle) { | ||
if (destroyHooksExist() && | ||
typeof this[async_id_symbol] === 'number' && | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
|
||
const timeout1 = setTimeout(common.mustNotCall(), 0); | ||
const timeout2 = setInterval(common.mustNotCall(), 0); | ||
|
||
assert.strictEqual(Number.isNaN(+timeout1), false); | ||
assert.strictEqual(Number.isNaN(+timeout2), false); | ||
|
||
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. Can you check that |
||
assert.notStrictEqual(`${timeout1}`, Object.prototype.toString.call(timeout1)); | ||
assert.notStrictEqual(`${timeout2}`, Object.prototype.toString.call(timeout2)); | ||
|
||
assert.notStrictEqual(+timeout1, +timeout2); | ||
|
||
const o = {}; | ||
o[timeout1] = timeout1; | ||
o[timeout2] = timeout2; | ||
const keys = Object.keys(o); | ||
assert.deepStrictEqual(keys, [`${timeout1}`, `${timeout2}`]); | ||
|
||
clearTimeout(keys[0]); | ||
clearInterval(keys[1]); |
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.
Nit: Please wrap at 80 characters :)
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.
fixed