Skip to content

Commit

Permalink
Merge pull request #6 from PerryRylance/Perry
Browse files Browse the repository at this point in the history
1.0.2
  • Loading branch information
PerryRylance authored Nov 7, 2020
2 parents df9d00f + b12010b commit 6600640
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
26 changes: 20 additions & 6 deletions event-dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class EventDispatcher
listener: listener,
thisObject: (thisObject ? thisObject : this),
useCapture: (useCapture ? true : false)
};
};

target.push(obj);

Expand Down Expand Up @@ -151,11 +151,19 @@ class EventDispatcher
event[name] = src[name];
}
}

function getParent(subject)
{
if("eventDispatcherParent" in subject)
return subject.eventDispatcherParent;

return subject.parent;
}

event.target = this;

var path = [];
for(var obj = this.parent; obj != null; obj = obj.parent)
for(var obj = getParent(this); obj != null; obj = getParent(obj))
path.unshift(obj);

event.phase = Event.CAPTURING_PHASE;
Expand All @@ -174,7 +182,7 @@ class EventDispatcher

// Native DOM event
var topMostElement = this.element;
for(var obj = this.parent; obj != null; obj = obj.parent)
for(var obj = getParent(this); obj != null; obj = getParent(obj))
{
if(obj.element)
topMostElement = obj.element;
Expand Down Expand Up @@ -263,14 +271,20 @@ class EventDispatcher
if(!(arr = this._listenersByType[event.type]))
return;

for(var i = 0; i < arr.length; i++)
// Create a copy of the array, in case any listeners are unbound during iteration
var chain = arr.slice();

for(var i = 0; i < chain.length; i++)
{
obj = arr[i];
obj = chain[i];

if(arr.indexOf(obj) == -1)
continue; // Listener has been unbound

if(event.phase == Event.CAPTURING_PHASE && !obj.useCapture)
continue;

obj.listener.call(arr[i].thisObject, event);
obj.listener.call(chain[i].thisObject, event);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@perry-rylance/event-dispatcher",
"version": "1.0.1",
"version": "1.0.2",
"description": "A lightweight ES6 event dispatcher which supports bubbling, capture phases and bubbling to the DOM tree.",
"main": "event-dispatcher.js",
"type": "module",
Expand Down

0 comments on commit 6600640

Please sign in to comment.