Merge pull request #2149 from danzel/ios7-2122

Work around iOS7 memory trouble
This commit is contained in:
Vladimir Agafonkin 2013-11-04 13:33:54 -08:00
commit 77bd10c69d
2 changed files with 51 additions and 6 deletions

View File

@ -190,6 +190,29 @@ describe('Events', function() {
expect(spy2.called).to.be(false); expect(spy2.called).to.be(false);
}); });
it('removes listeners with context == this and a stamp originally added without one', function () {
var obj = new Klass(),
obj2 = new Klass(),
spy1 = sinon.spy(),
spy2 = sinon.spy(),
spy3 = sinon.spy();
obj.addEventListener('test', spy1, obj);
L.Util.stamp(obj);
obj.addEventListener('test', spy2, obj);
obj.addEventListener('test', spy3, obj2); // So that there is a contextId based listener, otherwise removeEventListener will do correct behaviour anyway
obj.removeEventListener('test', spy1, obj);
obj.removeEventListener('test', spy2, obj);
obj.removeEventListener('test', spy3, obj2);
obj.fireEvent('test');
expect(spy1.called).to.be(false);
expect(spy2.called).to.be(false);
expect(spy3.called).to.be(false);
});
it('doesnt lose track of listeners when removing non existent ones', function () { it('doesnt lose track of listeners when removing non existent ones', function () {
var obj = new Klass(), var obj = new Klass(),
spy = sinon.spy(), spy = sinon.spy(),
@ -212,6 +235,27 @@ describe('Events', function() {
expect(spy.called).to.be(false); expect(spy.called).to.be(false);
}); });
it('correctly removes all listeners if given no fn', function () {
var obj = new Klass(),
spy = sinon.spy(),
foo = {},
foo2 = {},
foo3 = {};
obj.addEventListener('test', spy, foo2);
obj.addEventListener('test', spy, foo3);
obj.removeEventListener('test'); // Removes both of the above listeners
expect(obj.hasEventListeners('test')).to.be(false);
//Add and remove a listener
obj.addEventListener('test', spy, foo2);
obj.removeEventListener('test', spy, foo2);
expect(obj.hasEventListeners('test')).to.be(false);
});
it('makes sure an event is not triggered if a listener is removed during dispatch',function() { it('makes sure an event is not triggered if a listener is removed during dispatch',function() {
var obj = new Klass(), var obj = new Klass(),
spy = sinon.spy(); spy = sinon.spy();

View File

@ -14,7 +14,7 @@ L.Mixin.Events = {
if (L.Util.invokeEach(types, this.addEventListener, this, fn, context)) { return this; } if (L.Util.invokeEach(types, this.addEventListener, this, fn, context)) { return this; }
var events = this[eventsKey] = this[eventsKey] || {}, var events = this[eventsKey] = this[eventsKey] || {},
contextId = context && L.stamp(context), contextId = context && context !== this && L.stamp(context),
i, len, event, type, indexKey, indexLenKey, typeIndex; i, len, event, type, indexKey, indexLenKey, typeIndex;
// types can be a string of space-separated words // types can be a string of space-separated words
@ -27,7 +27,7 @@ L.Mixin.Events = {
}; };
type = types[i]; type = types[i];
if (context) { if (contextId) {
// store listeners of a particular context in a separate hash (if it has an id) // store listeners of a particular context in a separate hash (if it has an id)
// gives a major performance boost when removing thousands of map layers // gives a major performance boost when removing thousands of map layers
@ -74,7 +74,7 @@ L.Mixin.Events = {
if (L.Util.invokeEach(types, this.removeEventListener, this, fn, context)) { return this; } if (L.Util.invokeEach(types, this.removeEventListener, this, fn, context)) { return this; }
var events = this[eventsKey], var events = this[eventsKey],
contextId = context && L.stamp(context), contextId = context && context !== this && L.stamp(context),
i, len, type, listeners, j, indexKey, indexLenKey, typeIndex, removed; i, len, type, listeners, j, indexKey, indexLenKey, typeIndex, removed;
types = L.Util.splitWords(types); types = L.Util.splitWords(types);
@ -90,9 +90,10 @@ L.Mixin.Events = {
// clear all listeners for a type if function isn't specified // clear all listeners for a type if function isn't specified
delete events[type]; delete events[type];
delete events[indexKey]; delete events[indexKey];
delete events[indexLenKey];
} else { } else {
listeners = context && typeIndex ? typeIndex[contextId] : events[type]; listeners = contextId && typeIndex ? typeIndex[contextId] : events[type];
if (listeners) { if (listeners) {
for (j = listeners.length - 1; j >= 0; j--) { for (j = listeners.length - 1; j >= 0; j--) {
@ -135,7 +136,7 @@ L.Mixin.Events = {
listeners = events[type].slice(); listeners = events[type].slice();
for (i = 0, len = listeners.length; i < len; i++) { for (i = 0, len = listeners.length; i < len; i++) {
listeners[i].action.call(listeners[i].context || this, event); listeners[i].action.call(listeners[i].context, event);
} }
} }
@ -147,7 +148,7 @@ L.Mixin.Events = {
if (listeners) { if (listeners) {
for (i = 0, len = listeners.length; i < len; i++) { for (i = 0, len = listeners.length; i < len; i++) {
listeners[i].action.call(listeners[i].context || this, event); listeners[i].action.call(listeners[i].context, event);
} }
} }
} }