@@ -167,11 +167,15 @@ exports._unrefActive = function(item) {
167
167
// Appends a timer onto the end of an existing timers list, or creates a new
168
168
// TimerWrap backed list if one does not already exist for the specified timeout
169
169
// duration.
170
- function insert ( item , unrefed ) {
170
+ function insert ( item , unrefed , start ) {
171
171
const msecs = item . _idleTimeout ;
172
172
if ( msecs < 0 || msecs === undefined ) return ;
173
173
174
- item . _idleStart = TimerWrap . now ( ) ;
174
+ if ( typeof start === 'number' ) {
175
+ item . _idleStart = start ;
176
+ } else {
177
+ item . _idleStart = TimerWrap . now ( ) ;
178
+ }
175
179
176
180
const lists = unrefed === true ? unrefedLists : refedLists ;
177
181
@@ -446,16 +450,17 @@ function ontimeout(timer) {
446
450
var args = timer . _timerArgs ;
447
451
if ( typeof timer . _onTimeout !== 'function' )
448
452
return promiseResolve ( timer . _onTimeout , args [ 0 ] ) ;
453
+ const start = TimerWrap . now ( ) ;
449
454
if ( ! args )
450
455
timer . _onTimeout ( ) ;
451
456
else
452
457
Reflect . apply ( timer . _onTimeout , timer , args ) ;
453
458
if ( timer . _repeat )
454
- rearm ( timer ) ;
459
+ rearm ( timer , start ) ;
455
460
}
456
461
457
462
458
- function rearm ( timer ) {
463
+ function rearm ( timer , start ) {
459
464
// // Do not re-arm unenroll'd or closed timers.
460
465
if ( timer . _idleTimeout === - 1 ) return ;
461
466
@@ -464,7 +469,15 @@ function rearm(timer) {
464
469
timer . _handle . start ( timer . _repeat ) ;
465
470
} else {
466
471
timer . _idleTimeout = timer . _repeat ;
467
- active ( timer ) ;
472
+
473
+ const duration = TimerWrap . now ( ) - start ;
474
+ if ( duration >= timer . _repeat ) {
475
+ // If callback duration >= timer._repeat,
476
+ // add 1 ms to avoid blocking eventloop
477
+ insert ( timer , false , start + duration - timer . _repeat + 1 ) ;
478
+ } else {
479
+ insert ( timer , false , start ) ;
480
+ }
468
481
}
469
482
}
470
483
0 commit comments