[go: nahoru, domu]

Skip to content

Commit

Permalink
Simplify EventTargetMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz committed Nov 25, 2018
1 parent cffb42e commit 11ef535
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions core/util/eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,28 @@

export default class EventTargetMixin {
constructor() {
this._listeners = null;
this._listeners = new Map();
}

addEventListener(type, callback) {
if (!this._listeners) {
this._listeners = new Map();
}
if (!this._listeners.has(type)) {
this._listeners.set(type, new Set());
}
this._listeners.get(type).add(callback);
}

removeEventListener(type, callback) {
if (!this._listeners || !this._listeners.has(type)) {
return;
if (this._listeners.has(type)) {
this._listeners.get(type).delete(callback);
}
this._listeners.get(type).delete(callback);
}

dispatchEvent(event) {
if (!this._listeners || !this._listeners.has(event.type)) {
if (!this._listeners.has(event.type)) {
return true;
}
this._listeners.get(event.type).forEach((callback) => {
callback.call(this, event);
}, this);
this._listeners.get(event.type)
.forEach(callback => callback.call(this, event), this);
return !event.defaultPrevented;
}
}

0 comments on commit 11ef535

Please sign in to comment.