Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
Perf improvement of new event handlig code.
Browse files Browse the repository at this point in the history
  • Loading branch information
orangemug committed Oct 4, 2016
1 parent 0a805ea commit 74700f9
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function Delegate(root) {

/** @type function() */
this.handle = Delegate.prototype.handle.bind(this);

// Cache of event listeners removed during an event cycle
this._removedListeners = [];
}

/**
Expand Down Expand Up @@ -239,6 +242,7 @@ Delegate.prototype.off = function(eventType, selector, handler, useCapture) {
listener = listenerList[i];

if ((!selector || selector === listener.selector) && (!handler || handler === listener.handler)) {
this._removedListeners.push(listener);
listenerList.splice(i, 1);
}
}
Expand Down Expand Up @@ -346,9 +350,11 @@ Delegate.prototype.handle = function(event) {
target = target.parentElement;
}

var ret;

for(i=0; i<toFire.length; i++) {
// Has it been removed during while the event function was fired
if(listenerList.indexOf(toFire[i][2]) < 0) {
if(this._removedListeners.indexOf(toFire[i][2]) > -1) {
continue;
}
returned = this.fire.apply(this, toFire[i]);
Expand All @@ -359,9 +365,12 @@ Delegate.prototype.handle = function(event) {
if (returned === false) {
toFire[i][0][EVENTIGNORE] = true;
toFire[i][0].preventDefault();
return false;
ret = false;
break;
}
}

return ret;
};

/**
Expand Down

0 comments on commit 74700f9

Please sign in to comment.