From 9b1aff905b638aa274a5fc8f88662df446d374bd Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Fri, 16 Mar 2012 09:41:00 -0700 Subject: [PATCH] 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 --- src/service/scope.js | 3 ++- test/service/scopeSpec.js | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/service/scope.js b/src/service/scope.js index ef1b7b937f94..b7ee61d619a3 100644 --- a/src/service/scope.js +++ b/src/service/scope.js @@ -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; diff --git a/test/service/scopeSpec.js b/test/service/scopeSpec.js index 95b028fe7691..972584cf8604 100644 --- a/test/service/scopeSpec.js +++ b/test/service/scopeSpec.js @@ -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'); + })); });