diff --git a/lib/timers.js b/lib/timers.js index 478a05433912f3..dc2506e01e09a4 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -100,7 +100,7 @@ const unrefedLists = {}; // Schedule or re-schedule a timer. // The item must have been enroll()'d first. -exports.active = function(item) { +const active = exports.active = function(item) { insert(item, false); }; @@ -351,19 +351,19 @@ exports.setTimeout = function(callback, after) { if (process.domain) timer.domain = process.domain; - exports.active(timer); + active(timer); return timer; }; -exports.clearTimeout = function(timer) { +const clearTimeout = exports.clearTimeout = function(timer) { if (timer && (timer[kOnTimeout] || timer._onTimeout)) { timer[kOnTimeout] = timer._onTimeout = null; if (timer instanceof Timeout) { timer.close(); // for after === 0 } else { - exports.unenroll(timer); + unenroll(timer); } } }; @@ -409,7 +409,7 @@ exports.setInterval = function(callback, repeat) { timer._repeat = ontimeout; if (process.domain) timer.domain = process.domain; - exports.active(timer); + active(timer); return timer; @@ -425,7 +425,7 @@ exports.setInterval = function(callback, repeat) { this._handle.start(repeat, 0); } else { timer._idleTimeout = repeat; - exports.active(timer); + active(timer); } } }; @@ -468,7 +468,7 @@ Timeout.prototype.unref = function() { // Prevent running cb again when unref() is called during the same cb if (this._called && !this._repeat) { - exports.unenroll(this); + unenroll(this); return; } @@ -496,7 +496,7 @@ Timeout.prototype.close = function() { this._handle[kOnTimeout] = null; this._handle.close(); } else { - exports.unenroll(this); + unenroll(this); } return this; }; diff --git a/test/parallel/test-timers-api-refs.js b/test/parallel/test-timers-api-refs.js new file mode 100644 index 00000000000000..c062369444b1e0 --- /dev/null +++ b/test/parallel/test-timers-api-refs.js @@ -0,0 +1,21 @@ +'use strict'; +const common = require('../common'); +const timers = require('timers'); + +// delete global APIs to make sure they're not relied on by the internal timers +// code +delete global.setTimeout; +delete global.clearTimeout; +delete global.setInterval; +delete global.clearInterval; +delete global.setImmediate; +delete global.clearImmediate; + +const timeoutCallback = () => { timers.clearTimeout(timeout); }; +const timeout = timers.setTimeout(common.mustCall(timeoutCallback), 1); + +const intervalCallback = () => { timers.clearInterval(interval); }; +const interval = timers.setInterval(common.mustCall(intervalCallback), 1); + +const immediateCallback = () => { timers.clearImmediate(immediate); }; +const immediate = timers.setImmediate(immediateCallback);