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

fix($rootScope.$on) check listener existense while deregistering #9667

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
},

Expand Down
25 changes: 25 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}));
});
});

Expand Down