@@ -155,7 +155,7 @@ function initAsyncResource(resource, type) {
155
155
156
156
// Timer constructor function.
157
157
// The entire prototype is defined in lib/timers.js
158
- function Timeout ( callback , after , args , isRepeat ) {
158
+ function Timeout ( callback , after , args , isRepeat , isRefed ) {
159
159
after *= 1 ; // Coalesce to number or NaN
160
160
if ( ! ( after >= 1 && after <= TIMEOUT_MAX ) ) {
161
161
if ( after > TIMEOUT_MAX ) {
@@ -179,7 +179,9 @@ function Timeout(callback, after, args, isRepeat) {
179
179
this . _repeat = isRepeat ? after : null ;
180
180
this . _destroyed = false ;
181
181
182
- this [ kRefed ] = null ;
182
+ if ( isRefed )
183
+ incRefCount ( ) ;
184
+ this [ kRefed ] = isRefed ;
183
185
184
186
initAsyncResource ( this , 'Timeout' ) ;
185
187
}
@@ -295,27 +297,43 @@ function decRefCount() {
295
297
// Schedule or re-schedule a timer.
296
298
// The item must have been enroll()'d first.
297
299
function active ( item ) {
298
- insert ( item , true , getLibuvNow ( ) ) ;
300
+ insertGuarded ( item , true ) ;
299
301
}
300
302
301
303
// Internal APIs that need timeouts should use `unrefActive()` instead of
302
304
// `active()` so that they do not unnecessarily keep the process open.
303
305
function unrefActive ( item ) {
304
- insert ( item , false , getLibuvNow ( ) ) ;
306
+ insertGuarded ( item , false ) ;
305
307
}
306
308
307
309
// The underlying logic for scheduling or re-scheduling a timer.
308
310
//
309
311
// Appends a timer onto the end of an existing timers list, or creates a new
310
312
// list if one does not already exist for the specified timeout duration.
311
- function insert ( item , refed , start ) {
312
- let msecs = item . _idleTimeout ;
313
+ function insertGuarded ( item , refed , start ) {
314
+ const msecs = item . _idleTimeout ;
313
315
if ( msecs < 0 || msecs === undefined )
314
316
return ;
315
317
316
- // Truncate so that accuracy of sub-millisecond timers is not assumed.
317
- msecs = MathTrunc ( msecs ) ;
318
+ insert ( item , msecs , start ) ;
319
+
320
+ if ( ! item [ async_id_symbol ] || item . _destroyed ) {
321
+ item . _destroyed = false ;
322
+ initAsyncResource ( item , 'Timeout' ) ;
323
+ }
324
+
325
+ if ( refed === ! item [ kRefed ] ) {
326
+ if ( refed )
327
+ incRefCount ( ) ;
328
+ else
329
+ decRefCount ( ) ;
330
+ }
331
+ item [ kRefed ] = refed ;
332
+ }
318
333
334
+ function insert ( item , msecs , start = getLibuvNow ( ) ) {
335
+ // Truncate so that accuracy of sub-milisecond timers is not assumed.
336
+ msecs = MathTrunc ( msecs ) ;
319
337
item . _idleStart = start ;
320
338
321
339
// Use an existing list if there is one, otherwise we need to make a new one.
@@ -332,19 +350,6 @@ function insert(item, refed, start) {
332
350
}
333
351
}
334
352
335
- if ( ! item [ async_id_symbol ] || item . _destroyed ) {
336
- item . _destroyed = false ;
337
- initAsyncResource ( item , 'Timeout' ) ;
338
- }
339
-
340
- if ( refed === ! item [ kRefed ] ) {
341
- if ( refed )
342
- incRefCount ( ) ;
343
- else
344
- decRefCount ( ) ;
345
- }
346
- item [ kRefed ] = refed ;
347
-
348
353
L . append ( list , item ) ;
349
354
}
350
355
@@ -354,8 +359,8 @@ function setUnrefTimeout(callback, after) {
354
359
throw new ERR_INVALID_CALLBACK ( callback ) ;
355
360
}
356
361
357
- const timer = new Timeout ( callback , after , undefined , false ) ;
358
- unrefActive ( timer ) ;
362
+ const timer = new Timeout ( callback , after , undefined , false , false ) ;
363
+ insert ( timer , timer . _idleTimeout ) ;
359
364
360
365
return timer ;
361
366
}
@@ -540,16 +545,14 @@ function getTimerCallbacks(runNextTicks) {
540
545
} finally {
541
546
if ( timer . _repeat && timer . _idleTimeout !== - 1 ) {
542
547
timer . _idleTimeout = timer . _repeat ;
543
- if ( start === undefined )
544
- start = getLibuvNow ( ) ;
545
- insert ( timer , timer [ kRefed ] , start ) ;
548
+ insert ( timer , timer . _idleTimeout , start ) ;
546
549
} else if ( ! timer . _idleNext && ! timer . _idlePrev ) {
547
550
if ( timer [ kRefed ] )
548
551
refCount -- ;
549
552
timer [ kRefed ] = null ;
550
553
551
554
if ( destroyHooksExist ( ) && ! timer . _destroyed ) {
552
- emitDestroy ( timer [ async_id_symbol ] ) ;
555
+ emitDestroy ( asyncId ) ;
553
556
}
554
557
timer . _destroyed = true ;
555
558
}
@@ -598,6 +601,7 @@ module.exports = {
598
601
} ,
599
602
active,
600
603
unrefActive,
604
+ insert,
601
605
timerListMap,
602
606
timerListQueue,
603
607
decRefCount,
0 commit comments