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:
Dmytro Yarmak
2014-10-17 16:31:59 +03:00
committed by Peter Bacon Darwin
parent ed3f799b5c
commit bf9e7bfb5a
2 changed files with 30 additions and 2 deletions

View File

@@ -1103,8 +1103,11 @@ function $RootScopeProvider(){
var self = this;
return function() {
namedListeners[namedListeners.indexOf(listener)] = null;
decrementListenerCount(self, 1, name);
var indexOfListener = namedListeners.indexOf(listener);
if (indexOfListener !== -1) {
namedListeners[indexOfListener] = null;
decrementListenerCount(self, 1, name);
}
};
},

View File

@@ -1638,6 +1638,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});
}));
});
});