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

fix($rootScope): correctly propagate async changes after local $digest() #15501

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
8 changes: 7 additions & 1 deletion src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,10 @@ function $RootScopeProvider() {
}
asyncQueue.length = 0;

if (this === $rootScope) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be moved out of the while dirty loop since it's not necessary for it to be there

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to put this as close as possible to the point where we were certain that the asyncQueue has been fully processed. In 99.9% of the cases it won't make any difference if this is moved out of the look, so I am fine moving it.

hasRootScopeDigestRun = true;
}

traverseScopesLoop:
do { // "traverse the scopes" loop
if ((watchers = current.$$watchers)) {
Expand Down Expand Up @@ -1014,8 +1018,9 @@ function $RootScopeProvider() {
// if we are outside of an $digest loop and this is the first time we are scheduling async
// task also schedule async auto-flush
if (!$rootScope.$$phase && !asyncQueue.length) {
hasRootScopeDigestRun = false;
$browser.defer(function() {
if (asyncQueue.length) {
if (!hasRootScopeDigestRun || asyncQueue.length) {
$rootScope.$digest();
}
});
Expand Down Expand Up @@ -1333,6 +1338,7 @@ function $RootScopeProvider() {
var postDigestQueue = $rootScope.$$postDigestQueue = [];
var applyAsyncQueue = $rootScope.$$applyAsyncQueue = [];

var hasRootScopeDigestRun = false;
var postDigestQueuePosition = 0;

return $rootScope;
Expand Down
20 changes: 20 additions & 0 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,26 @@ describe('Scope', function() {
$browser.defer.flush(100000);
expect(log).toEqual(['eval-ed 1!', 'eval-ed 2!']);
});


it('should not have execution affected by a local `$digest` call', function() {
var scope1 = $rootScope.$new();
var scope2 = $rootScope.$new();

scope1.$watch('value', function(value) {
scope1.result = value;
});

scope1.$evalAsync(function() {
scope1.value = 'bar';
});

scope2.$digest();

$browser.defer.flush(0);

expect(scope1.result).toBe('bar');
});
});
});

Expand Down