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

Commit

Permalink
feat(scope): broadcast $destroy event on scope destruction
Browse files Browse the repository at this point in the history
perf testing shows that in chrome this change adds 5-15% overhead
when destroying 10k nested scopes where each scope has a $destroy listener
  • Loading branch information
IgorMinar committed Mar 16, 2012
1 parent 252d454 commit 9b1aff9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/service/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions test/service/scopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
}));
});


Expand Down

0 comments on commit 9b1aff9

Please sign in to comment.