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

feat($rootScope): allow $evalAsync to queue operations to be run after digest #3636

Closed
wants to merge 2 commits 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
28 changes: 20 additions & 8 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ function $RootScopeProvider(){
this.$$childHead = this.$$childTail = null;
this['this'] = this.$root = this;
this.$$destroyed = false;
this.$$asyncQueue = [];
this.$$internalAsyncQueue = [];
this.$$externalAsyncQueue = [];
this.$$listeners = {};
this.$$isolateBindings = {};
}
Expand Down Expand Up @@ -166,7 +167,8 @@ function $RootScopeProvider(){
child = new Scope();
child.$root = this.$root;
// ensure that there is just one async queue per $rootScope and it's children
child.$$asyncQueue = this.$$asyncQueue;
child.$$internalAsyncQueue = this.$$internalAsyncQueue;
child.$$externalAsyncQueue = this.$$externalAsyncQueue;
} else {
Child = function() {}; // should be anonymous; This is so that when the minifier munges
// the name it does not become random set of chars. These will then show up as class
Expand Down Expand Up @@ -493,7 +495,8 @@ function $RootScopeProvider(){
$digest: function() {
var watch, value, last,
watchers,
asyncQueue = this.$$asyncQueue,
internalAsyncQueue = this.$$internalAsyncQueue,
externalAsyncQueue = this.$$externalAsyncQueue,
length,
dirty, ttl = TTL,
next, current, target = this,
Expand All @@ -506,9 +509,9 @@ function $RootScopeProvider(){
dirty = false;
current = target;

while(asyncQueue.length) {
while(internalAsyncQueue.length) {
try {
current.$eval(asyncQueue.shift());
current.$eval(internalAsyncQueue.shift());
} catch (e) {
$exceptionHandler(e);
}
Expand Down Expand Up @@ -563,9 +566,17 @@ function $RootScopeProvider(){
'{0} $digest() iterations reached. Aborting!\nWatchers fired in the last 5 iterations: {1}',
TTL, toJson(watchLog));
}
} while (dirty || asyncQueue.length);
} while (dirty || internalAsyncQueue.length);

clearPhase();

while(externalAsyncQueue.length) {
try {
target.$eval(externalAsyncQueue.shift());
} catch (e) {
$exceptionHandler(e);
}
}
},


Expand Down Expand Up @@ -678,9 +689,10 @@ function $RootScopeProvider(){
* - `string`: execute using the rules as defined in {@link guide/expression expression}.
* - `function(scope)`: execute the function with the current `scope` parameter.
*
* @param {boolean} runDigest Whether or not to skip the next digest cycle after the expr is evaluated
*/
$evalAsync: function(expr) {
this.$$asyncQueue.push(expr);
$evalAsync: function(expr, runDigest) {
(runDigest === false ? this.$$externalAsyncQueue : this.$$internalAsyncQueue).push(expr);
},

/**
Expand Down
76 changes: 73 additions & 3 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,56 @@ describe('Scope', function() {
expect(log).toEqual('parent.async;child.async;parent.$digest;child.$digest;');
}));

it('should not run another digest for an $evalAsync call when it is external', inject(function($rootScope) {
var internalWatchCount = 0;
var externalWatchCount = 0;

$rootScope.internalCount = 0;
$rootScope.externalCount = 0;

$rootScope.$evalAsync(function(scope) {
$rootScope.internalCount++;
});

$rootScope.$evalAsync(function(scope) {
$rootScope.externalCount++;
}, false);

$rootScope.$watch('internalCount', function(value) {
internalWatchCount = value;
});
$rootScope.$watch('externalCount', function(value) {
externalWatchCount = value;
});

$rootScope.$digest();

expect(internalWatchCount).toEqual(1);
expect(externalWatchCount).toEqual(0);
}));

it('should run an external evalAsync call on all child scopes when a parent scope is digested', inject(function($rootScope) {
var parent = $rootScope.$new(),
child = parent.$new(),
count = 0;

$rootScope.$evalAsync(function() {
count++;
}, false);

parent.$evalAsync(function() {
count++;
}, false);

child.$evalAsync(function() {
count++;
}, false);

expect(count).toBe(0);
$rootScope.$digest();
expect(count).toBe(3);
}));

it('should cause a $digest rerun', inject(function($rootScope) {
$rootScope.log = '';
$rootScope.value = 0;
Expand Down Expand Up @@ -701,10 +751,30 @@ describe('Scope', function() {
childScope.$evalAsync('childExpression');
isolateScope.$evalAsync('isolateExpression');

expect(childScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
expect(isolateScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
expect($rootScope.$$asyncQueue).toEqual(['rootExpression', 'childExpression', 'isolateExpression']);
expect(childScope.$$internalAsyncQueue).toBe($rootScope.$$internalAsyncQueue);
expect(isolateScope.$$internalAsyncQueue).toBe($rootScope.$$internalAsyncQueue);
expect($rootScope.$$internalAsyncQueue).toEqual(['rootExpression', 'childExpression', 'isolateExpression']);
}));

it('should include $evalAsync(fn, false) callbacks added during $apply() in the queue dispatched after the DOM is updated', function () {
inject(function ($compile, $rootScope) {
var element = $compile(
'<div><div ng-repeat="item in items">{{item}}</div></div>')($rootScope);

var executed = false;
$rootScope.items = [ 'a;', 'b;' ];
$rootScope.$apply(function () {
$rootScope.$evalAsync(function () {
executed = true;
expect(element.text()).toBe('a;b;');
}, false);
});

expect(executed).toBe(true);

dealoc(element);
});
});
});


Expand Down