Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit a56435f

Browse files
fix($rootScope.$on) check listener existense while deregistering
Check that listener is still present in $$listeners before decrease $$listenerCount. It fixes problem with incorrect $$listenerCount after call deregistering function multiple times. Closes #9666 Closes #9667
1 parent 75082c9 commit a56435f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/ng/rootScope.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -979,8 +979,11 @@ function $RootScopeProvider(){
979979

980980
var self = this;
981981
return function() {
982-
namedListeners[indexOf(namedListeners, listener)] = null;
983-
decrementListenerCount(self, 1, name);
982+
var indexOfListener = indexOf(namedListeners, listener);
983+
if (indexOfListener !== -1) {
984+
namedListeners[indexOfListener] = null;
985+
decrementListenerCount(self, 1, name);
986+
}
984987
};
985988
},
986989

test/ng/rootScopeSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,31 @@ describe('Scope', function() {
13611361
expect(child1.$$listenerCount).toEqual({event1: 1});
13621362
expect(child2.$$listenerCount).toEqual({});
13631363
}));
1364+
1365+
1366+
it('should not decrement $$listenerCount when called second time', inject(function($rootScope) {
1367+
var child = $rootScope.$new(),
1368+
listener1Spy = jasmine.createSpy(),
1369+
listener2Spy = jasmine.createSpy();
1370+
1371+
child.$on('abc', listener1Spy);
1372+
expect($rootScope.$$listenerCount).toEqual({abc: 1});
1373+
expect(child.$$listenerCount).toEqual({abc: 1});
1374+
1375+
var deregisterEventListener = child.$on('abc', listener2Spy);
1376+
expect($rootScope.$$listenerCount).toEqual({abc: 2});
1377+
expect(child.$$listenerCount).toEqual({abc: 2});
1378+
1379+
deregisterEventListener();
1380+
1381+
expect($rootScope.$$listenerCount).toEqual({abc: 1});
1382+
expect(child.$$listenerCount).toEqual({abc: 1});
1383+
1384+
deregisterEventListener();
1385+
1386+
expect($rootScope.$$listenerCount).toEqual({abc: 1});
1387+
expect(child.$$listenerCount).toEqual({abc: 1});
1388+
}));
13641389
});
13651390
});
13661391

0 commit comments

Comments
 (0)