Skip to content

Commit

Permalink
Cache ViewManagerDelegate on ViewManagers
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 9, 2025
1 parent 3e59f22 commit 80193ed
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.yoga.YogaMeasureMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.Stack;

/**
Expand All @@ -41,6 +42,8 @@ public abstract class ViewManager<T extends View, C extends ReactShadowNode>

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

private @Nullable Optional<ViewManagerDelegate<T>> mDelegate = Optional.empty();

/**
* 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 = getOrCreacteViewManagerDelegate();
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> getOrCreacteViewManagerDelegate() {
return mDelegate.orElseGet(this::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 = getOrCreacteViewManagerDelegate();
if (delegate != null) {
delegate.receiveCommand(root, commandId, args);
}
Expand Down

0 comments on commit 80193ed

Please sign in to comment.