Just store array of listeners, count is no longer needed. (#4842)

* Just store array of listeners, count is no longer needed.

* Remove unused code
This commit is contained in:
Per Liedman 2016-08-25 17:23:58 +02:00 committed by Vladimir Agafonkin
parent b00f6e0a97
commit 017d29c4eb
2 changed files with 11 additions and 24 deletions

View File

@ -377,11 +377,7 @@ describe('Events', function () {
obj.removeEventListener('test', removeSpy); obj.removeEventListener('test', removeSpy);
// expect(obj.listens('test', L.Util.falseFn)).to.be(false); expect(obj.listens('test')).to.be(false);
// Remove the expect below and comment out the one above, once we've
// gotten rid of _events.count (which makes the expect above pass).
var listeners = obj._events['test'];
expect(listeners.count === listeners.listeners.length).to.be(true);
}); });
}); });

View File

@ -95,10 +95,7 @@ L.Evented = L.Class.extend({
/* get/init listeners for type */ /* get/init listeners for type */
var typeListeners = this._events[type]; var typeListeners = this._events[type];
if (!typeListeners) { if (!typeListeners) {
typeListeners = { typeListeners = [];
listeners: [],
count: 0
};
this._events[type] = typeListeners; this._events[type] = typeListeners;
} }
@ -107,7 +104,7 @@ L.Evented = L.Class.extend({
context = undefined; context = undefined;
} }
var newListener = {fn: fn, ctx: context}, var newListener = {fn: fn, ctx: context},
listeners = typeListeners.listeners; listeners = typeListeners;
// check if fn already there // check if fn already there
for (var i = 0, len = listeners.length; i < len; i++) { for (var i = 0, len = listeners.length; i < len; i++) {
@ -121,21 +118,18 @@ L.Evented = L.Class.extend({
}, },
_off: function (type, fn, context) { _off: function (type, fn, context) {
var typeListeners, var listeners,
listeners,
i, i,
len; len;
if (!this._events) { return; } if (!this._events) { return; }
typeListeners = this._events[type]; listeners = this._events[type];
if (!typeListeners) { if (!listeners) {
return; return;
} }
listeners = typeListeners.listeners;
if (!fn) { if (!fn) {
// Set all removed listeners to noop so they are not called if remove happens in fire // Set all removed listeners to noop so they are not called if remove happens in fire
for (i = 0, len = listeners.length; i < len; i++) { for (i = 0, len = listeners.length; i < len; i++) {
@ -146,7 +140,6 @@ L.Evented = L.Class.extend({
return; return;
} }
if (context === this) { if (context === this) {
context = undefined; context = undefined;
} }
@ -161,11 +154,10 @@ L.Evented = L.Class.extend({
// set the removed listener to noop so that's not called if remove happens in fire // set the removed listener to noop so that's not called if remove happens in fire
l.fn = L.Util.falseFn; l.fn = L.Util.falseFn;
typeListeners.count--;
if (this._firingCount) { if (this._firingCount) {
/* copy array in case events are being fired */ /* copy array in case events are being fired */
typeListeners.listeners = listeners = listeners.slice(); this._events[type] = listeners = listeners.slice();
} }
listeners.splice(i, 1); listeners.splice(i, 1);
@ -185,11 +177,10 @@ L.Evented = L.Class.extend({
var event = L.Util.extend({}, data, {type: type, target: this}); var event = L.Util.extend({}, data, {type: type, target: this});
if (this._events) { if (this._events) {
var typeListeners = this._events[type]; var listeners = this._events[type];
if (typeListeners) { if (listeners) {
this._firingCount = (this._firingCount + 1) || 1; this._firingCount = (this._firingCount + 1) || 1;
var listeners = typeListeners.listeners;
for (var i = 0, len = listeners.length; i < len; i++) { for (var i = 0, len = listeners.length; i < len; i++) {
var l = listeners[i]; var l = listeners[i];
l.fn.call(l.ctx || this, event); l.fn.call(l.ctx || this, event);
@ -210,8 +201,8 @@ L.Evented = L.Class.extend({
// @method listens(type: String): Boolean // @method listens(type: String): Boolean
// Returns `true` if a particular event type has any listeners attached to it. // Returns `true` if a particular event type has any listeners attached to it.
listens: function (type, propagate) { listens: function (type, propagate) {
var typeListeners = this._events && this._events[type]; var listeners = this._events && this._events[type];
if (typeListeners && typeListeners.count) { return true; } if (listeners && listeners.length) { return true; }
if (propagate) { if (propagate) {
// also check parents for listeners if event propagates // also check parents for listeners if event propagates