Skip to content

Commit

Permalink
timers: fix subsequent enroll calls not working
Browse files Browse the repository at this point in the history
A bug was introduced in #17704 which meant that subsequent
calls to enroll would unset the new _idleTimeout and the
enrolled object could never again function as a timer.

PR-URL: #19936
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Jeremiah Senkpiel <[email protected]>
  • Loading branch information
apapirovski committed Apr 17, 2018
1 parent 31d6cec commit 734eb17
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,14 @@ exports.unenroll = util.deprecate(unenroll,
// This function does not start the timer, see `active()`.
// Using existing objects as timers slightly reduces object overhead.
function enroll(item, msecs) {
item._idleTimeout = validateTimerDuration(msecs);
msecs = validateTimerDuration(msecs);

// if this item was already in a list somewhere
// then we should unenroll it from that
if (item._idleNext) unenroll(item);

L.init(item);
item._idleTimeout = msecs;
}

exports.enroll = util.deprecate(enroll,
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-timers-enroll-second-time.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const common = require('../common');

const assert = require('assert');
const timers = require('timers');

const enrollObj = {
_onTimeout: common.mustCall(),
};

timers.enroll(enrollObj, 1);
assert.strictEqual(enrollObj._idleTimeout, 1);
timers.enroll(enrollObj, 10);
assert.strictEqual(enrollObj._idleTimeout, 10);
timers.active(enrollObj);

0 comments on commit 734eb17

Please sign in to comment.