Skip to content

Commit

Permalink
fix(EventBus): Fix faulty logic for off, caused by Dash-Industry-Fo…
Browse files Browse the repository at this point in the history
  • Loading branch information
justinanastos committed Apr 25, 2017
1 parent 4ded8c8 commit 58a56cb
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/core/EventBus.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function EventBus() {
};

const inserted = handlers[type].some((item , idx) => {
if (priority > item.priority ) {
if (item && priority > item.priority ) {
handlers[type].splice(idx, 0, handler);
return true;
}
Expand All @@ -72,8 +72,11 @@ function EventBus() {
if (!type || !listener || !handlers[type]) return;
const idx = getHandlerIdx(type, listener, scope);
if (idx < 0) return;
handlers[type] = handlers[type].slice(0);
handlers[type].splice(idx, 1);
handlers[type][idx] = null;

setTimeout(() => {
handlers[type] = handlers[type].filter((item) => item);
}, 0);
}

function trigger(type, payload) {
Expand All @@ -85,7 +88,7 @@ function EventBus() {

payload.type = type;

handlers[type].forEach( handler => handler.callback.call(handler.scope, payload) );
handlers[type].forEach( handler => handler && handler.callback.call(handler.scope, payload) );
}

function getHandlerIdx(type, listener, scope) {
Expand All @@ -95,7 +98,7 @@ function EventBus() {
if (!handlers[type]) return idx;

handlers[type].some( (item, index) => {
if (item.callback === listener && (!scope || scope === item.scope)) {
if (item && item.callback === listener && (!scope || scope === item.scope)) {
idx = index;
return true;
}
Expand All @@ -121,4 +124,4 @@ EventBus.__dashjs_factory_name = 'EventBus';
const factory = FactoryMaker.getSingletonFactory(EventBus);
factory.EVENT_PRIORITY_LOW = EVENT_PRIORITY_LOW;
factory.EVENT_PRIORITY_HIGH = EVENT_PRIORITY_HIGH;
export default factory;
export default factory;

0 comments on commit 58a56cb

Please sign in to comment.