Skip to content

Commit

Permalink
Cache ViewManagerDelegate on ViewManagers (#48550)
Browse files Browse the repository at this point in the history
Summary:

Cache ViewManagerDelegate on ViewManagers

changelog: [internal] internal

Differential Revision: D67957883
  • Loading branch information
mdvacca authored and facebook-github-bot committed Jan 10, 2025
1 parent 3d8e9a7 commit 9750f8f
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>

private static final String NAME = ViewManager.class.getSimpleName();

private boolean mIsDelegateLoaded = false;
private @Nullable ViewManagerDelegate<T> mDelegate = null;

/**
* For View recycling: we store a Stack of unused, dead Views. This is null by default, and when
* null signals that View Recycling is disabled. `enableViewRecycling` must be explicitly called
Expand Down Expand Up @@ -88,7 +91,7 @@ protected void setupViewRecycling() {
* @param props {@link ReactStylesDiffMap} props to update the view with
*/
public void updateProperties(@NonNull T viewToUpdate, ReactStylesDiffMap props) {
final ViewManagerDelegate<T> delegate = getDelegate();
final ViewManagerDelegate<T> delegate = getOrCreateViewManagerDelegate();
if (delegate != null) {
ViewManagerPropertyUpdater.updateProps(delegate, viewToUpdate, props);
} else {
Expand All @@ -109,11 +112,14 @@ public void updateProperties(@NonNull T viewToUpdate, ReactStylesDiffMap props)
* @return an instance of {@link ViewManagerDelegate} if the props of the view managed by this
* view manager should be set via this delegate
*/
@Nullable
protected ViewManagerDelegate<T> getDelegate() {
protected @Nullable ViewManagerDelegate<T> getDelegate() {
return null;
}

private @Nullable ViewManagerDelegate<T> getOrCreateViewManagerDelegate() {
return mIsDelegateLoaded ? mDelegate : (mDelegate = getDelegate());
}

/** Creates a view with knowledge of props and state. */
public @NonNull T createView(
int reactTag,
Expand Down Expand Up @@ -306,7 +312,7 @@ public void receiveCommand(@NonNull T root, int commandId, @Nullable ReadableArr
* @param args optional arguments for the command
*/
public void receiveCommand(@NonNull T root, String commandId, @Nullable ReadableArray args) {
final ViewManagerDelegate<T> delegate = getDelegate();
final ViewManagerDelegate<T> delegate = getOrCreateViewManagerDelegate();
if (delegate != null) {
delegate.receiveCommand(root, commandId, args);
}
Expand Down

0 comments on commit 9750f8f

Please sign in to comment.