| 2291 | type LoopAnimationConfig = { iterations: number }; |
| 2292 | |
| 2293 | var loop = function( |
| 2294 | animation: CompositeAnimation, |
| 2295 | { iterations = -1 }: LoopAnimationConfig = {}, |
| 2296 | ): CompositeAnimation { |
| 2297 | var isFinished = false; |
| 2298 | var iterationsSoFar = 0; |
| 2299 | return { |
| 2300 | start: function(callback?: ?EndCallback) { |
| 2301 | var restart = function(result: EndResult = {finished: true}): void { |
| 2302 | if (isFinished || |
| 2303 | (iterationsSoFar === iterations) || |
| 2304 | (result.finished === false)) { |
| 2305 | callback && callback(result); |
| 2306 | } else { |
| 2307 | iterationsSoFar++; |
| 2308 | animation.reset(); |
| 2309 | animation.start(restart); |
| 2310 | } |
| 2311 | }; |
| 2312 | if (!animation || iterations === 0) { |
| 2313 | callback && callback({finished: true}); |
| 2314 | } else { |
| 2315 | if (animation._isUsingNativeDriver()) { |
| 2316 | animation._startNativeLoop(iterations); |
| 2317 | } else { |
| 2318 | restart(); // Start looping recursively on the js thread |
| 2319 | } |
| 2320 | } |
| 2321 | }, |
| 2322 | |
| 2323 | stop: function(): void { |
| 2324 | isFinished = true; |
| 2325 | animation.stop(); |
| 2326 | }, |
| 2327 | |
| 2328 | reset: function(): void { |
| 2329 | iterationsSoFar = 0; |
| 2330 | isFinished = false; |
| 2331 | animation.reset(); |
| 2332 | }, |
| 2333 | |
| 2334 | _startNativeLoop: function() { |
| 2335 | throw new Error('Loops run using the native driver cannot contain Animated.loop animations'); |
| 2336 | }, |
| 2337 | |
| 2338 | _isUsingNativeDriver: function(): boolean { |
| 2339 | return animation._isUsingNativeDriver(); |
| 2340 | } |
| 2341 | }; |
| 2342 | }; |
| 2343 | |
| 2344 | type Mapping = {[key: string]: Mapping} | AnimatedValue; |
| 2345 | type EventConfig = { |