-
Notifications
You must be signed in to change notification settings - Fork 24k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Native Animated - Restore default values when removing props on Android #12842
Closed
janicduplessis
wants to merge
6
commits into
facebook:master
from
janicduplessis:native-animated-default-values-android
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd9abc6
Properly restore default values when removing props driven by native
janicduplessis 5fbf352
Fix NativeAnimatedNodesManager initialization race condition
janicduplessis 3657ccd
Merge branch 'master' into native-animated-default-values-android
janicduplessis 6ffbc97
Merge branch 'master' into native-animated-default-values-android
janicduplessis a5a49cb
Fix buck build
janicduplessis deeb713
Fix tests
janicduplessis File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,17 +17,20 @@ | |
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.Callback; | ||
import com.facebook.react.bridge.LifecycleEventListener; | ||
import com.facebook.react.bridge.OnBatchCompleteListener; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.bridge.WritableMap; | ||
import com.facebook.react.common.annotations.VisibleForTesting; | ||
import com.facebook.react.module.annotations.ReactModule; | ||
import com.facebook.react.modules.core.DeviceEventManagerModule; | ||
import com.facebook.react.modules.core.ReactChoreographer; | ||
import com.facebook.react.uimanager.GuardedFrameCallback; | ||
import com.facebook.react.uimanager.NativeViewHierarchyManager; | ||
import com.facebook.react.uimanager.UIBlock; | ||
import com.facebook.react.uimanager.UIManagerModule; | ||
import com.facebook.react.uimanager.UIManagerModuleListener; | ||
|
||
/** | ||
* Module that exposes interface for creating and managing animated nodes on the "native" side. | ||
|
@@ -72,19 +75,18 @@ | |
*/ | ||
@ReactModule(name = NativeAnimatedModule.NAME) | ||
public class NativeAnimatedModule extends ReactContextBaseJavaModule implements | ||
OnBatchCompleteListener, LifecycleEventListener { | ||
LifecycleEventListener, UIManagerModuleListener { | ||
|
||
protected static final String NAME = "NativeAnimatedModule"; | ||
|
||
private interface UIThreadOperation { | ||
void execute(NativeAnimatedNodesManager animatedNodesManager); | ||
} | ||
|
||
private final Object mOperationsCopyLock = new Object(); | ||
private final GuardedFrameCallback mAnimatedFrameCallback; | ||
private final ReactChoreographer mReactChoreographer; | ||
private ArrayList<UIThreadOperation> mOperations = new ArrayList<>(); | ||
private volatile @Nullable ArrayList<UIThreadOperation> mReadyOperations = null; | ||
private ArrayList<UIThreadOperation> mPreOperations = new ArrayList<>(); | ||
|
||
private @Nullable NativeAnimatedNodesManager mNodesManager; | ||
|
||
|
@@ -95,26 +97,9 @@ public NativeAnimatedModule(ReactApplicationContext reactContext) { | |
mAnimatedFrameCallback = new GuardedFrameCallback(reactContext) { | ||
@Override | ||
protected void doFrameGuarded(final long frameTimeNanos) { | ||
if (mNodesManager == null) { | ||
UIManagerModule uiManager = getReactApplicationContext() | ||
.getNativeModule(UIManagerModule.class); | ||
mNodesManager = new NativeAnimatedNodesManager(uiManager); | ||
} | ||
|
||
ArrayList<UIThreadOperation> operations; | ||
synchronized (mOperationsCopyLock) { | ||
operations = mReadyOperations; | ||
mReadyOperations = null; | ||
} | ||
|
||
if (operations != null) { | ||
for (int i = 0, size = operations.size(); i < size; i++) { | ||
operations.get(i).execute(mNodesManager); | ||
} | ||
} | ||
|
||
if (mNodesManager.hasActiveAnimations()) { | ||
mNodesManager.runUpdates(frameTimeNanos); | ||
NativeAnimatedNodesManager nodesManager = getNodesManager(); | ||
if (nodesManager.hasActiveAnimations()) { | ||
nodesManager.runUpdates(frameTimeNanos); | ||
} | ||
|
||
// TODO: Would be great to avoid adding this callback in case there are no active animations | ||
|
@@ -130,7 +115,10 @@ protected void doFrameGuarded(final long frameTimeNanos) { | |
|
||
@Override | ||
public void initialize() { | ||
getReactApplicationContext().addLifecycleEventListener(this); | ||
ReactApplicationContext reactCtx = getReactApplicationContext(); | ||
UIManagerModule uiManager = reactCtx.getNativeModule(UIManagerModule.class); | ||
reactCtx.addLifecycleEventListener(this); | ||
uiManager.addUIManagerListener(this); | ||
} | ||
|
||
@Override | ||
|
@@ -139,24 +127,32 @@ public void onHostResume() { | |
} | ||
|
||
@Override | ||
public void onBatchComplete() { | ||
// Note: The order of executing onBatchComplete handler (especially in terms of onBatchComplete | ||
// from the UIManagerModule) doesn't matter as we only enqueue operations for the UI thread to | ||
// be executed from here. Thanks to ReactChoreographer all the operations from here are going | ||
// to be executed *after* all the operations enqueued by UIManager as the callback type that we | ||
// use for ReactChoreographer (CallbackType.NATIVE_ANIMATED_MODULE) is run after callbacks that | ||
// UIManager uses. | ||
ArrayList<UIThreadOperation> operations = mOperations.isEmpty() ? null : mOperations; | ||
if (operations != null) { | ||
mOperations = new ArrayList<>(); | ||
synchronized (mOperationsCopyLock) { | ||
if (mReadyOperations == null) { | ||
mReadyOperations = operations; | ||
} else { | ||
mReadyOperations.addAll(operations); | ||
public void willDispatchViewUpdates(final UIManagerModule uiManager) { | ||
if (mOperations.isEmpty() && mPreOperations.isEmpty()) { | ||
return; | ||
} | ||
final ArrayList<UIThreadOperation> preOperations = mPreOperations; | ||
final ArrayList<UIThreadOperation> operations = mOperations; | ||
mPreOperations = new ArrayList<>(); | ||
mOperations = new ArrayList<>(); | ||
uiManager.prependUIBlock(new UIBlock() { | ||
@Override | ||
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) { | ||
NativeAnimatedNodesManager nodesManager = getNodesManager(); | ||
for (UIThreadOperation operation : preOperations) { | ||
operation.execute(nodesManager); | ||
} | ||
} | ||
} | ||
}); | ||
uiManager.addUIBlock(new UIBlock() { | ||
@Override | ||
public void execute(NativeViewHierarchyManager nativeViewHierarchyManager) { | ||
NativeAnimatedNodesManager nodesManager = getNodesManager(); | ||
for (UIThreadOperation operation : operations) { | ||
operation.execute(nodesManager); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
|
@@ -174,6 +170,15 @@ public String getName() { | |
return NAME; | ||
} | ||
|
||
private NativeAnimatedNodesManager getNodesManager() { | ||
if (mNodesManager == null) { | ||
UIManagerModule uiManager = getReactApplicationContext().getNativeModule(UIManagerModule.class); | ||
mNodesManager = new NativeAnimatedNodesManager(uiManager); | ||
} | ||
|
||
return mNodesManager; | ||
} | ||
|
||
private void clearFrameCallback() { | ||
Assertions.assertNotNull(mReactChoreographer).removeFrameCallback( | ||
ReactChoreographer.CallbackType.NATIVE_ANIMATED_MODULE, | ||
|
@@ -186,6 +191,11 @@ private void enqueueFrameCallback() { | |
mAnimatedFrameCallback); | ||
} | ||
|
||
@VisibleForTesting | ||
public void setNodesManager(NativeAnimatedNodesManager nodesManager) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if there is a better way to do this, we have to inject a mock in here but there is no way currently. I tried using |
||
mNodesManager = nodesManager; | ||
} | ||
|
||
@ReactMethod | ||
public void createAnimatedNode(final int tag, final ReadableMap config) { | ||
mOperations.add(new UIThreadOperation() { | ||
|
@@ -336,6 +346,12 @@ public void execute(NativeAnimatedNodesManager animatedNodesManager) { | |
|
||
@ReactMethod | ||
public void disconnectAnimatedNodeFromView(final int animatedNodeTag, final int viewTag) { | ||
mPreOperations.add(new UIThreadOperation() { | ||
@Override | ||
public void execute(NativeAnimatedNodesManager animatedNodesManager) { | ||
animatedNodesManager.restoreDefaultValues(animatedNodeTag, viewTag); | ||
} | ||
}); | ||
mOperations.add(new UIThreadOperation() { | ||
@Override | ||
public void execute(NativeAnimatedNodesManager animatedNodesManager) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this method used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a method native modules can override that gets called when batches are completed so it is fine to remove it since it will just call the one from the super class.