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

Commit bf9e7bf

Browse files
dmytroyarmakpetebacondarwin
authored andcommitted
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 ed3f799 commit bf9e7bf

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
@@ -1103,8 +1103,11 @@ function $RootScopeProvider(){
11031103

11041104
var self = this;
11051105
return function() {
1106-
namedListeners[namedListeners.indexOf(listener)] = null;
1107-
decrementListenerCount(self, 1, name);
1106+
var indexOfListener = namedListeners.indexOf(listener);
1107+
if (indexOfListener !== -1) {
1108+
namedListeners[indexOfListener] = null;
1109+
decrementListenerCount(self, 1, name);
1110+
}
11081111
};
11091112
},
11101113

test/ng/rootScopeSpec.js

+25
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,31 @@ describe('Scope', function() {
16381638
expect(child1.$$listenerCount).toEqual({event1: 1});
16391639
expect(child2.$$listenerCount).toEqual({});
16401640
}));
1641+
1642+
1643+
it('should not decrement $$listenerCount when called second time', inject(function($rootScope) {
1644+
var child = $rootScope.$new(),
1645+
listener1Spy = jasmine.createSpy(),
1646+
listener2Spy = jasmine.createSpy();
1647+
1648+
child.$on('abc', listener1Spy);
1649+
expect($rootScope.$$listenerCount).toEqual({abc: 1});
1650+
expect(child.$$listenerCount).toEqual({abc: 1});
1651+
1652+
var deregisterEventListener = child.$on('abc', listener2Spy);
1653+
expect($rootScope.$$listenerCount).toEqual({abc: 2});
1654+
expect(child.$$listenerCount).toEqual({abc: 2});
1655+
1656+
deregisterEventListener();
1657+
1658+
expect($rootScope.$$listenerCount).toEqual({abc: 1});
1659+
expect(child.$$listenerCount).toEqual({abc: 1});
1660+
1661+
deregisterEventListener();
1662+
1663+
expect($rootScope.$$listenerCount).toEqual({abc: 1});
1664+
expect(child.$$listenerCount).toEqual({abc: 1});
1665+
}));
16411666
});
16421667
});
16431668

0 commit comments

Comments
 (0)