Skip to content

Commit

Permalink
Merge pull request #12 from BladeRunnerJS/add-flag-to-optionally-prev…
Browse files Browse the repository at this point in the history
…ent-errors-swallowed

Add flag to optionally prevent errors swallowed
  • Loading branch information
thecapdan authored Nov 25, 2016
2 parents 9354cc8 + 96d9e33 commit 06707f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "emitr",
"version": "0.0.8",
"version": "0.0.9",
"description": "An node/browser event emitter that supports dispatching based on types.",
"homepage": "http://BladeRunnerJS.github.io/emitr",
"license": "MIT",
Expand Down
18 changes: 12 additions & 6 deletions src/Emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ var MultiMap = require('./MultiMap');
///////////////////////////////////////////////////////////////////////////
var ONCE_FUNCTION_MARKER = {};

function notify(listeners, args) {
function notify(listeners, suppressErrors, args) {
if (listeners.length === 0) { return false; }
// take a copy in case one of the callbacks modifies the listeners array.
listeners = listeners.slice();
for (var i = 0, len = listeners.length; i < len; ++i) {
var listener = listeners[i];
try {
if (suppressErrors) {
try {
listener.callback.apply(listener.context, args);
} catch (e) {
// Swallowing the errors will make this for-loop resilient
}
} else {
listener.callback.apply(listener.context, args);
} catch(e) {
// do nothing
}
}
return true;
Expand Down Expand Up @@ -50,6 +54,8 @@ function Emitter() {
this._emitterMetaEventsOn = false;
}

Emitter.suppressErrors = true;

Emitter.prototype = {
/**
* Registers a listener for an event.
Expand Down Expand Up @@ -203,7 +209,7 @@ Emitter.prototype = {
args = slice.call(arguments, 1);
if (this._emitterListeners.hasAny(event)) {
anyListeners = true;
notify(this._emitterListeners.getValues(event), args);
notify(this._emitterListeners.getValues(event), Emitter.suppressErrors, args);
}

// navigate up the prototype chain emitting against the constructors.
Expand All @@ -212,7 +218,7 @@ Emitter.prototype = {
while (proto !== null && proto !== last) {
if (this._emitterListeners.hasAny(proto.constructor)) {
anyListeners = true;
notify(this._emitterListeners.getValues(proto.constructor), arguments);
notify(this._emitterListeners.getValues(proto.constructor), Emitter.suppressErrors, arguments);
}
last = proto;
proto = Object.getPrototypeOf(proto);
Expand Down

0 comments on commit 06707f8

Please sign in to comment.