Support for stopping animations that run on UI thread.

Summary:This change extends animated native module API with `stopAnimation` method that is responsible for interrupting actively running animation as a reslut of a JS call. In order for the `stopAnimation` to understand `animationId` argument I also had to add `animationId` to `startAnimation` method. As JS thread runs in parallel to the thread which executes the animation there is a chance that JS may call `stopAnimation` after the animation has finished. Because of that we are not doing any checks on the `animationId` parameter passed to `stopAnimation` in native and if the animation does not exists in the registry we ignore that call.

**Test Plan**
Run JS tests: `npm test Libraries/Animated/src/__tests__/AnimatedNative-test.js`
Run java tests: `buck test ReactAndroid/src/test/java/com/facebook/react/animated`
Closes https://github.com/facebook/react-native/pull/7058

Differential Revision: D3211906

fb-gh-sync-id: 3761509651de36a550b00d33e2a631c379d3900f
fbshipit-source-id: 3761509651de36a550b00d33e2a631c379d3900f
This commit is contained in:
Krzysztof Magiera
2016-04-22 00:01:55 -07:00
committed by Facebook Github Bot 8
parent 63adb48dc4
commit cd11738819
7 changed files with 143 additions and 13 deletions

View File

@@ -17,9 +17,10 @@ import com.facebook.react.bridge.Callback;
*/
/*package*/ abstract class AnimationDriver {
boolean mHasFinished = false;
ValueAnimatedNode mAnimatedValue;
Callback mEndCallback;
/*package*/ boolean mHasFinished = false;
/*package*/ ValueAnimatedNode mAnimatedValue;
/*package*/ Callback mEndCallback;
/*package*/ int mId;
/**
* This method gets called in the main animation loop with a frame time passed down from the

View File

@@ -212,6 +212,7 @@ public class NativeAnimatedModule extends ReactContextBaseJavaModule implements
@ReactMethod
public void startAnimatingNode(
final int animationId,
final int animatedNodeTag,
final ReadableMap animationConfig,
final Callback endCallback) {
@@ -219,6 +220,7 @@ public class NativeAnimatedModule extends ReactContextBaseJavaModule implements
@Override
public void execute(NativeAnimatedNodesManager animatedNodesManager) {
animatedNodesManager.startAnimatingNode(
animationId,
animatedNodeTag,
animationConfig,
endCallback);
@@ -226,6 +228,16 @@ public class NativeAnimatedModule extends ReactContextBaseJavaModule implements
});
}
@ReactMethod
public void stopAnimation(final int animationId) {
mOperations.add(new UIThreadOperation() {
@Override
public void execute(NativeAnimatedNodesManager animatedNodesManager) {
animatedNodesManager.stopAnimation(animationId);
}
});
}
@ReactMethod
public void connectAnimatedNodes(final int parentNodeTag, final int childNodeTag) {
mOperations.add(new UIThreadOperation() {

View File

@@ -98,6 +98,7 @@ import javax.annotation.Nullable;
}
public void startAnimatingNode(
int animationId,
int animatedNodeTag,
ReadableMap animationConfig,
Callback endCallback) {
@@ -117,11 +118,34 @@ import javax.annotation.Nullable;
} else {
throw new JSApplicationIllegalArgumentException("Unsupported animation type: " + type);
}
animation.mId = animationId;
animation.mEndCallback = endCallback;
animation.mAnimatedValue = (ValueAnimatedNode) node;
mActiveAnimations.add(animation);
}
public void stopAnimation(int animationId) {
// in most of the cases there should never be more than a few active animations running at the
// same time. Therefore it does not make much sense to create an animationId -> animation
// object map that would require additional memory just to support the use-case of stopping
// an animation
for (int i = 0; i < mActiveAnimations.size(); i++) {
AnimationDriver animation = mActiveAnimations.get(i);
if (animation.mId == animationId) {
// Invoke animation end callback with {finished: false}
WritableMap endCallbackResponse = Arguments.createMap();
endCallbackResponse.putBoolean("finished", false);
animation.mEndCallback.invoke(endCallbackResponse);
mActiveAnimations.remove(i);
return;
}
}
// Do not throw an error in the case animation could not be found. We only keep "active"
// animations in the registry and there is a chance that Animated.js will enqueue a
// stopAnimation call after the animation has ended or the call will reach native thread only
// when the animation is already over.
}
public void connectAnimatedNodes(int parentNodeTag, int childNodeTag) {
AnimatedNode parentNode = mAnimatedNodes.get(parentNodeTag);
if (parentNode == null) {