From 29ce881e1b627e7cb12125d0bf7794a1ec2b031e Mon Sep 17 00:00:00 2001 From: Chris Garrett Date: Wed, 26 Jun 2019 10:33:37 -0700 Subject: [PATCH] [BUGFIX lts] Remove all own listeners We were previously only removing own listeners if they were functions, this PR updates us to always remove all of them. (cherry picked from commit 96a832b397360ed4acb2446bb3a42e702508f3d7) --- packages/@ember/-internals/meta/lib/meta.ts | 12 +++----- .../-internals/meta/tests/listeners_test.js | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/packages/@ember/-internals/meta/lib/meta.ts b/packages/@ember/-internals/meta/lib/meta.ts index 3d54fd98906..fdcaa7dde27 100644 --- a/packages/@ember/-internals/meta/lib/meta.ts +++ b/packages/@ember/-internals/meta/lib/meta.ts @@ -588,14 +588,10 @@ export class Meta { } as Listener); } else { let listener = listeners[i]; - // If the listener is our own function listener and we are trying to - // remove it, we want to splice it out entirely so we don't hold onto a - // reference. - if ( - kind === ListenerKind.REMOVE && - listener.kind !== ListenerKind.REMOVE && - typeof method === 'function' - ) { + + // If the listener is our own listener and we are trying to remove it, we + // want to splice it out entirely so we don't hold onto a reference. + if (kind === ListenerKind.REMOVE && listener.kind !== ListenerKind.REMOVE) { listeners.splice(i, 1); } else { // update own listener diff --git a/packages/@ember/-internals/meta/tests/listeners_test.js b/packages/@ember/-internals/meta/tests/listeners_test.js index 2e1ca9898be..e0ff87b1f5e 100644 --- a/packages/@ember/-internals/meta/tests/listeners_test.js +++ b/packages/@ember/-internals/meta/tests/listeners_test.js @@ -172,5 +172,33 @@ moduleFor( 'one reopen call after mutating parents and flattening out of order' ); } + + '@test removed listeners are removed from the underlying structure GH#1112213'(assert) { + // this is using private API to confirm the underlying data structure is properly maintained + // and should be changed to match the data structure as needed + + class Class1 {} + let class1Meta = meta(Class1.prototype); + class1Meta.addToListeners('hello', null, 'm', 0); + + let instance1 = new Class1(); + let m1 = meta(instance1); + + function listenerFunc() {} + + m1.removeFromListeners('hello', null, 'm', 0); + + m1.addToListeners('stringListener', null, 'm', 0); + m1.addToListeners('functionListener', null, listenerFunc, 0); + + m1.removeFromListeners('functionListener', null, listenerFunc, 0); + m1.removeFromListeners('stringListener', null, 'm', 0); + + assert.equal( + m1.flattenedListeners().length, + 1, + 'instance listeners correctly removed, inherited listeners remain' + ); + } } );