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

Commit 0ca17ce

Browse files
committed
wip
1 parent 0e1c40d commit 0ca17ce

File tree

4 files changed

+174
-55
lines changed

4 files changed

+174
-55
lines changed

src/ngAnimate/animateCss.js

+18-23
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
143143
var gcsLookup = new LocalCacheLookup();
144144
var gcsStaggerLookup = new LocalCacheLookup();
145145

146-
this.$get = ['$window', '$$jqLite', '$qRaf', '$timeout', '$$rAF',
147-
'$$animateRunner', '$document', '$$animateOptions', '$sniffer',
148-
function($window, $$jqLite, $qRaf, $timeout, $$rAF,
149-
$$animateRunner, $document, $$animateOptions, $sniffer) {
146+
this.$get = ['$window', '$$jqLite', '$animateRunner', '$timeout',
147+
'$document', '$$animateOptions', '$sniffer', '$$rAF',
148+
function($window, $$jqLite, $animateRunner, $timeout,
149+
$document, $$animateOptions, $sniffer, $$rAF) {
150150

151151
var parentCounter = 0;
152152
function gcsHashFn(node, extraClasses) {
@@ -196,15 +196,13 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
196196
return stagger;
197197
}
198198

199-
var cancelLastRafRequest, rafDeferred, bod = $document[0].body;
200-
function waitUntilQuiet() {
199+
var cancelLastRefRequest, bod = $document[0].body;
200+
function waitUntilQuiet(callback) {
201201
if (cancelLastRafRequest) {
202202
cancelLastRafRequest(); //cancels the request
203203
}
204-
if (!rafDeferred) {
205-
rafDeferred = $qRaf.defer();
206-
}
207204
cancelLastRafRequest = $$rAF(function() {
205+
cancelLastRequest = null;
208206
gcsLookup.flush();
209207
gcsStaggerLookup.flush();
210208

@@ -214,12 +212,8 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
214212
//ensure that the the preparation animation is properly flushed so that
215213
//the active state picks up from there. DO NOT REMOVE THIS LINE.
216214
var a = bod.offsetWidth + 1;
217-
218-
var deferred = rafDeferred;
219-
rafDeferred = null;
220-
deferred.resolve();
215+
callback();
221216
});
222-
return rafDeferred.promise;
223217
}
224218

225219
return init;
@@ -252,7 +246,7 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
252246
var animationClosed;
253247
var animationPaused;
254248
var animationCompleted;
255-
var deferred;
249+
var runner;
256250

257251
if (options.duration === 0 || (!$sniffer.animations && !$sniffer.transitions)) {
258252
close();
@@ -449,19 +443,20 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
449443
start: function() {
450444
if (animationClosed) return;
451445

452-
deferred = $qRaf.defer();
453-
waitUntilQuiet().then(function() {
446+
runner = new $animateRunner({
447+
end: endFn,
448+
cancel: cancelFn
449+
});
450+
451+
waitUntilQuiet(function() {
454452
start(deferred);
455453
});
456454

457455
// we don't have access to pause/resume the animation
458456
// since it hasn't run yet. AnimateRunner will therefore
459457
// set noop functions for resume and pause and they will
460458
// later be overridden once the animation is triggered
461-
return $$animateRunner(deferred.promise, {
462-
end: endFn,
463-
cancel: cancelFn
464-
});
459+
return runner;
465460
},
466461
end: endFn,
467462
cancel: cancelFn,
@@ -509,8 +504,8 @@ var $AnimateCssProvider = ['$animateProvider', function($animateProvider) {
509504
}
510505

511506
// if the preparation function fails then the promise is not setup
512-
if (deferred) {
513-
rejected ? deferred.reject() : deferred.resolve();
507+
if (runner) {
508+
rejected ? runner.end() : runner.cancel();
514509
}
515510
}
516511

src/ngAnimate/animateJs.js

+29-30
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
var $$AnimateJsProvider = ['$animateProvider', function($animateProvider) {
4-
this.$get = ['$injector', '$qRaf', '$$animateRunner', '$$animateOptions',
5-
function($injector, $qRaf, $$animateRunner, $$animateOptions) {
4+
this.$get = ['$injector', '$animateRunner', '$$animateOptions', '$$rAFMutex',
5+
function($injector, $animateRunner, $$animateOptions, $$rAFMutex) {
66

77
return function(element, event, classes, options) {
88
// the `classes` argument is optional and if it is not used
@@ -56,45 +56,35 @@ var $$AnimateJsProvider = ['$animateProvider', function($animateProvider) {
5656

5757
return {
5858
start: function() {
59-
var deferred = $qRaf.defer();
60-
61-
var chain;
6259
var activeAnimations = [];
60+
var chain = [];
61+
6362
if (before) {
64-
chain = before(activeAnimations);
63+
chain.push(function(fn) {
64+
before(activeAnimations, fn);
65+
});
6566
}
6667

67-
if (isPromiseLike(chain)) {
68-
chain = chain.then(applyOptions);
68+
if (chain.length) {
69+
chain.push(function(fn) {
70+
applyOptions();
71+
fn();
72+
});
6973
} else {
7074
applyOptions();
7175
}
7276

7377
if (after) {
74-
if (isPromiseLike(chain)) {
75-
chain = chain.then(function() {
76-
return after(activeAnimations);
77-
});
78-
} else {
79-
chain = after(activeAnimations);
80-
}
78+
chain.push(function(fn) {
79+
after(activeAnimations, fn);
80+
});
8181
}
8282

8383
var animationClosed = false;
8484

85-
chain = isPromiseLike(chain) ? chain : $qRaf.when(chain);
86-
chain = chain.then(
87-
function() { onComplete(); },
88-
function() { onComplete(true); });
89-
90-
function onComplete(cancelled) {
91-
animationClosed = true;
92-
applyOptions();
93-
options.$applyStyles();
94-
cancelled ? deferred.reject() : deferred.resolve();
95-
}
85+
$animateRunner.chain(chain, onComplete);
9686

97-
return $$animateRunner(deferred.promise, {
87+
var runner = new $$animateRunner({
9888
end: function() {
9989
endAnimations();
10090
},
@@ -103,6 +93,15 @@ var $$AnimateJsProvider = ['$animateProvider', function($animateProvider) {
10393
}
10494
});
10595

96+
return runner;
97+
98+
function onComplete(success) {
99+
animationClosed = true;
100+
applyOptions();
101+
options.$applyStyles();
102+
success ? runner.resolve() : runner.reject();
103+
}
104+
106105
function endAnimations(cancelled) {
107106
if (!animationClosed) {
108107
forEach(activeAnimations, function(endFn) {
@@ -163,8 +162,7 @@ var $$AnimateJsProvider = ['$animateProvider', function($animateProvider) {
163162
if (!animation) return;
164163

165164
// note that all of these animations will run in parallel
166-
operations.push(function(activeAnimations) {
167-
var defer = $qRaf.defer();
165+
operations.push(function(activeAnimations, doneFn) {
168166
var resolved = false;
169167
var doneFn = noop;
170168
var onAnimationComplete = function(rejected) {
@@ -173,6 +171,7 @@ var $$AnimateJsProvider = ['$animateProvider', function($animateProvider) {
173171
resolved = true;
174172
}
175173
};
174+
176175
doneFn = executeAnimationFn(animation, element, event, options, function(result) {
177176
var cancelled = result === false;
178177
onAnimationComplete(cancelled);
@@ -211,7 +210,7 @@ var $$AnimateJsProvider = ['$animateProvider', function($animateProvider) {
211210

212211
if (operations.length === 0) return;
213212

214-
return function(activeAnimations) {
213+
return function(activeAnimations, callback) {
215214
var promises = [];
216215
if (operations.length) {
217216
forEach(operations, function(animateFn) {

src/ngAnimate/animateRunner.js

+124-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,129 @@
11
'use strict';
22

3-
var $$AnimateRunnerFactory = [function() {
3+
var $$rAFMutexFactory = ['$$rAF', function($$rAF) {
4+
return function() {
5+
var passed = false;
6+
$$rAF(function() {
7+
passed = true;
8+
});
9+
return function(fn) {
10+
passed ? fn() : $$rAF(fn);
11+
};
12+
}
13+
}];
14+
15+
var $AnimateRunnerFactory = ['$q', '$$rAFMutex', function($q, $$rAfMutex) {
16+
var DONE_PENDING_STATE = 1;
17+
var DONE_COMPLETE_STATE = 2;
18+
19+
var AnimateRunner = function(host) {
20+
this._doneCallbacks = [];
21+
this.setHost(host);
22+
this._atleastOneRAFPassedSinceStart = $$rAFMutex();
23+
this._state = 0;
24+
};
25+
26+
AnimateRunner.chain = function(chain, callback) {
27+
var status = true;
28+
var index = 0;
29+
30+
function next() {
31+
if (index === chain.length) {
32+
callback(status);
33+
return;
34+
}
35+
36+
chain[index](function(response) {
37+
status = status && response;
38+
index++;
39+
next();
40+
});
41+
}
42+
};
43+
44+
AnimateRunner.all = function(runners, callback) {
45+
var count = 0;
46+
var status = true;
47+
forEach(runners, function(runner) {
48+
runner.done(onProgress);
49+
});
50+
51+
function onProgress(response) {
52+
status = status && response;
53+
if (++count === runners.length) {
54+
callback(status);
55+
}
56+
}
57+
};
58+
59+
AnimateRunner.prototype = {
60+
setHost: function(host) {
61+
this.host = host || {};
62+
},
63+
64+
done: function(fn) {
65+
if (this.state === DONE_COMPLETE_STATE) {
66+
fn();
67+
} else {
68+
this._doneCallbacks.push(fn);
69+
}
70+
},
71+
72+
progress: noop,
73+
74+
getPromise : function() {
75+
if (!this.promise) {
76+
var self = this;
77+
this.promise = $q(function(resolve, reject) {
78+
self.done(function(status) {
79+
status === false ? reject() : resolve();
80+
});
81+
});
82+
}
83+
return this.promise;
84+
},
85+
86+
then: function(resolveHandler, rejectHandler) {
87+
return this.getPromise().then(resolveHandler, rejectHandler);
88+
},
89+
90+
pause: function() {
91+
(this.host.pause || noop)();
92+
},
93+
94+
resume: function() {
95+
(this.host.resume || noop)();
96+
},
97+
98+
end: function() {
99+
this._complete(true);
100+
},
101+
102+
cancel: function() {
103+
this._complete(false);
104+
},
105+
106+
_complete: function(response) {
107+
if (this._state === 0) {
108+
this._state = DONE_PENDING_STATE;
109+
110+
var self = this;
111+
this._atleastOneRAFPassedSinceStart(function() {
112+
invoke(self._doneCallbacks, response);
113+
self._doneCallbacks.length = 0;
114+
self._state = DONE_COMPLETE_STATE;
115+
});
116+
}
117+
}
118+
};
119+
120+
return AnimateRunner;
121+
122+
function invoke(fns, data) {
123+
forEach(fns, function(fn) {
124+
fn(data);
125+
});
126+
}
4127
// there is a lot of variable switching going on here. The main idea
5128
// is that the runner can be "updated" later on without having to create
6129
// a new variable. The driver that is given will apply it's new methods to

src/ngAnimate/module.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
angular.module('ngAnimate', [])
1616
.directive('ngAnimateChildren', $$AnimateChildrenDirective)
1717

18-
.factory('$$animateRunner', $$AnimateRunnerFactory)
18+
.factory('$$rAFMutex', $$rAFMutexFactory)
19+
20+
.factory('$animateRunner', $AnimateRunnerFactory)
1921
.factory('$$animateOptions', $$AnimateOptionsFactory)
2022

2123
.provider('$$animateQueue', $$AnimateQueueProvider)

0 commit comments

Comments
 (0)