You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
995 B
48 lines
995 B
|
3 years ago
|
var isBrowser = require('./isBrowser');
|
||
|
|
|
||
|
|
var listeners = [];
|
||
|
|
var isOn = false;
|
||
|
|
exports = {
|
||
|
|
start: function() {
|
||
|
|
isOn = true;
|
||
|
|
},
|
||
|
|
stop: function() {
|
||
|
|
isOn = false;
|
||
|
|
},
|
||
|
|
addListener: function(fn) {
|
||
|
|
listeners.push(fn);
|
||
|
|
},
|
||
|
|
rmListener: function(fn) {
|
||
|
|
var idx = listeners.indexOf(fn);
|
||
|
|
|
||
|
|
if (idx > -1) {
|
||
|
|
listeners.splice(idx, 1);
|
||
|
|
}
|
||
|
|
},
|
||
|
|
rmAllListeners: function() {
|
||
|
|
listeners = [];
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
if (isBrowser) {
|
||
|
|
window.addEventListener('error', function(event) {
|
||
|
|
callListeners(event.error);
|
||
|
|
});
|
||
|
|
window.addEventListener('unhandledrejection', function(e) {
|
||
|
|
callListeners(e.reason);
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
process.on('uncaughtException', callListeners);
|
||
|
|
process.on('unhandledRejection', callListeners);
|
||
|
|
}
|
||
|
|
|
||
|
|
function callListeners(err) {
|
||
|
|
if (!isOn) return;
|
||
|
|
|
||
|
|
for (var i = 0, len = listeners.length; i < len; i++) {
|
||
|
|
listeners[i](err);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
module.exports = exports;
|