move rncxx utils (#50668)

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

Changelog: [Internal]

Reviewed By: zeyap

Differential Revision: D72800712

fbshipit-source-id: f966906d10df1fac818fc9998dbe5e0b8d67be3f
This commit is contained in:
Andrew Datsenko
2025-04-11 14:35:03 -07:00
committed by Facebook GitHub Bot
parent a92a209544
commit 047914562e
4 changed files with 158 additions and 0 deletions
@@ -0,0 +1,20 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <type_traits>
namespace std {
// mimics the c++23 std::to_underlying
template <class Enum>
constexpr auto to_underlying(Enum e) noexcept {
return static_cast<std::underlying_type_t<Enum>>(e);
}
} // namespace std
@@ -0,0 +1,46 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <react/utils/RunLoopObserver.h>
#include <functional>
#include <utility>
namespace facebook::react {
class PlatformRunLoopObserver;
class PlatformRunLoopObserver : public RunLoopObserver {
public:
PlatformRunLoopObserver(
RunLoopObserver::Activity activities,
const RunLoopObserver::WeakOwner& owner)
: RunLoopObserver(activities, owner) {}
~PlatformRunLoopObserver() override = default;
PlatformRunLoopObserver(PlatformRunLoopObserver& other) = delete;
PlatformRunLoopObserver& operator=(PlatformRunLoopObserver& other) = delete;
PlatformRunLoopObserver(PlatformRunLoopObserver&& other) = delete;
PlatformRunLoopObserver& operator=(PlatformRunLoopObserver&& other) = delete;
bool isOnRunLoopThread() const noexcept override {
return false;
}
void onRender() const noexcept {
if (auto owner = owner_.lock()) {
activityDidChange(activities_);
}
}
private:
void startObserving() const noexcept override {}
void stopObserving() const noexcept override {}
};
} // namespace facebook::react
@@ -0,0 +1,59 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "RunLoopObserverManager.h"
#include <react/debug/react_native_assert.h>
namespace facebook::react {
/*
* Event beat associated with JavaScript runtime. The beat is called on
* RuntimeScheduler's thread induced by the UI thread event loop.
*/
class EventBeatImpl : public EventBeat, public RunLoopObserver::Delegate {
public:
EventBeatImpl(
std::shared_ptr<OwnerBox> ownerBox,
std::shared_ptr<const RunLoopObserver> uiRunLoopObserver,
RuntimeScheduler& runtimeScheduler)
: EventBeat(std::move(ownerBox), runtimeScheduler),
uiRunLoopObserver_(std::move(uiRunLoopObserver)) {
uiRunLoopObserver_->setDelegate(this);
uiRunLoopObserver_->enable();
}
#pragma mark - RunLoopObserver::Delegate
void activityDidChange(
const RunLoopObserver::Delegate* delegate,
RunLoopObserver::Activity /*activity*/) const noexcept override {
react_native_assert(delegate == this);
induce();
}
private:
std::shared_ptr<const RunLoopObserver> uiRunLoopObserver_;
};
std::unique_ptr<EventBeat> RunLoopObserverManager::createEventBeat(
std::shared_ptr<EventBeat::OwnerBox> ownerBox,
RuntimeScheduler& runtimeScheduler) {
auto observer = std::make_shared<const PlatformRunLoopObserver>(
RunLoopObserver::Activity::BeforeWaiting, ownerBox->owner);
observer_ = observer;
return std::make_unique<EventBeatImpl>(
std::move(ownerBox), std::move(observer), runtimeScheduler);
}
void RunLoopObserverManager::onRender() const noexcept {
if (auto observer = observer_.lock()) {
observer->onRender();
}
}
} // namespace facebook::react
@@ -0,0 +1,33 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include "PlatformRunLoopObserver.h"
#include <react/renderer/core/EventBeat.h>
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
#include <memory>
namespace facebook::react {
class RunLoopObserverManager
: std::enable_shared_from_this<RunLoopObserverManager> {
public:
std::unique_ptr<EventBeat> createEventBeat(
std::shared_ptr<EventBeat::OwnerBox> ownerBox,
RuntimeScheduler& runtimeScheduler);
void onRender() const noexcept;
void induce() const noexcept;
private:
std::weak_ptr<const PlatformRunLoopObserver> observer_;
};
} // namespace facebook::react