feat(scope): broadcast $destroy event on scope destruction

perf testing shows that in chrome this change adds 5-15% overhead
when destroying 10k nested scopes where each scope has a $destroy listener
This commit is contained in:
Igor Minar 2012-03-16 09:41:00 -07:00
parent 252d4548f9
commit 9b1aff905b
2 changed files with 14 additions and 1 deletions

View file

@ -136,7 +136,6 @@ function $RootScopeProvider(){
this.$$phase = this.$parent = this.$$watchers =
this.$$nextSibling = this.$$prevSibling =
this.$$childHead = this.$$childTail = null;
this.$destructor = noop;
this['this'] = this.$root = this;
this.$$asyncQueue = [];
this.$$listeners = {};
@ -458,6 +457,8 @@ function $RootScopeProvider(){
if (this.$root == this) return; // we can't remove the root node;
var parent = this.$parent;
this.$broadcast('$destroy');
if (parent.$$childHead == this) parent.$$childHead = this.$$nextSibling;
if (parent.$$childTail == this) parent.$$childTail = this.$$prevSibling;
if (this.$$prevSibling) this.$$prevSibling.$$nextSibling = this.$$nextSibling;

View file

@ -2,6 +2,9 @@
describe('Scope', function() {
beforeEach(module(provideLog));
describe('$root', function() {
it('should point to itself', inject(function($rootScope) {
expect($rootScope.$root).toEqual($rootScope);
@ -393,6 +396,15 @@ describe('Scope', function() {
$rootScope.$digest();
expect(log).toEqual('12');
}));
it('should broadcast the $destroy event', inject(function($rootScope, log) {
first.$on('$destroy', log.fn('first'));
first.$new().$on('$destroy', log.fn('first-child'));
first.$destroy();
expect(log).toEqual('first; first-child');
}));
});