Skip to content

Commit

Permalink
Added browser implementation for awaitGlobalException
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoly Ressin committed Mar 23, 2015
1 parent d72d11b commit 59eda5c
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions test/mocha/helpers/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,26 @@ var token = {};
module.exports = {
awaitGlobalException: function(fn) {
if (typeof process !== "undefined" && typeof process.version === "string") {
function replaceListeners(by) {
var single = typeof by === "function";
if (process.title === "browser") {
var original = window.onerror;
window.onerror = single ? function(message, file, line, column, e) {
return by(e);
} : by[0];
return [original];
} else {
var original = process.listeners("uncaughtException");
process.removeAllListeners("uncaughtException");
if (single) by = [by];
by.forEach(function(listener) {
process.on("uncaughtException", listener);
});
return original;
}
}
return new Promise(function(resolve, reject) {
var listeners = process.listeners("uncaughtException");
process.removeAllListeners("uncaughtException");
process.on("uncaughtException", function(e) {
var listeners = replaceListeners(function(e) {
var err;
var ret;
try {
Expand All @@ -15,11 +31,7 @@ module.exports = {
err = e;
}
if (!err && ret === false) return;
process.removeAllListeners("uncaughtException");
listeners.forEach(function(listener) {
process.on("uncaughtException", listener);
});

replaceListeners(listeners);
Promise.delay(1).then(function() {
if (err) reject(err);
resolve();
Expand Down

0 comments on commit 59eda5c

Please sign in to comment.