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

refactor($rootScope): asyncQueue is pre-$parsed, no need to call $eval #15682

Closed
wants to merge 1 commit 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
7 changes: 4 additions & 3 deletions src/ng/rootScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,12 +786,13 @@ function $RootScopeProvider() {
current = target;

// It's safe for asyncQueuePosition to be a local variable here because this loop can't
// be reentered recursively. Calling $digest from a function passed to $applyAsync would
// be reentered recursively. Calling $digest from a function passed to $evalAsync would
// lead to a '$digest already in progress' error.
for (var asyncQueuePosition = 0; asyncQueuePosition < asyncQueue.length; asyncQueuePosition++) {
try {
asyncTask = asyncQueue[asyncQueuePosition];
asyncTask.scope.$eval(asyncTask.expression, asyncTask.locals);
fn = asyncTask.fn;
Copy link
Member

Choose a reason for hiding this comment

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

As @jbedard correctly pointed out, it is a good idea to have some test about the context (or lack thereof), to guard against future "optimizations".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

fn(asyncTask.scope, asyncTask.locals);
} catch (e) {
$exceptionHandler(e);
}
Expand Down Expand Up @@ -1025,7 +1026,7 @@ function $RootScopeProvider() {
});
}

asyncQueue.push({scope: this, expression: $parse(expr), locals: locals});
asyncQueue.push({scope: this, fn: $parse(expr), locals: locals});
},

$$postDigest: function(fn) {
Expand Down
14 changes: 11 additions & 3 deletions test/ng/rootScopeSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1444,9 +1444,9 @@ describe('Scope', function() {
expect(childScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
expect(isolateScope.$$asyncQueue).toBeUndefined();
expect($rootScope.$$asyncQueue).toEqual([
{scope: $rootScope, expression: $parse('rootExpression'), locals: undefined},
{scope: childScope, expression: $parse('childExpression'), locals: undefined},
{scope: isolateScope, expression: $parse('isolateExpression'), locals: undefined}
{scope: $rootScope, fn: $parse('rootExpression'), locals: undefined},
{scope: childScope, fn: $parse('childExpression'), locals: undefined},
{scope: isolateScope, fn: $parse('isolateExpression'), locals: undefined}
]);
}));

Expand Down Expand Up @@ -1499,6 +1499,14 @@ describe('Scope', function() {
expect(log).toEqual(['eval-ed 1!', 'eval-ed 2!']);
});
});

it('should not pass anything as `this` to scheduled functions', inject(function($rootScope) {
var this1 = {};
var this2 = (function() { return this; })();
$rootScope.$evalAsync(function() { this1 = this; });
$rootScope.$digest();
expect(this1).toEqual(this2);
}));
});


Expand Down