24
24
const async_wrap = process . binding ( 'async_wrap' ) ;
25
25
const TimerWrap = process . binding ( 'timer_wrap' ) . Timer ;
26
26
const L = require ( 'internal/linkedlist' ) ;
27
+ const timerInternals = require ( 'internal/timers' ) ;
27
28
const internalUtil = require ( 'internal/util' ) ;
28
29
const { createPromise, promiseResolve } = process . binding ( 'util' ) ;
29
30
const assert = require ( 'assert' ) ;
@@ -44,8 +45,8 @@ const {
44
45
// Grab the constants necessary for working with internal arrays.
45
46
const { kInit, kDestroy, kAsyncIdCounter } = async_wrap . constants ;
46
47
// 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 ;
49
50
50
51
/* This is an Uint32Array for easier sharing with C++ land. */
51
52
const scheduledImmediateCount = process . _scheduledImmediateCount ;
@@ -55,7 +56,10 @@ const activateImmediateCheck = process._activateImmediateCheck;
55
56
delete process . _activateImmediateCheck ;
56
57
57
58
// 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 ;
59
63
60
64
61
65
// HOW and WHY the timers implementation works the way it does.
@@ -446,12 +450,17 @@ function setTimeout(callback, after, arg1, arg2, arg3) {
446
450
break ;
447
451
}
448
452
449
- return new Timeout ( callback , after , args , false ) ;
453
+ const timeout = new Timeout ( callback , after , args , false ) ;
454
+ active ( timeout ) ;
455
+
456
+ return timeout ;
450
457
}
451
458
452
459
setTimeout [ internalUtil . promisify . custom ] = function ( after , value ) {
453
460
const promise = createPromise ( ) ;
454
- new Timeout ( promise , after , [ value ] , false ) ;
461
+ const timeout = new Timeout ( promise , after , [ value ] , false ) ;
462
+ active ( timeout ) ;
463
+
455
464
return promise ;
456
465
} ;
457
466
@@ -523,7 +532,10 @@ exports.setInterval = function(callback, repeat, arg1, arg2, arg3) {
523
532
break ;
524
533
}
525
534
526
- return new Timeout ( callback , repeat , args , true ) ;
535
+ const timeout = new Timeout ( callback , repeat , args , true ) ;
536
+ active ( timeout ) ;
537
+
538
+ return timeout ;
527
539
} ;
528
540
529
541
exports . clearInterval = function ( timer ) {
@@ -534,44 +546,6 @@ exports.clearInterval = function(timer) {
534
546
} ;
535
547
536
548
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
-
575
549
function unrefdHandle ( ) {
576
550
// Don't attempt to call the callback if it is not a function.
577
551
if ( typeof this . owner . _onTimeout === 'function' ) {
0 commit comments