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
This commit is contained in:
Peter Bacon Darwin
2014-10-19 12:10:58 +01:00
parent 75082c975c
commit a56435f3ae
2 changed files with 30 additions and 2 deletions
+5 -2
View File
@@ -979,8 +979,11 @@ function $RootScopeProvider(){
var self = this;
return function() {
namedListeners[indexOf(namedListeners, listener)] = null;
decrementListenerCount(self, 1, name);
var indexOfListener = indexOf(namedListeners, listener);
if (indexOfListener !== -1) {
namedListeners[indexOfListener] = null;
decrementListenerCount(self, 1, name);
}
};
},
+25
View File
@@ -1361,6 +1361,31 @@ describe('Scope', function() {
expect(child1.$$listenerCount).toEqual({event1: 1});
expect(child2.$$listenerCount).toEqual({});
}));
it('should not decrement $$listenerCount when called second time', inject(function($rootScope) {
var child = $rootScope.$new(),
listener1Spy = jasmine.createSpy(),
listener2Spy = jasmine.createSpy();
child.$on('abc', listener1Spy);
expect($rootScope.$$listenerCount).toEqual({abc: 1});
expect(child.$$listenerCount).toEqual({abc: 1});
var deregisterEventListener = child.$on('abc', listener2Spy);
expect($rootScope.$$listenerCount).toEqual({abc: 2});
expect(child.$$listenerCount).toEqual({abc: 2});
deregisterEventListener();
expect($rootScope.$$listenerCount).toEqual({abc: 1});
expect(child.$$listenerCount).toEqual({abc: 1});
deregisterEventListener();
expect($rootScope.$$listenerCount).toEqual({abc: 1});
expect(child.$$listenerCount).toEqual({abc: 1});
}));
});
});