2424const async_wrap = process.binding('async_wrap');
2525const TimerWrap = process.binding('timer_wrap').Timer;
2626const L = require('internal/linkedlist');
27+ const timerInternals = require('internal/timers');
2728const internalUtil = require('internal/util');
2829const { createPromise, promiseResolve } = process.binding('util');
2930const assert = require('assert');
@@ -44,8 +45,8 @@ const {
4445// Grab the constants necessary for working with internal arrays.
4546const { kInit, kDestroy, kAsyncIdCounter } = async_wrap.constants;
4647// Symbols for storing async id state.
47- const async_id_symbol = Symbol('asyncId') ;
48- const trigger_async_id_symbol = Symbol('triggerAsyncId') ;
48+ const async_id_symbol = timerInternals.async_id_symbol ;
49+ const trigger_async_id_symbol = timerInternals.trigger_async_id_symbol ;
4950
5051/* This is an Uint32Array for easier sharing with C++ land. */
5152const scheduledImmediateCount = process._scheduledImmediateCount;
@@ -55,7 +56,10 @@ const activateImmediateCheck = process._activateImmediateCheck;
5556delete process._activateImmediateCheck;
5657
5758// Timeout values > TIMEOUT_MAX are set to 1.
58- const TIMEOUT_MAX = 2 ** 31 - 1;
59+ const TIMEOUT_MAX = timerInternals.TIMEOUT_MAX;
60+
61+ // The Timeout class
62+ const Timeout = timerInternals.Timeout;
5963
6064
6165// HOW and WHY the timers implementation works the way it does.
@@ -446,12 +450,17 @@ function setTimeout(callback, after, arg1, arg2, arg3) {
446450 break;
447451 }
448452
449- return new Timeout(callback, after, args, false);
453+ const timeout = new Timeout(callback, after, args, false);
454+ active(timeout);
455+
456+ return timeout;
450457}
451458
452459setTimeout[internalUtil.promisify.custom] = function(after, value) {
453460 const promise = createPromise();
454- new Timeout(promise, after, [value], false);
461+ const timeout = new Timeout(promise, after, [value], false);
462+ active(timeout);
463+
455464 return promise;
456465};
457466
@@ -523,7 +532,10 @@ exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
523532 break;
524533 }
525534
526- return new Timeout(callback, repeat, args, true);
535+ const timeout = new Timeout(callback, repeat, args, true);
536+ active(timeout);
537+
538+ return timeout;
527539};
528540
529541exports.clearInterval = function(timer) {
@@ -534,44 +546,6 @@ exports.clearInterval = function(timer) {
534546};
535547
536548
537- function Timeout(callback, after, args, isRepeat) {
538- after *= 1; // coalesce to number or NaN
539- if (!(after >= 1 && after <= TIMEOUT_MAX)) {
540- if (after > TIMEOUT_MAX) {
541- process.emitWarning(`${after} does not fit into` +
542- ' a 32-bit signed integer.' +
543- '\nTimeout duration was set to 1.',
544- 'TimeoutOverflowWarning');
545- }
546- after = 1; // schedule on next tick, follows browser behavior
547- }
548-
549- this._called = false;
550- this._idleTimeout = after;
551- this._idlePrev = this;
552- this._idleNext = this;
553- this._idleStart = null;
554- // this must be set to null first to avoid function tracking
555- // on the hidden class, revisit in V8 versions after 6.2
556- this._onTimeout = null;
557- this._onTimeout = callback;
558- this._timerArgs = args;
559- this._repeat = isRepeat ? after : null;
560- this._destroyed = false;
561-
562- this[async_id_symbol] = ++async_id_fields[kAsyncIdCounter];
563- this[trigger_async_id_symbol] = getDefaultTriggerAsyncId();
564- if (async_hook_fields[kInit] > 0) {
565- emitInit(this[async_id_symbol],
566- 'Timeout',
567- this[trigger_async_id_symbol],
568- this);
569- }
570-
571- active(this);
572- }
573-
574-
575549function unrefdHandle() {
576550 // Don't attempt to call the callback if it is not a function.
577551 if (typeof this.owner._onTimeout === 'function') {
0 commit comments