Skip to content

Commit

Permalink
added onComplete callback to animation config
Browse files Browse the repository at this point in the history
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
vaukalak authored and Facebook Github Bot 4 committed Aug 18, 2016
1 parent 4ad01be commit 26e8ae7
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Libraries/Animated/src/AnimatedImplementation.js
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 26e8ae7

Please sign in to comment.