diff --git a/lib/timers.js b/lib/timers.js index 2ef3e32da9a26e..35f9ddee0bd158 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -24,24 +24,23 @@ var triggerTimers = new Map(); // the main function - creates lists on demand and the watchers associated // with them. function insert(item, msecs) { - item._idleStart = Timer.now(); - item._idleTimeout = msecs; - if (msecs < 0) return; - var timer; - - if (! lists[msecs]) { - timer = new Timer(); - timer.start(msecs, 0); - timer.msecs = msecs; - timer[kOnTimeout] = listOnTimeout; + item._idleStart = Timer.now(); + item._idleTimeout = msecs; - lists[msecs] = new Set(); - triggerTimers.set(msecs, timer); + if (lists[msecs]) { + lists[msecs].add(item); + return; } - lists[msecs].add(item); + lists[msecs] = new Set().add(item); + + const timer = new Timer(); + timer.start(msecs, 0); + timer.msecs = msecs; + timer[kOnTimeout] = listOnTimeout; + triggerTimers.set(msecs, timer); } function listOnTimeout() { @@ -51,53 +50,54 @@ function listOnTimeout() { var now = Timer.now(); var diff, first, threw; - if (lists[msecs]) { - for (let first of lists[msecs]) { - diff = now - first._idleStart; - if (diff < msecs) { - list.start(msecs - diff, 0); - return; - } else { - lists[msecs].delete(first); - if (!first._onTimeout) continue; - - // v0.4 compatibility: if the timer callback throws and the - // domain or uncaughtException handler ignore the exception, - // other timers that expire on this tick should still run. - // - // https://github.com/joyent/node/issues/2631 - var domain = first.domain; - if (domain && domain._disposed) - continue; - - try { - if (domain) - domain.enter(); - threw = true; - first._called = true; - first._onTimeout(); - if (domain) - domain.exit(); - threw = false; - } finally { - if (threw) { - // We need to continue processing after domain error handling - // is complete, but not by using whatever domain was left over - // when the timeout threw its exception. - var oldDomain = process.domain; - process.domain = null; - process.nextTick(listOnTimeoutNT, list); - process.domain = oldDomain; - } + for (let first of lists[msecs]) { + diff = now - first._idleStart; + if (diff < msecs) { + list.start(msecs - diff, 0); + return; + } else { + lists[msecs].delete(first); + if (!first._onTimeout) continue; + + // v0.4 compatibility: if the timer callback throws and the + // domain or uncaughtException handler ignore the exception, + // other timers that expire on this tick should still run. + // + // https://github.com/joyent/node/issues/2631 + var domain = first.domain; + if (domain && domain._disposed) + continue; + + try { + if (domain) + domain.enter(); + + threw = true; + first._called = true; + first._onTimeout(); + + if (domain) + domain.exit(); + + threw = false; + } finally { + if (threw) { + // We need to continue processing after domain error handling + // is complete, but not by using whatever domain was left over + // when the timeout threw its exception. + var oldDomain = process.domain; + process.domain = null; + process.nextTick(listOnTimeoutNT, list); + process.domain = oldDomain; } } } } - - list.close(); if (lists[msecs] && lists[msecs].size === 0) { delete lists[msecs]; } + + list.close(); } @@ -530,7 +530,7 @@ function unrefTimeout() { // call the onTimeout callback for those expired, // and rearm the actual timer if the next timeout to expire // will expire before the current actual timer. - for (var cur of unrefList) { + for (let cur of unrefList) { timeSinceLastActive = now - cur._idleStart; if (timeSinceLastActive < cur._idleTimeout) {