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

Commit 9a60408

Browse files
matskoNarretz
authored andcommittedJan 17, 2016
fix(ngAnimate): ensure that animate promises resolve when the document is hidden
Prior to this fix any promise/callback chained on a call to the $animate methods would only flush if and when the browser page is visible. This fix ensures that a timeout will be used instead when the document is hidden.
1 parent 09f6061 commit 9a60408

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed
 

‎src/ng/animateRunner.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ var $$AnimateAsyncRunFactoryProvider = function() {
2828
};
2929

3030
var $$AnimateRunnerFactoryProvider = function() {
31-
this.$get = ['$q', '$sniffer', '$$animateAsyncRun',
32-
function($q, $sniffer, $$animateAsyncRun) {
31+
this.$get = ['$q', '$sniffer', '$$animateAsyncRun', '$document', '$timeout',
32+
function($q, $sniffer, $$animateAsyncRun, $document, $timeout) {
3333

3434
var INITIAL_STATE = 0;
3535
var DONE_PENDING_STATE = 1;
@@ -74,8 +74,19 @@ var $$AnimateRunnerFactoryProvider = function() {
7474
function AnimateRunner(host) {
7575
this.setHost(host);
7676

77+
var rafTick = $$animateAsyncRun();
78+
var timeoutTick = function(fn) {
79+
$timeout(fn, 0, false);
80+
};
81+
7782
this._doneCallbacks = [];
78-
this._runInAnimationFrame = $$animateAsyncRun();
83+
this._tick = function(fn) {
84+
if ($document[0].hidden) {
85+
timeoutTick(fn);
86+
} else {
87+
rafTick(fn);
88+
}
89+
};
7990
this._state = 0;
8091
}
8192

@@ -148,7 +159,7 @@ var $$AnimateRunnerFactoryProvider = function() {
148159
var self = this;
149160
if (self._state === INITIAL_STATE) {
150161
self._state = DONE_PENDING_STATE;
151-
self._runInAnimationFrame(function() {
162+
self._tick(function() {
152163
self._resolve(response);
153164
});
154165
}

‎test/ng/animateRunnerSpec.js

+37
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,43 @@ describe("$$AnimateRunner", function() {
162162
expect(animationFailed).toBe(true);
163163
}));
164164

165+
it("should use timeouts to trigger async operations when the document is hidden", function() {
166+
var doc;
167+
168+
module(function($provide) {
169+
doc = jqLite({
170+
body: document.body,
171+
hidden: true
172+
});
173+
$provide.value('$document', doc);
174+
});
175+
176+
inject(function($$AnimateRunner, $rootScope, $$rAF, $timeout) {
177+
var spy = jasmine.createSpy();
178+
var runner = new $$AnimateRunner();
179+
runner.done(spy);
180+
runner.complete(true);
181+
expect(spy).not.toHaveBeenCalled();
182+
$$rAF.flush();
183+
expect(spy).not.toHaveBeenCalled();
184+
$timeout.flush();
185+
expect(spy).toHaveBeenCalled();
186+
187+
doc[0].hidden = false;
188+
189+
spy = jasmine.createSpy();
190+
runner = new $$AnimateRunner();
191+
runner.done(spy);
192+
runner.complete(true);
193+
expect(spy).not.toHaveBeenCalled();
194+
$$rAF.flush();
195+
expect(spy).toHaveBeenCalled();
196+
expect(function() {
197+
$timeout.flush();
198+
}).toThrow();
199+
});
200+
});
201+
165202
they("should expose the `finally` promise function to handle the final state when $prop",
166203
{ 'rejected': 'cancel', 'resolved': 'end' }, function(method) {
167204
inject(function($$AnimateRunner, $rootScope) {

0 commit comments

Comments
 (0)
This repository has been archived.