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

fix($rootScope): deallocate $$listeners array when empty #16277

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
2 changes: 1 addition & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,6 @@ function $RootScopeProvider() {
// Disable listeners, watchers and apply/digest methods
this.$destroy = this.$digest = this.$apply = this.$evalAsync = this.$applyAsync = noop;
this.$on = this.$watch = this.$watchGroup = function() { return noop; };
this.$$listeners = {};

// Disconnect the next sibling to prevent `cleanUpScope` destroying those too
this.$$nextSibling = null;
Expand Down Expand Up @@ -1364,6 +1363,7 @@ function $RootScopeProvider() {

if (current.$$listenerCount[name] === 0) {
delete current.$$listenerCount[name];
delete current.$$listeners[name];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue brought up is that $$listeners and $$listenerCount often do not have the same lifetime. $$listenerCount will reach 0 only when this scope and all child scopes have no listeners. I think that might convince me that this PR is no good as-is. But I still thinking cleaning up $$listeners when empty might be a decent idea. TBD...

}
} while ((current = current.$parent));
}
Expand Down
20 changes: 20 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,26 @@ describe('Scope', function() {
expect(secondListener).not.toHaveBeenCalled();
});
});


it('should deallocate the $$listenerCount entry when reaching 0', inject(function($rootScope) {
var child = $rootScope.$new();

child.$on('abc', noop)();

expect('abc' in $rootScope.$$listenerCount).toBe(false);
expect('abc' in child.$$listenerCount).toBe(false);
}));


it('should deallocate the $$listeners entry when empty', inject(function($rootScope) {
var child = $rootScope.$new();

child.$on('abc', noop)();

expect('abc' in $rootScope.$$listeners).toBe(false);
expect('abc' in child.$$listeners).toBe(false);
}));
});
});

Expand Down