Skip to content
Prev Previous commit
fixup: replace all sets with arrays
  • Loading branch information
Trott committed Sep 26, 2015
commit bf5c0049cbc1bde1a5e6b03d8288ebda5e5fc807
66 changes: 34 additions & 32 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ const unenroll = exports.unenroll = function(item) {

// if empty then stop the watcher
if (timers.length === 0 && triggerTimers.length !== 0) {
triggerTimers.forEach(function(tt) {
tt.close();
});
for (let i = 0; i < triggerTimers.length; i++) {
triggerTimers[i].close();
}
triggerTimers = [];
}
// if active is called later, then we want to make sure not to insert again
Expand Down Expand Up @@ -389,17 +389,17 @@ Timeout.prototype.close = function() {
};


var immediateQueue = new Set();
var immediateQueue = [];


function processImmediate() {
var queue = immediateQueue;
var domain, immediate;

immediateQueue = new Set();
immediateQueue = [];

for (let immediate of queue) {
queue.delete(immediate);
for (let i = 0; i < queue.length; i++) {
immediate = queue[i];
domain = immediate.domain;

if (domain)
Expand All @@ -413,15 +413,10 @@ function processImmediate() {
threw = false;
} finally {
if (threw) {
if (!queue.size === 0) {
// Handle any remaining on next tick, assuming we're still
// alive to do so.
for (let item of immediateQueue) {
queue.add(item);
}
immediateQueue = queue;
process.nextTick(processImmediate);
}
// Handle any remaining on next tick, assuming we're still
// alive to do so.
immediateQueue = queue.slice(i + 1).concat(immediateQueue);
process.nextTick(processImmediate);
}
}

Expand All @@ -432,7 +427,7 @@ function processImmediate() {
// Only round-trip to C++ land if we have to. Calling clearImmediate() on an
// immediate that's in |queue| is okay. Worst case is we make a superfluous
// call to NeedImmediateCallbackSetter().
if (immediateQueue.size === 0) {
if (immediateQueue.length === 0) {
process._needImmediateCallback = false;
}
}
Expand Down Expand Up @@ -490,7 +485,7 @@ exports.setImmediate = function(callback, arg1, arg2, arg3) {
if (process.domain)
immediate.domain = process.domain;

immediateQueue.add(immediate);
immediateQueue.push(immediate);

return immediate;
};
Expand All @@ -501,9 +496,12 @@ exports.clearImmediate = function(immediate) {

immediate._onImmediate = undefined;

immediateQueue.delete(immediate);
const myIndex = immediateQueue.indexOf(immediate);
if (myIndex !== -1) {
immediateQueue.splice(myIndex, 1);
}

if (immediateQueue.size === 0) {
if (immediateQueue.length === 0) {
process._needImmediateCallback = false;
}
};
Expand All @@ -512,17 +510,23 @@ exports.clearImmediate = function(immediate) {
// Internal APIs that need timeouts should use timers._unrefActive instead of
// timers.active as internal timeouts shouldn't hold the loop open

var unrefList, unrefTimer;
var unrefArray = [];
var unrefTimer;

function _makeTimerTimeout(timer) {
var domain = timer.domain;
var msecs = timer._idleTimeout;
let myIndex;

const myIndex = timers.indexOf(timer);
myIndex = timers.indexOf(timer);
if (myIndex !== -1) {
timers.splice(myIndex, 1);
}
unrefList.delete(timer);

myIndex = unrefArray.indexOf(timer);
if (myIndex !== -1) {
unrefArray.splice(myIndex, 1);
}

// Timer has been unenrolled by another timer that fired at the same time,
// so don't make it timeout.
Expand Down Expand Up @@ -576,7 +580,8 @@ 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 (let cur of unrefList) {
for (let i = 0; i < unrefArray.length; i++) {
let cur = unrefArray[i];
timeSinceLastActive = now - cur._idleStart;

if (timeSinceLastActive < cur._idleTimeout) {
Expand Down Expand Up @@ -624,14 +629,11 @@ exports._unrefActive = function(item) {
if (myIndex !== -1) {
timers.splice(myIndex, 1);
}
if (!unrefList) {
unrefList = new Set();

unrefTimer = new Timer();
unrefTimer.unref();
unrefTimer.when = -1;
unrefTimer[kOnTimeout] = unrefTimeout;
}
unrefTimer = new Timer();
unrefTimer.unref();
unrefTimer.when = -1;
unrefTimer[kOnTimeout] = unrefTimeout;

var now = Timer.now();
item._idleStart = now;
Expand All @@ -645,5 +647,5 @@ exports._unrefActive = function(item) {
unrefTimer.when = when;
}

unrefList.add(item);
unrefArray.push(item);
};