Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 17 additions & 34 deletions src/js/evented.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
export class Evented {
constructor(/* options = {}*/) {
// TODO: do we need this empty constructor?
}
import _ from 'lodash';

export class Evented {
on(event, handler, ctx) {
const once = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];

if (typeof this.bindings === 'undefined') {
if (_.isUndefined(this.bindings)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think the original intent is to check if it's an object, perhaps that one instead?

this.bindings = {};
}
if (typeof this.bindings[event] === 'undefined') {
if (_.isUndefined(this.bindings[event])) {
this.bindings[event] = [];
}
this.bindings[event].push({ handler, ctx, once });
Expand All @@ -20,51 +18,36 @@ export class Evented {
}

off(event, handler) {
if (typeof this.bindings === 'undefined' || typeof this.bindings[event] === 'undefined') {
if (_.isUndefined(this.bindings) || _.isUndefined(this.bindings[event])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for this first statement

return false;
}

if (typeof handler === 'undefined') {
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);
}
}
});
}
}

trigger(event) {
if (typeof this.bindings !== 'undefined' && this.bindings[event]) {
const _len = arguments.length;
const args = Array(_len > 1 ? _len - 1 : 0);
let i = 0;
if (!_.isUndefined(this.bindings) && this.bindings[event]) {
const args = _.drop(arguments);

for (let _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
this.bindings[event].forEach((binding, index) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so much easier to read.

const { ctx, handler, once } = binding;

while (i < this.bindings[event].length) {
const _bindings$event$i = this.bindings[event][i];
const { ctx, handler, once } = _bindings$event$i;

let context = ctx;
if (typeof context === 'undefined') {
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);
}
}
});
}
}

Expand Down