Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Simplify Evented code (#339)
  • Loading branch information
RobbieTheWagner committed Oct 1, 2019
1 parent e53d6ee commit 8384628
Showing 1 changed file with 9 additions and 18 deletions.
27 changes: 9 additions & 18 deletions src/js/evented.js
Expand Up @@ -26,38 +26,29 @@ export class Evented {
if (isUndefined(handler)) {
delete this.bindings[event];
} else {
let i = 0;
while (i < this.bindings[event].length) {
if (this.bindings[event][i].handler === handler) {
this.bindings[event].splice(i, 1);
} else {
++i;
this.bindings[event].forEach((binding, index) => {
if (binding.handler === handler) {
this.bindings[event].splice(index, 1);
}
}
});
}

return this;
}

trigger(event, ...args) {
if (!isUndefined(this.bindings) && this.bindings[event]) {
let i = 0;
while (i < this.bindings[event].length) {
const { handler, ctx, once } = this.bindings[event][i];
this.bindings[event].forEach((binding, index) => {
const { ctx, handler, once } = binding;

let context = ctx;
if (isUndefined(context)) {
context = this;
}
const context = ctx || this;

handler.apply(context, args);

if (once) {
this.bindings[event].splice(i, 1);
} else {
++i;
this.bindings[event].splice(index, 1);
}
}
});
}

return this;
Expand Down

0 comments on commit 8384628

Please sign in to comment.