Skip to content

Commit

Permalink
feat: make view recycling optional on iOS (#35378)
Browse files Browse the repository at this point in the history
Summary:
This PR resolves the potential problem of misconfiguration of components after being recycled. Some of them have custom, sometimes native (e.g. connected to VCs) logic that messes up with the concept of recycling.

bypass-github-export-checks

## Changelog

Added `shouldBeRecycled` field checking to `RCTComponentViewClassDescriptor `, a check for it in `_enqueueComponentViewWithComponentHandle:(ComponentHandle)componentHandle
                         componentViewDescriptor:(RCTComponentViewDescriptor)componentViewDescriptor` method, and a default implementation in `RCTComponentViewDescriptor` returning `YES` in order not to change the default behavior.

[iOS] [Added] - Add `shouldBeRecycled` method on `iOS`.

Pull Request resolved: #35378

Test Plan: Override this method in your custom `componentView` and see that the component is not recycled.

Reviewed By: javache

Differential Revision: D41381683

Pulled By: cipolleschi

fbshipit-source-id: 10fd1e88f99b3608767c0b57fad462837924f02a
  • Loading branch information
WoLewicki authored and facebook-github-bot committed Jan 5, 2024
1 parent cfeb43e commit 613a5a7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class RCTComponentViewClassDescriptor final {
*/
bool observesMountingTransactionWillMount{false};
bool observesMountingTransactionDidMount{false};

/*
* Whether the component can be recycled or not
*/
bool shouldBeRecycled{true};
};

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class RCTComponentViewDescriptor final {
*/
bool observesMountingTransactionWillMount{false};
bool observesMountingTransactionDidMount{false};
bool shouldBeRecycled{true};
};

inline bool operator==(const RCTComponentViewDescriptor &lhs, const RCTComponentViewDescriptor &rhs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ - (RCTComponentViewClassDescriptor)_componentViewClassDescriptorFromClass:(Class
(bool)class_respondsToSelector(viewClass, @selector(mountingTransactionWillMount:withSurfaceTelemetry:)),
.observesMountingTransactionDidMount =
(bool)class_respondsToSelector(viewClass, @selector(mountingTransactionDidMount:withSurfaceTelemetry:)),
.shouldBeRecycled = [viewClass respondsToSelector:@selector(shouldBeRecycled)]
? (bool)[viewClass performSelector:@selector(shouldBeRecycled)]
: true,
};
#pragma clang diagnostic pop
}
Expand Down Expand Up @@ -210,6 +213,7 @@ - (RCTComponentViewDescriptor)createComponentViewWithComponentHandle:(facebook::
.view = [viewClass new],
.observesMountingTransactionWillMount = componentViewClassDescriptor.observesMountingTransactionWillMount,
.observesMountingTransactionDidMount = componentViewClassDescriptor.observesMountingTransactionDidMount,
.shouldBeRecycled = componentViewClassDescriptor.shouldBeRecycled,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ - (void)_enqueueComponentViewWithComponentHandle:(ComponentHandle)componentHandl
RCTAssertMainQueue();
auto &recycledViews = _recyclePool[componentHandle];

if (recycledViews.size() > RCTComponentViewRegistryRecyclePoolMaxSize) {
if (recycledViews.size() > RCTComponentViewRegistryRecyclePoolMaxSize || !componentViewDescriptor.shouldBeRecycled) {
return;
}

Expand Down

0 comments on commit 613a5a7

Please sign in to comment.