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

Commit

Permalink
feat(scope): throw exception when recursive $apply
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar committed Jan 6, 2012
1 parent acb4338 commit 0bf6110
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 22 deletions.
32 changes: 21 additions & 11 deletions src/service/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
*/
function $RootScopeProvider(){
this.$get = ['$injector', '$exceptionHandler', '$parse',
function( $injector, $exceptionHandler, $parse){
function( $injector, $exceptionHandler, $parse) {

/**
* @ngdoc function
* @name angular.module.ng.$rootScope.Scope
Expand Down Expand Up @@ -152,8 +153,7 @@ function $RootScopeProvider(){
child.$parent = this;
child.$id = nextUid();
child.$$asyncQueue = [];
child.$$phase = child.$$watchers =
child.$$nextSibling = child.$$childHead = child.$$childTail = null;
child.$$watchers = child.$$nextSibling = child.$$childHead = child.$$childTail = null;
child.$$prevSibling = this.$$childTail;
if (this.$$childHead) {
this.$$childTail.$$nextSibling = child;
Expand Down Expand Up @@ -326,15 +326,12 @@ function $RootScopeProvider(){
watchLog = [],
logIdx, logMsg;

if (target.$$phase) {
throw Error(target.$$phase + ' already in progress');
}
do {
flagPhase(target, '$digest');

do {
dirty = false;
current = target;
do {
current.$$phase = '$digest';
asyncQueue = current.$$asyncQueue;
while(asyncQueue.length) {
try {
Expand All @@ -356,7 +353,7 @@ function $RootScopeProvider(){
watch.last = copy(value);
watch.fn(current, value, ((last === initWatchVal) ? value : last));
if (ttl < 5) {
logIdx = 4-ttl;
logIdx = 4 - ttl;
if (!watchLog[logIdx]) watchLog[logIdx] = [];
logMsg = (isFunction(watch.exp))
? 'fn: ' + (watch.exp.name || watch.exp.toString())
Expand All @@ -371,8 +368,6 @@ function $RootScopeProvider(){
}
}

current.$$phase = null;

// Insanity Warning: scope depth-first traversal
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $broadcast
Expand All @@ -388,6 +383,8 @@ function $RootScopeProvider(){
'Watchers fired in the last 5 iterations: ' + toJson(watchLog));
}
} while (dirty || asyncQueue.length);

this.$root.$$phase = null;
},

/**
Expand Down Expand Up @@ -524,10 +521,12 @@ function $RootScopeProvider(){
*/
$apply: function(expr) {
try {
flagPhase(this, '$apply');
return this.$eval(expr);
} catch (e) {
$exceptionHandler(e);
} finally {
this.$root.$$phase = null;
this.$root.$digest();
}
},
Expand Down Expand Up @@ -671,6 +670,17 @@ function $RootScopeProvider(){
}
};


function flagPhase(scope, phase) {
var root = scope.$root;

if (root.$$phase) {
throw Error(root.$$phase + ' already in progress');
}

root.$$phase = phase;
}

return new Scope();

function compileToFn(exp, name) {
Expand Down
77 changes: 66 additions & 11 deletions test/service/scopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

describe('Scope', function() {

beforeEach(inject(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}));


describe('$root', function() {
it('should point to itself', inject(function($rootScope) {
expect($rootScope.$root).toEqual($rootScope);
Expand Down Expand Up @@ -122,7 +117,9 @@ describe('Scope', function() {
}));


it('should delegate exceptions', inject(function($rootScope, $exceptionHandler, $log) {
it('should delegate exceptions', inject(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}, function($rootScope, $exceptionHandler, $log) {
$rootScope.$watch('a', function() {throw new Error('abc');});
$rootScope.a = 1;
$rootScope.$digest();
Expand Down Expand Up @@ -227,7 +224,7 @@ describe('Scope', function() {
}));


it('should prevent infinite recurcion and print print watcher function name or body',
it('should prevent infinite recursion and print print watcher function name or body',
inject(function($rootScope) {
$rootScope.$watch(function watcherA() {return $rootScope.a;}, function(self) {self.b++;});
$rootScope.$watch(function() {return $rootScope.b;}, function(self) {self.a++;});
Expand Down Expand Up @@ -277,7 +274,7 @@ describe('Scope', function() {
}));


it('should prevent recursion', inject(function($rootScope) {
it('should prevent $digest recursion', inject(function($rootScope) {
var callCount = 0;
$rootScope.$watch('name', function() {
expect(function() {
Expand Down Expand Up @@ -462,7 +459,9 @@ describe('Scope', function() {
}));


it('should catch exceptions', inject(function($rootScope, $exceptionHandler, $log) {
it('should catch exceptions', inject(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}, function($rootScope, $exceptionHandler, $log) {
var log = '';
var child = $rootScope.$new();
$rootScope.$watch('a', function(scope, a) { log += '1'; });
Expand All @@ -476,7 +475,9 @@ describe('Scope', function() {

describe('exceptions', function() {
var log;
beforeEach(inject(function($rootScope) {
beforeEach(inject(function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}, function($rootScope) {
log = '';
$rootScope.$watch(function() { log += '$digest;'; });
$rootScope.$digest();
Expand All @@ -502,6 +503,57 @@ describe('Scope', function() {
expect($exceptionHandler.errors).toEqual([error]);
}));
});


describe('recursive $apply protection', function() {
it('should throw an exception if $apply is called while an $apply is in progress', inject(
function($rootScope) {
expect(function() {
$rootScope.$apply(function() {
$rootScope.$apply();
});
}).toThrow('$apply already in progress');
}));


it('should throw an exception if $apply is called while flushing evalAsync queue', inject(
function($rootScope) {
expect(function() {
$rootScope.$apply(function() {
$rootScope.$evalAsync(function() {
$rootScope.$apply();
});
});
}).toThrow('$digest already in progress');
}));


it('should throw an exception if $apply is called while a watch is being initialized', inject(
function($rootScope) {
var childScope1 = $rootScope.$new();
childScope1.$watch('x', function() {
childScope1.$apply();
});
expect(function() { childScope1.$apply(); }).toThrow('$digest already in progress');
}));


it('should thrown an exception if $apply in called from a watch fn (after init)', inject(
function($rootScope) {
var childScope2 = $rootScope.$new();
childScope2.$apply(function() {
childScope2.$watch('x', function(scope, newVal, oldVal) {
if (newVal !== oldVal) {
childScope2.$apply();
}
});
});

expect(function() { childScope2.$apply(function() {
childScope2.x = 'something';
}); }).toThrow('$digest already in progress');
}));
});
});


Expand Down Expand Up @@ -561,7 +613,10 @@ describe('Scope', function() {
log += event.currentScope.id + '>';
}

beforeEach(inject(function($rootScope) {
beforeEach(inject(
function($exceptionHandlerProvider) {
$exceptionHandlerProvider.mode('log');
}, function($rootScope) {
log = '';
child = $rootScope.$new();
grandChild = child.$new();
Expand Down

0 comments on commit 0bf6110

Please sign in to comment.