Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deal with errors #18

Merged
merged 1 commit into from
Apr 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});