clean up NativeAnimatedModule

Reviewed By: achen1

Differential Revision: D4883111

fbshipit-source-id: 63873d46db8d2736672a6d102e86dabfbf4f4610
This commit is contained in:
Aaron Chiu 2017-04-14 16:16:16 -07:00 committed by Facebook Github Bot
parent 346fe33077
commit 1c440dbcae
1 changed files with 40 additions and 45 deletions

View File

@ -13,7 +13,6 @@ import javax.annotation.Nullable;
import java.util.ArrayList; import java.util.ArrayList;
import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.Callback;
@ -82,33 +81,25 @@ public class NativeAnimatedModule extends ReactContextBaseJavaModule implements
} }
private final Object mOperationsCopyLock = new Object(); private final Object mOperationsCopyLock = new Object();
private @Nullable GuardedFrameCallback mAnimatedFrameCallback; private final GuardedFrameCallback mAnimatedFrameCallback;
private @Nullable ReactChoreographer mReactChoreographer; private final ReactChoreographer mReactChoreographer;
private ArrayList<UIThreadOperation> mOperations = new ArrayList<>(); private ArrayList<UIThreadOperation> mOperations = new ArrayList<>();
private volatile @Nullable ArrayList<UIThreadOperation> mReadyOperations = null; private volatile @Nullable ArrayList<UIThreadOperation> mReadyOperations = null;
private @Nullable NativeAnimatedNodesManager mNodesManager;
public NativeAnimatedModule(ReactApplicationContext reactContext) { public NativeAnimatedModule(ReactApplicationContext reactContext) {
super(reactContext); super(reactContext);
}
@Override
public void initialize() {
getReactApplicationContext().addLifecycleEventListener(this);
}
@Override
public void onHostResume() {
if (mReactChoreographer == null) {
// Safe to acquire choreographer here, as onHostResume() is invoked from UI thread.
mReactChoreographer = ReactChoreographer.getInstance(); mReactChoreographer = ReactChoreographer.getInstance();
mAnimatedFrameCallback = new GuardedFrameCallback(reactContext) {
ReactApplicationContext reactCtx = getReactApplicationContext();
UIManagerModule uiManager = reactCtx.getNativeModule(UIManagerModule.class);
final NativeAnimatedNodesManager nodesManager = new NativeAnimatedNodesManager(uiManager);
mAnimatedFrameCallback = new GuardedFrameCallback(reactCtx) {
@Override @Override
protected void doFrameGuarded(final long frameTimeNanos) { protected void doFrameGuarded(final long frameTimeNanos) {
if (mNodesManager == null) {
UIManagerModule uiManager = getReactApplicationContext()
.getNativeModule(UIManagerModule.class);
mNodesManager = new NativeAnimatedNodesManager(uiManager);
}
ArrayList<UIThreadOperation> operations; ArrayList<UIThreadOperation> operations;
synchronized (mOperationsCopyLock) { synchronized (mOperationsCopyLock) {
@ -118,12 +109,12 @@ public class NativeAnimatedModule extends ReactContextBaseJavaModule implements
if (operations != null) { if (operations != null) {
for (int i = 0, size = operations.size(); i < size; i++) { for (int i = 0, size = operations.size(); i < size; i++) {
operations.get(i).execute(nodesManager); operations.get(i).execute(mNodesManager);
} }
} }
if (nodesManager.hasActiveAnimations()) { if (mNodesManager.hasActiveAnimations()) {
nodesManager.runUpdates(frameTimeNanos); mNodesManager.runUpdates(frameTimeNanos);
} }
// TODO: Would be great to avoid adding this callback in case there are no active animations // TODO: Would be great to avoid adding this callback in case there are no active animations
@ -136,6 +127,14 @@ public class NativeAnimatedModule extends ReactContextBaseJavaModule implements
} }
}; };
} }
@Override
public void initialize() {
getReactApplicationContext().addLifecycleEventListener(this);
}
@Override
public void onHostResume() {
enqueueFrameCallback(); enqueueFrameCallback();
} }
@ -162,10 +161,6 @@ public class NativeAnimatedModule extends ReactContextBaseJavaModule implements
@Override @Override
public void onHostPause() { public void onHostPause() {
if (mReactChoreographer == null) {
FLog.e(NAME, "Called NativeAnimated.onHostPause() with a null ReactChoreographer.");
return;
}
clearFrameCallback(); clearFrameCallback();
} }