Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
timers: enable timers to be used as primitives
For web compatibility this allows timers to be stored as the key
of an Object property and be passed back to corresponding method to
clear the timer.
  • Loading branch information
bmeck committed Mar 29, 2018
commit 9ac2bd41f6d13020d258c861197991b828bd5fe9
21 changes: 21 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ const kRefed = Symbol('refed');
const refedLists = Object.create(null);
const unrefedLists = Object.create(null);

const KNOWN_TIMERS = Object.create(null);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Extraneous line.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.
const active = exports.active = function(item) {
KNOWN_TIMERS[item[async_id_symbol]] = item;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we do the caching in [@@toPrimitive]()? I doubt this is a very frequently used feature, so it might be good if we could avoid the storage overhead for the common case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

seems fine to change that.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

not sure how we could easily avoid the delete though when clearing without doing some funny stuff. the cache should swap into dictionary mode really fast though.

insert(item, false);
};

Expand Down Expand Up @@ -453,6 +456,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) {
Expand Down Expand Up @@ -497,6 +506,12 @@ exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
};

exports.clearInterval = function(timer) {
if (typeof timer === 'number' || typeof timer === 'string') {
if (timer in KNOWN_TIMERS) {
clearInterval(KNOWN_TIMERS[timer]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think we may want to avoid accessing global.clearInterval here … maybe doing something like we do for clearTimeout, i.e. const clearInterval = exports.clearInterval = … is a good idea here too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would timer = KNOWN_TIMERS[timer] work? This would avoid a recursion.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

fixed

}
return;
}
if (timer && timer._repeat) {
timer._repeat = null;
clearTimeout(timer);
Expand All @@ -521,6 +536,9 @@ function unrefdHandle(timer, now) {
return true;
}

Timeout.prototype[Symbol.toPrimitive] = function() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why @@toPrimitive rather than valueOf?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The 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.

return this[async_id_symbol];
};

Timeout.prototype.unref = function() {
if (this._handle) {
Expand Down Expand Up @@ -558,6 +576,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' &&
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-timers-toPrimitive.js
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you check that +timeout1 === timeout1[Symbol.toPrimitive]()?

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]);