Permalink
Browse files

added onComplete callback to animation config

Summary:
This fix provides possibility to subscribe to a child animation lifecycle. You'll be able to observe every single animation:
```
Animated.sequence([
            Animated.timing(
                this.state.scale,
                {
                    toValue: 0,
                    duration: 300,
                    onComplete: () => this.setState({someProp: 'new value'})
                }
            ),
            Animated.timing(
                this.state.scale,
                {
                    toValue: 1,
                    duration: 300
                }
            ),
        ]).start();
```
`state.someProp`, will updated with `'new value'` when the first animation will be completed.
Closes #8494

Reviewed By: javache

Differential Revision: D3735322

Pulled By: foghina

fbshipit-source-id: fb69a4b993f7ab6a16da4fdd670e6c0b11c93517
  • Loading branch information...
1 parent 4ad01be commit 26e8ae74b6fdd5a3481edb64185765ffaf06753f vaukalak committed with Facebook Github Bot 4 Aug 18, 2016
Showing with 14 additions and 0 deletions.
  1. +14 −0 Libraries/Animated/src/AnimatedImplementation.js
@@ -74,6 +74,7 @@ class Animated {
type AnimationConfig = {
isInteraction?: bool,
useNativeDriver?: bool,
+ onComplete?: ?EndCallback,
};
// Important note: start() and stop() will only be called at most once.
@@ -1676,6 +1677,16 @@ var modulo = function(
return new AnimatedModulo(a, modulus);
};
+const _combineCallbacks = function(callback: ?EndCallback, config : AnimationConfig) {
+ if (callback && config.onComplete) {
+ return (...args) => {
+ config.onComplete && config.onComplete(...args);
+ callback && callback(...args);
+ };
+ } else {
+ return callback || config.onComplete;
+ }
+};
var maybeVectorAnim = function(
value: AnimatedValue | AnimatedValueXY,
@@ -1707,6 +1718,7 @@ var spring = function(
): CompositeAnimation {
return maybeVectorAnim(value, config, spring) || {
start: function(callback?: ?EndCallback): void {
+ callback = _combineCallbacks(callback, config);
var singleValue: any = value;
var singleConfig: any = config;
singleValue.stopTracking();
@@ -1735,6 +1747,7 @@ var timing = function(
): CompositeAnimation {
return maybeVectorAnim(value, config, timing) || {
start: function(callback?: ?EndCallback): void {
+ callback = _combineCallbacks(callback, config);
var singleValue: any = value;
var singleConfig: any = config;
singleValue.stopTracking();
@@ -1763,6 +1776,7 @@ var decay = function(
): CompositeAnimation {
return maybeVectorAnim(value, config, decay) || {
start: function(callback?: ?EndCallback): void {
+ callback = _combineCallbacks(callback, config);
var singleValue: any = value;
var singleConfig: any = config;
singleValue.stopTracking();

0 comments on commit 26e8ae7

Please sign in to comment.