| 2206 | stopTogether?: bool, // If one is stopped, stop all. default: true |
| 2207 | } |
| 2208 | var parallel = function( |
| 2209 | animations: Array<CompositeAnimation>, |
| 2210 | config?: ?ParallelConfig, |
| 2211 | ): CompositeAnimation { |
| 2212 | var doneCount = 0; |
| 2213 | // Make sure we only call stop() at most once for each animation |
| 2214 | var hasEnded = {}; |
| 2215 | var stopTogether = !(config && config.stopTogether === false); |
| 2216 | |
| 2217 | var result = { |
| 2218 | start: function(callback?: ?EndCallback) { |
| 2219 | if (doneCount === animations.length) { |
| 2220 | callback && callback({finished: true}); |
| 2221 | return; |
| 2222 | } |
| 2223 | |
| 2224 | animations.forEach((animation, idx) => { |
| 2225 | var cb = function(endResult) { |
| 2226 | hasEnded[idx] = true; |
| 2227 | doneCount++; |
| 2228 | if (doneCount === animations.length) { |
| 2229 | doneCount = 0; |
| 2230 | callback && callback(endResult); |
| 2231 | return; |
| 2232 | } |
| 2233 | |
| 2234 | if (!endResult.finished && stopTogether) { |
| 2235 | result.stop(); |
| 2236 | } |
| 2237 | }; |
| 2238 | |
| 2239 | if (!animation) { |
| 2240 | cb({finished: true}); |
| 2241 | } else { |
| 2242 | animation.start(cb); |
| 2243 | } |
| 2244 | }); |
| 2245 | }, |
| 2246 | |
| 2247 | stop: function(): void { |
| 2248 | animations.forEach((animation, idx) => { |
| 2249 | !hasEnded[idx] && animation.stop(); |
| 2250 | hasEnded[idx] = true; |
| 2251 | }); |
| 2252 | }, |
| 2253 | |
| 2254 | reset: function(): void { |
| 2255 | animations.forEach((animation, idx) => { |
| 2256 | animation.reset(); |
| 2257 | hasEnded[idx] = false; |
| 2258 | doneCount = 0; |
| 2259 | }); |
| 2260 | }, |
| 2261 | |
| 2262 | _startNativeLoop: function() { |
| 2263 | throw new Error('Loops run using the native driver cannot contain Animated.parallel animations'); |
| 2264 | }, |
| 2265 | |