Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RuntimeScheduler in EventBeat 2nd try #47196

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-native/React/Fabric/AppleEventBeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace facebook::react {
AppleEventBeat::AppleEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
std::unique_ptr<const RunLoopObserver> uiRunLoopObserver,
RuntimeExecutor runtimeExecutor)
: EventBeat(std::move(ownerBox), std::move(runtimeExecutor)),
RuntimeScheduler& runtimeScheduler)
: EventBeat(std::move(ownerBox), runtimeScheduler),
uiRunLoopObserver_(std::move(uiRunLoopObserver)) {
uiRunLoopObserver_->setDelegate(this);
uiRunLoopObserver_->enable();
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native/React/Fabric/AppleEventBeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace facebook::react {

class RuntimeScheduler;

/*
* Event beat associated with JavaScript runtime.
* The beat is called on `RuntimeExecutor`'s thread induced by the UI thread
Expand All @@ -23,7 +25,7 @@ class AppleEventBeat : public EventBeat, public RunLoopObserver::Delegate {
AppleEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
std::unique_ptr<const RunLoopObserver> uiRunLoopObserver,
RuntimeExecutor runtimeExecutor);
RuntimeScheduler& RuntimeScheduler);

#pragma mark - RunLoopObserver::Delegate

Expand Down
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 @@ -253,10 +253,10 @@ - (RCTScheduler *)_createScheduler
toolbox.bridgelessBindingsExecutor = _bridgelessBindingsExecutor;

toolbox.eventBeatFactory =
[runtimeExecutor](std::shared_ptr<EventBeat::OwnerBox> ownerBox) -> std::unique_ptr<EventBeat> {
[runtimeScheduler](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<AppleEventBeat>(std::move(ownerBox), std::move(runLoopObserver), runtimeExecutor);
return std::make_unique<AppleEventBeat>(std::move(ownerBox), std::move(runLoopObserver), *runtimeScheduler);
};

RCTScheduler *scheduler = [[RCTScheduler alloc] initWithToolbox:toolbox];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ namespace facebook::react {
AndroidEventBeat::AndroidEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
EventBeatManager* eventBeatManager,
RuntimeExecutor runtimeExecutor,
RuntimeScheduler& runtimeScheduler,
jni::global_ref<jobject> javaUIManager)
: EventBeat(std::move(ownerBox), std::move(runtimeExecutor)),
: EventBeat(std::move(ownerBox), runtimeScheduler),
eventBeatManager_(eventBeatManager),
javaUIManager_(std::move(javaUIManager)) {
eventBeatManager->addObserver(*this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AndroidEventBeat final : public EventBeat,
AndroidEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
EventBeatManager* eventBeatManager,
RuntimeExecutor runtimeExecutor,
RuntimeScheduler& runtimeScheduler,
jni::global_ref<jobject> javaUIManager);

~AndroidEventBeat() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,28 +460,25 @@ void FabricUIManagerBinding::installFabricUIManager(

auto runtimeExecutor = runtimeExecutorHolder->cthis()->get();

if (runtimeSchedulerHolder) {
auto runtimeScheduler = runtimeSchedulerHolder->cthis()->get().lock();
if (runtimeScheduler) {
runtimeExecutor =
[runtimeScheduler](
std::function<void(jsi::Runtime & runtime)>&& callback) {
runtimeScheduler->scheduleWork(std::move(callback));
};
contextContainer->insert(
"RuntimeScheduler",
std::weak_ptr<RuntimeScheduler>(runtimeScheduler));
}
auto runtimeScheduler = runtimeSchedulerHolder->cthis()->get().lock();
if (runtimeScheduler) {
runtimeExecutor =
[runtimeScheduler](
std::function<void(jsi::Runtime & runtime)>&& callback) {
runtimeScheduler->scheduleWork(std::move(callback));
};
contextContainer->insert(
"RuntimeScheduler", std::weak_ptr<RuntimeScheduler>(runtimeScheduler));
}

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

Expand Down
29 changes: 15 additions & 14 deletions packages/react-native/ReactCommon/react/renderer/core/EventBeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

#include "EventBeat.h"

#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
#include <utility>

namespace facebook::react {

EventBeat::EventBeat(
std::shared_ptr<OwnerBox> ownerBox,
RuntimeExecutor runtimeExecutor)
: ownerBox_(std::move(ownerBox)),
runtimeExecutor_(std::move(runtimeExecutor)) {}
RuntimeScheduler& runtimeScheduler)
: ownerBox_(std::move(ownerBox)), runtimeScheduler_(runtimeScheduler) {}

void EventBeat::request() const {
isRequested_ = true;
Expand All @@ -33,17 +33,18 @@ void EventBeat::induce() const {
isRequested_ = false;
isBeatCallbackScheduled_ = true;

runtimeExecutor_([this, ownerBox = ownerBox_](jsi::Runtime& runtime) {
auto owner = ownerBox->owner.lock();
if (!owner) {
return;
}

isBeatCallbackScheduled_ = false;
if (beatCallback_) {
beatCallback_(runtime);
}
});
runtimeScheduler_.scheduleWork(
[this, ownerBox = ownerBox_](jsi::Runtime& runtime) {
auto owner = ownerBox->owner.lock();
if (!owner) {
return;
}

isBeatCallbackScheduled_ = false;
if (beatCallback_) {
beatCallback_(runtime);
}
});
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#pragma once

#include <ReactCommon/RuntimeExecutor.h>
#include <atomic>
#include <functional>
#include <memory>

namespace facebook::react {
class RuntimeScheduler;
}

namespace facebook::jsi {
class Runtime;
}
Expand Down Expand Up @@ -56,7 +59,7 @@ class EventBeat {

explicit EventBeat(
std::shared_ptr<OwnerBox> ownerBox,
RuntimeExecutor runtimeExecutor);
RuntimeScheduler& runtimeScheduler);

virtual ~EventBeat() = default;

Expand Down Expand Up @@ -88,7 +91,7 @@ class EventBeat {
mutable std::atomic<bool> isRequested_{false};

private:
RuntimeExecutor runtimeExecutor_;
RuntimeScheduler& runtimeScheduler_;
mutable std::atomic<bool> isBeatCallbackScheduled_{false};
};

Expand Down
Loading