Skip to content

Commit

Permalink
deal with errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Calvin Metcalf committed Apr 4, 2015
1 parent b23bf0b commit 2ef252d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
26 changes: 21 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,38 @@ var types = [
require('./timeout')
];
var draining;
var currentQueue;
var queueIndex = -1;
var queue = [];
function cleanUpNextTick() {
draining = false;
if (currentQueue && currentQueue.length) {
queue = currentQueue.concat(queue);
} else {
queueIndex = -1;
}
if (queue.length) {
nextTick();
}
}

//named nextTick for less confusing stack traces
function nextTick() {
draining = true;
var i, oldQueue;
var len = queue.length;
var timeout = setTimeout(cleanUpNextTick);
while (len) {
oldQueue = queue;
currentQueue = queue;
queue = [];
i = -1;
while (++i < len) {
oldQueue[i]();
while (++queueIndex < len) {
currentQueue[queueIndex]();
}
queueIndex = -1;
len = queue.length;
}
queueIndex = -1;
draining = false;
clearTimeout(timeout);
}
var scheduleDrain;
var i = -1;
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
"build": "npm run build-js && npm run uglify",
"uglify": "uglifyjs dist/immediate.js -mc > dist/immediate.min.js",
"build-js": "browserify -s immediate ./lib/index.js > dist/immediate.js",
"test": "jshint lib/*.js && node test/tests.js && browserify test/tests.js | testling"
"test": "jshint lib/*.js && node test/tests.js | tspec"
},
"jam": {
"main": "dist/immediate.js"
},
"devDependencies": {
"uglify-js": "^2.4.13",
"tape": "^2.13.1",
"jshint": "^2.5.1",
"browserify": "^4.1.5",
"testling": "^1.6.1"
"jshint": "^2.5.1",
"tap-spec": "^3.0.0",
"uglify-js": "^2.4.13",
"tape": "^4.0.0"
}
}
27 changes: 26 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,29 @@ if (process.browser && typeof Worker !== 'undefined') {
};
worker.postMessage('ping');
});
}
}

test('test errors', function (t) {
t.plan(7);
var order = 0;
process.once('uncaughtException', function(err) {
t.ok(true, err.message);
t.equals(2, order++, 'error is third');
immediate(function () {
t.equals(5, order++, 'schedualed in error is last');
});
});
immediate(function () {
t.equals(0, order++, 'first one works');
immediate(function () {
t.equals(4, order++, 'recursive one is 4th');
});
});
immediate(function () {
t.equals(1, order++, 'second one starts');
throw(new Error('an error is thrown'));
});
immediate(function () {
t.equals(3, order++, '3rd schedualed happens after the error');
});
});

0 comments on commit 2ef252d

Please sign in to comment.