From a34f2121c221057b76a989764ed55db5869a5b3b Mon Sep 17 00:00:00 2001 From: Taye Adeyemi Date: Sun, 10 Jan 2016 16:12:28 +0000 Subject: [PATCH] Eventable: reduce code duplication in fire method --- src/Eventable.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/Eventable.js b/src/Eventable.js index 91f778c47..b69f302d9 100644 --- a/src/Eventable.js +++ b/src/Eventable.js @@ -1,5 +1,11 @@ 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; @@ -7,12 +13,8 @@ class Eventable { 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 @@ -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); } }