Skip to content

Commit

Permalink
refactor EventBeat owner (#47050)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #47050

changelog: [internal]

- Rename SchedulerToolbox::asynchronousEventBeatFactory to SchedulerToolbox::eventBeatFactory. There is only one eventBeat, no need to distinguish between sync and async.
- Remove typealias EventBeat::SharedOwnerBox and pass by value instead of const&.

# Goal of this stack:
Centralise event beat logic into EventBeat class inside react-native-github. Subclasses should only override `EventBeat::request` and `EventBeat::induce`.

Reviewed By: christophpurrer

Differential Revision: D64291288

fbshipit-source-id: 96afc56a51677ed9e61b2a662bb968678fa770f7
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Oct 17, 2024
1 parent 125d012 commit e000021
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions packages/react-native/React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ - (RCTScheduler *)_createScheduler
toolbox.runtimeExecutor = runtimeExecutor;
toolbox.bridgelessBindingsExecutor = _bridgelessBindingsExecutor;

toolbox.asynchronousEventBeatFactory =
[runtimeExecutor](const EventBeat::SharedOwnerBox &ownerBox) -> std::unique_ptr<EventBeat> {
toolbox.eventBeatFactory =
[runtimeExecutor](std::shared_ptr<EventBeat::OwnerBox> ownerBox) -> std::unique_ptr<EventBeat> {
auto runLoopObserver =
std::make_unique<const MainRunLoopObserver>(RunLoopObserver::Activity::BeforeWaiting, ownerBox->owner);
return std::make_unique<AsynchronousEventBeat>(std::move(runLoopObserver), runtimeExecutor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
namespace facebook::react {

AsyncEventBeat::AsyncEventBeat(
const EventBeat::SharedOwnerBox& ownerBox,
std::shared_ptr<OwnerBox> ownerBox,
EventBeatManager* eventBeatManager,
RuntimeExecutor runtimeExecutor,
jni::global_ref<jobject> javaUIManager)
: EventBeat(ownerBox),
: EventBeat(std::move(ownerBox)),
eventBeatManager_(eventBeatManager),
runtimeExecutor_(std::move(runtimeExecutor)),
javaUIManager_(std::move(javaUIManager)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace facebook::react {
class AsyncEventBeat final : public EventBeat, public EventBeatManagerObserver {
public:
AsyncEventBeat(
const EventBeat::SharedOwnerBox& ownerBox,
std::shared_ptr<OwnerBox> ownerBox,
EventBeatManager* eventBeatManager,
RuntimeExecutor runtimeExecutor,
jni::global_ref<jobject> javaUIManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,12 +485,15 @@ void FabricUIManagerBinding::installFabricUIManager(
}
}

EventBeat::Factory asynchronousBeatFactory =
EventBeat::Factory eventBeatFactory =
[eventBeatManager, runtimeExecutor, globalJavaUiManager](
const EventBeat::SharedOwnerBox& ownerBox)
std::shared_ptr<EventBeat::OwnerBox> ownerBox)
-> std::unique_ptr<EventBeat> {
return std::make_unique<AsyncEventBeat>(
ownerBox, eventBeatManager, runtimeExecutor, globalJavaUiManager);
std::move(ownerBox),
eventBeatManager,
runtimeExecutor,
globalJavaUiManager);
};

contextContainer->insert("ReactNativeConfig", config);
Expand All @@ -513,7 +516,7 @@ void FabricUIManagerBinding::installFabricUIManager(
toolbox.bridgelessBindingsExecutor = std::nullopt;
toolbox.runtimeExecutor = runtimeExecutor;

toolbox.asynchronousEventBeatFactory = asynchronousBeatFactory;
toolbox.eventBeatFactory = eventBeatFactory;

animationDriver_ = std::make_shared<LayoutAnimationDriver>(
runtimeExecutor, contextContainer, this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace facebook::react {

EventBeat::EventBeat(SharedOwnerBox ownerBox)
EventBeat::EventBeat(std::shared_ptr<OwnerBox> ownerBox)
: ownerBox_(std::move(ownerBox)) {}

void EventBeat::request() const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ class EventBeat {
* creation of some other object that owns an `EventBeat`.
*/
using Owner = std::weak_ptr<const void>;

struct OwnerBox {
Owner owner;
};
using SharedOwnerBox = std::shared_ptr<OwnerBox>;

using Factory =
std::function<std::unique_ptr<EventBeat>(const SharedOwnerBox& ownerBox)>;
using Factory = std::function<std::unique_ptr<EventBeat>(
std::shared_ptr<OwnerBox> ownerBox)>;

using BeatCallback = std::function<void(jsi::Runtime& runtime)>;

EventBeat(SharedOwnerBox ownerBox);
explicit EventBeat(std::shared_ptr<OwnerBox> ownerBox);

virtual ~EventBeat() = default;

Expand All @@ -73,7 +73,7 @@ class EventBeat {
virtual void induce() const;

BeatCallback beatCallback_;
SharedOwnerBox ownerBox_;
std::shared_ptr<OwnerBox> ownerBox_;
mutable std::atomic<bool> isRequested_{false};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ namespace facebook::react {

EventDispatcher::EventDispatcher(
const EventQueueProcessor& eventProcessor,
const EventBeat::Factory& asynchronousEventBeatFactory,
const EventBeat::SharedOwnerBox& ownerBox,
const EventBeat::Factory& eventBeatFactory,
std::shared_ptr<EventBeat::OwnerBox> ownerBox,
RuntimeScheduler& runtimeScheduler,
StatePipe statePipe,
std::weak_ptr<EventLogger> eventLogger)
: eventQueue_(EventQueue(
eventProcessor,
asynchronousEventBeatFactory(ownerBox),
eventBeatFactory(std::move(ownerBox)),
runtimeScheduler)),
statePipe_(std::move(statePipe)),
eventLogger_(std::move(eventLogger)) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class EventDispatcher {

EventDispatcher(
const EventQueueProcessor& eventProcessor,
const EventBeat::Factory& asynchronousEventBeatFactory,
const EventBeat::SharedOwnerBox& ownerBox,
const EventBeat::Factory& eventBeatFactory,
std::shared_ptr<EventBeat::OwnerBox> ownerBox,
RuntimeScheduler& runtimeScheduler,
StatePipe statePipe,
std::weak_ptr<EventLogger> eventLogger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Scheduler::Scheduler(
eventDispatcher_->emplace(
EventQueueProcessor(
eventPipe, eventPipeConclusion, statePipe, eventPerformanceLogger_),
schedulerToolbox.asynchronousEventBeatFactory,
schedulerToolbox.eventBeatFactory,
eventOwnerBox,
*runtimeScheduler,
statePipe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ struct SchedulerToolbox final {
RuntimeExecutor runtimeExecutor;

/*
* Asynchronous & synchronous event beats.
* Represent connections with the platform-specific run loops and general
* purpose background queue.
*/
EventBeat::Factory asynchronousEventBeatFactory;
EventBeat::Factory eventBeatFactory;

/*
* A list of `UIManagerCommitHook`s that should be registered in `UIManager`.
Expand Down

0 comments on commit e000021

Please sign in to comment.