Incorrect behavior of deregistration function returned from $rootScope.$on when called twice #9666
Description
Hello,
I've notice that when you call deregistration function more than one time it decrease $$listenerCount each time.
https://github.com/angular/angular.js/blob/master/src/ng/rootScope.js#L1105-L1108
After $$listenerCount becomes zero $breadcast starts ignore this event and It cause that other listeners for the same event will not be called.
https://github.com/angular/angular.js/blob/master/src/ng/rootScope.js#L1219
Reproducing
Here is plunker and steps to reproduce:
http://plnkr.co/edit/bHtWlu?p=preview
We have page with two handlers that bound to "sampleEvent", 3 buttons that broadcasts and unbinds handlers, block with displayed $$listenerCount value.
- sampleEvent count equal 2
- Click "Emit simple event"
- Two alerts should appear
- Click "Unregister first handler"
- sampleEvent count equal 1
- Click "Emit simple event"
- One alert should appear
- Click "Unregister first handler" second time
Actual result:
There is no sampleEvent count and second handler is not triggered after broadcast 'sampleEvent'
Expected result:
sampleEvent should still be 1 and second handler should be triggered after broadcast 'sampleEvent'
I'm going to create pull request with fix for this bug after few minutes.