Skip to content

Commit

Permalink
feat(Scope): async auto-flush $evalAsync queue when outside of $digest
Browse files Browse the repository at this point in the history
This change causes a new $digest to be scheduled in the next tick if
a task was was sent to the $evalAsync queue from outside of a $digest
or an $apply.

While this mode of operation is not common for most of the user code,
this change means that $q promises that utilze $evalAsync queue to
guarantee asynchronicity of promise apis will now also resolve outside
of a $digest, which turned out to be a big pain point for some developers.

The implementation ensures that we don't do more work than needed and
that we coalese as much work as possible into a single $digest.

The use of $browser instead of setTimeout ensures that we can mock out
and control the scheduling of "auto-flush", which should in theory
allow all of the existing code and tests to work without negative
side-effects.

Closes angular#3539
Closes angular#2438
  • Loading branch information
IgorMinar committed Aug 25, 2013
1 parent 42af8ea commit 9dada81
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/ng/rootScope.js
Expand Up @@ -69,8 +69,8 @@ function $RootScopeProvider(){
return TTL;
};

this.$get = ['$injector', '$exceptionHandler', '$parse',
function( $injector, $exceptionHandler, $parse) {
this.$get = ['$injector', '$exceptionHandler', '$parse', '$browser',
function( $injector, $exceptionHandler, $parse, $browser) {

/**
* @ngdoc function
Expand Down Expand Up @@ -680,6 +680,16 @@ function $RootScopeProvider(){
*
*/
$evalAsync: function(expr) {
// 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 && !$rootScope.$$asyncQueue.length) {
$browser.defer(function() {
if ($rootScope.$$asyncQueue.length) {
$rootScope.$digest();
}
});
}

this.$$asyncQueue.push(expr);
},

Expand Down
2 changes: 1 addition & 1 deletion src/ng/timeout.js
Expand Up @@ -36,7 +36,7 @@ function $TimeoutProvider() {
var deferred = $q.defer(),
promise = deferred.promise,
skipApply = (isDefined(invokeApply) && !invokeApply),
timeoutId, cleanup;
timeoutId;

timeoutId = $browser.defer(function() {
try {
Expand Down
51 changes: 51 additions & 0 deletions test/ng/rootScopeSpec.js
Expand Up @@ -705,6 +705,57 @@ describe('Scope', function() {
expect(isolateScope.$$asyncQueue).toBe($rootScope.$$asyncQueue);
expect($rootScope.$$asyncQueue).toEqual(['rootExpression', 'childExpression', 'isolateExpression']);
}));


describe('auto-flushing when queueing outside of an $apply', function() {
var log, $rootScope, $browser;

beforeEach(inject(function(_log_, _$rootScope_, _$browser_) {
log = _log_;
$rootScope = _$rootScope_;
$browser = _$browser_;
}));


it('should auto-flush the queue asynchronously and trigger digest', function() {
$rootScope.$evalAsync(log.fn('eval-ed!'));
$rootScope.$watch(log.fn('digesting'));
expect(log).toEqual([]);

$browser.defer.flush(0);

expect(log).toEqual(['eval-ed!', 'digesting', 'digesting']);
});


it('should not trigger digest asynchronously if the queue is empty in the next tick', function() {
$rootScope.$evalAsync(log.fn('eval-ed!'));
$rootScope.$watch(log.fn('digesting'));
expect(log).toEqual([]);

$rootScope.$digest();

expect(log).toEqual(['eval-ed!', 'digesting', 'digesting']);
log.reset();

$browser.defer.flush(0);

expect(log).toEqual([]);
});


it('should not schedule more than one auto-flush task', function() {
$rootScope.$evalAsync(log.fn('eval-ed 1!'));
$rootScope.$evalAsync(log.fn('eval-ed 2!'));

$browser.defer.flush(0);
expect(log).toEqual(['eval-ed 1!', 'eval-ed 2!']);

expect(function() {
$browser.defer.flush(0);
}).toThrow('No deferred tasks with delay up to 0ms to be flushed!');
});
});
});


Expand Down

0 comments on commit 9dada81

Please sign in to comment.