WeakList: add anyOf + const-propagating forEach (#55313)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/55313

Changelog: [Internal]

TSIA - minor refactor for convenience and correctness.

NOTE: When we get C++23, we can deduplicate the identical const/non-const method bodies using the nifty [deducing `this`](https://en.cppreference.com/w/cpp/language/member_functions.html#Explicit_object_member_functions) feature.

Reviewed By: hoxyq

Differential Revision: D91140490

fbshipit-source-id: 75dc12814b4758c7b1b7666fe1d51eac7b2ba9a8
This commit is contained in:
Moti Zilberman authored and meta-codesync[bot] committed 2026-01-30 09:49:31 -08:00
1 parent a4bd274a60
commit fbb3264e1c
3 files changed
+93 -4

No files matched your search

@@ -376,7 +376,7 @@ folly::dynamic createHostMetadataPayload(const HostTargetMetadata& metadata) {
bool HostTarget::hasActiveSessionWithFuseboxClient() const {
bool hasActiveFuseboxSession = false;
sessions_.forEach([&](HostTargetSession& session) {
sessions_.forEach([&](auto& session) {
hasActiveFuseboxSession |= session.hasFuseboxClient();
});
return hasActiveFuseboxSession;
@@ -385,7 +385,7 @@ bool HostTarget::hasActiveSessionWithFuseboxClient() const {
void HostTarget::emitTracingProfileForFirstFuseboxClient(
tracing::HostTracingProfile tracingProfile) {
bool emitted = false;
sessions_.forEach([&](HostTargetSession& session) {
sessions_.forEach([&](auto& session) {
if (emitted) {
/**
* HostTracingProfile object is not copiable for performance reasons,
@@ -30,7 +30,7 @@ class WeakList {
* to destroyed elements) will be removed during iteration.
*/
template <typename Fn>
void forEach(Fn &&fn) const
void forEach(Fn &&fn)
{
for (auto it = ptrs_.begin(); it != ptrs_.end();) {
if (auto ptr = it->lock()) {
@@ -42,6 +42,65 @@ class WeakList {
}
}
/**
* Call the given function for every element in the list, ensuring the element
* is not destroyed for the duration of the call. Elements are visited in the
* order they were inserted.
*
* As a side effect, any null pointers in the underlying list (corresponding
* to destroyed elements) will be removed during iteration.
*/
template <typename Fn>
void forEach(Fn &&fn) const
{
for (auto it = ptrs_.cbegin(); it != ptrs_.cend();) {
if (auto ptr = it->lock()) {
fn(*ptr);
++it;
} else {
it = ptrs_.erase(it);
}
}
}
/**
* Returns true if the given function returns true for any element in the
* list, ensuring the element is not destroyed for the duration of the call.
*
* As a side effect, any null pointers in the underlying list (corresponding
* to destroyed elements) will be removed during iteration.
*/
template <typename Fn>
bool anyOf(Fn &&fn)
{
bool found = false;
forEach([&](auto &element) {
if (!found && fn(element)) {
found = true;
}
});
return found;
}
/**
* Returns true if the given function returns true for any element in the
* list, ensuring the element is not destroyed for the duration of the call.
*
* As a side effect, any null pointers in the underlying list (corresponding
* to destroyed elements) will be removed during iteration.
*/
template <typename Fn>
bool anyOf(Fn &&fn) const
{
bool found = false;
forEach([&](const auto &element) {
if (!found && fn(element)) {
found = true;
}
});
return found;
}
/**
* Returns the number of (non-null) elements in the list. The count will only
* remain accurate as long as the list is not modified and elements are
@@ -70,7 +70,7 @@ TEST(WeakListTest, ForEach) {
EXPECT_THAT(visited, ElementsAre(1, 3));
}
TEST(WeakListTest, ElementsAreAliveDuringCallback) {
TEST(WeakListTest, ElementsAreAliveDuringForEachCallback) {
WeakList<int> list;
auto p1 = std::make_shared<int>(1);
// A separate weak_ptr to observe the lifetime of `p1`.
@@ -88,4 +88,34 @@ TEST(WeakListTest, ElementsAreAliveDuringCallback) {
EXPECT_THAT(visited, ElementsAre(1));
}
TEST(WeakListTest, AnyOf) {
WeakList<int> list;
auto p1 = std::make_shared<int>(1);
auto p2 = std::make_shared<int>(2);
auto p3 = std::make_shared<int>(3);
list.insert(p1);
list.insert(p2);
list.insert(p3);
EXPECT_TRUE(list.anyOf([](const int& value) { return value == 2; }));
EXPECT_FALSE(list.anyOf([](const int& value) { return value == 4; }));
}
TEST(WeakListTest, ElementsAreAliveDuringAnyOfCallback) {
WeakList<int> list;
auto p1 = std::make_shared<int>(42);
// A separate weak_ptr to observe the lifetime of `p1`.
std::weak_ptr wp1 = p1;
list.insert(p1);
bool result = list.anyOf([&](const int& value) {
p1.reset();
EXPECT_FALSE(wp1.expired());
return value == 42;
});
EXPECT_TRUE(result);
EXPECT_TRUE(wp1.expired());
}
} // namespace facebook::react::jsinspector_modern