Skip to content

Commit

Permalink
Merge pull request #27 from StrahilKazlachev/fix/publish/handle-callb…
Browse files Browse the repository at this point in the history
…ack-error

fix(index): handle errors per callback invocation
  • Loading branch information
EisenbergEffect authored Nov 25, 2016
2 parents 3fb7cc0 + 64b449a commit 677e793
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ class Handler {
}
}

function invokeCallback(callback, data, event) {
try {
callback(data, event);
} catch (e) {
logger.error(e);
}
}

function invokeHandler(handler, data) {
try {
handler.handle(data);
} catch (e) {
logger.error(e);
}
}

/**
* Represents a disposable subsciption to an EventAggregator event.
*/
Expand Down Expand Up @@ -56,24 +72,16 @@ export class EventAggregator {
subscribers = subscribers.slice();
i = subscribers.length;

try {
while (i--) {
subscribers[i](data, event);
}
} catch (e) {
logger.error(e);
while (i--) {
invokeCallback(subscribers[i], data, event);
}
}
} else {
subscribers = this.messageHandlers.slice();
i = subscribers.length;

try {
while (i--) {
subscribers[i].handle(event);
}
} catch (e) {
logger.error(e);
while (i--) {
invokeHandler(subscribers[i], event);
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/event-aggregator.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ describe('event aggregator', () => {

ea.subscribe('dinner', crash);
ea.subscribe('dinner', callback);
ea.subscribe('dinner', crash);

ea.publish('dinner', data);

Expand Down

0 comments on commit 677e793

Please sign in to comment.