From 9fce6979825e78100621495686041dd9d2459bfe Mon Sep 17 00:00:00 2001 From: danzel Date: Mon, 4 Nov 2013 09:47:55 +1300 Subject: [PATCH] Work around iOS7 memory trouble by not applying the contextId performance optimization when context==this. The optimization does nothing in this case anyway. Fixes #2122 --- spec/suites/core/EventsSpec.js | 18 ++++++++++++++++++ src/core/Events.js | 4 ++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/spec/suites/core/EventsSpec.js b/spec/suites/core/EventsSpec.js index 400f4242..b16429df 100644 --- a/spec/suites/core/EventsSpec.js +++ b/spec/suites/core/EventsSpec.js @@ -190,6 +190,24 @@ describe('Events', function() { expect(spy2.called).to.be(false); }); + it('removes listeners with context == this and a stamp originally added without one', function () { + var obj = new Klass(), + spy1 = sinon.spy(), + spy2 = sinon.spy(); + + obj.addEventListener('test', spy1, obj); + L.Util.stamp(obj); + obj.addEventListener('test', spy2, obj); + + obj.removeEventListener('test', spy1, obj); + obj.removeEventListener('test', spy2, obj); + + obj.fireEvent('test'); + + expect(spy1.called).to.be(false); + expect(spy2.called).to.be(false); + }); + it('doesnt lose track of listeners when removing non existent ones', function () { var obj = new Klass(), spy = sinon.spy(), diff --git a/src/core/Events.js b/src/core/Events.js index 5c1efb10..f9cb92bd 100644 --- a/src/core/Events.js +++ b/src/core/Events.js @@ -14,7 +14,7 @@ L.Mixin.Events = { if (L.Util.invokeEach(types, this.addEventListener, this, fn, context)) { return this; } 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; // types can be a string of space-separated words @@ -27,7 +27,7 @@ L.Mixin.Events = { }; type = types[i]; - if (context) { + if (contextId) { // 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