Skip to content

Commit

Permalink
Eventable: reduce code duplication in fire method
Browse files Browse the repository at this point in the history
  • Loading branch information
taye committed Jan 10, 2016
1 parent 2a6e790 commit a34f212
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Eventable.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
const { indexOf } = require('./utils/arr');

function fireUntilImmediateStopped (event, listeners) {
for (let i = 0, len = listeners.length; i < len && !event.immediatePropagationStopped; i++) {
listeners[i](event);
}
}

class Eventable {
fire (event) {
let listeners;
const onEvent = 'on' + event.type;
const global = this.global;

// Interactable#on() listeners
if (event.type in this) {
listeners = this[event.type];

for (let i = 0, len = listeners.length; i < len && !event.immediatePropagationStopped; i++) {
listeners[i](event);
}
if ((listeners = this[event.type])) {
fireUntilImmediateStopped(event, listeners);
}

// interactable.onevent listener
Expand All @@ -21,11 +23,8 @@ class Eventable {
}

// interact.on() listeners
if (global && event.type in global && (listeners = global[event.type])) {

for (let i = 0, len = listeners.length; i < len && !event.immediatePropagationStopped; i++) {
listeners[i](event);
}
if (!event.propagationStopped && global && (listeners = global[event.type])) {
fireUntilImmediateStopped(event, listeners);
}
}

Expand Down

0 comments on commit a34f212

Please sign in to comment.