Skip to content
Closed
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/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 @@ -22,7 +22,7 @@ class PlatformRunLoopObserver : public RunLoopObserver {
public:
PlatformRunLoopObserver(
RunLoopObserver::Activity activities,
const RunLoopObserver::WeakOwner& owner,
RunLoopObserver::WeakOwner owner,
CFRunLoopRef runLoop);

~PlatformRunLoopObserver();
Expand All @@ -45,8 +45,11 @@ class MainRunLoopObserver final : public PlatformRunLoopObserver {
public:
MainRunLoopObserver(
RunLoopObserver::Activity activities,
const RunLoopObserver::WeakOwner& owner)
: PlatformRunLoopObserver(activities, owner, CFRunLoopGetMain()) {}
RunLoopObserver::WeakOwner owner)
: PlatformRunLoopObserver(
activities,
std::move(owner),
CFRunLoopGetMain()) {}
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,10 @@ static CFRunLoopActivity toCFRunLoopActivity(RunLoopObserver::Activity activity)

PlatformRunLoopObserver::PlatformRunLoopObserver(
RunLoopObserver::Activity activities,
const RunLoopObserver::WeakOwner &owner,
RunLoopObserver::WeakOwner owner,
CFRunLoopRef runLoop)
: RunLoopObserver(activities, owner), runLoop_(runLoop)
{
// A value (not a reference) to be captured by the block.
auto weakOwner = owner;

// The documentation for `CFRunLoop` family API states that all of the methods are thread-safe.
// See "Thread Safety and Run Loop Objects" section of the "Threading Programming Guide" for more details.
mainRunLoopObserver_ = CFRunLoopObserverCreateWithHandler(
Expand All @@ -60,7 +57,7 @@ static CFRunLoopActivity toCFRunLoopActivity(RunLoopObserver::Activity activity)
true /* repeats */,
0 /* order */,
^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
auto strongOwner = weakOwner.lock();
auto strongOwner = owner.lock();
if (!strongOwner) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@
#include <react/renderer/core/EventBeat.h>
#include <react/renderer/uimanager/primitives.h>

#include "AsyncEventBeat.h"
#include "AndroidEventBeat.h"

namespace facebook::react {

AsyncEventBeat::AsyncEventBeat(
const EventBeat::SharedOwnerBox& ownerBox,
AndroidEventBeat::AndroidEventBeat(
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)) {
eventBeatManager->addObserver(*this);
}

AsyncEventBeat::~AsyncEventBeat() {
AndroidEventBeat::~AndroidEventBeat() {
eventBeatManager_->removeObserver(*this);
}

void AsyncEventBeat::tick() const {
void AndroidEventBeat::tick() const {
if (!isRequested_ || isBeatCallbackScheduled_) {
return;
}
Expand All @@ -50,11 +50,11 @@ void AsyncEventBeat::tick() const {
});
}

void AsyncEventBeat::induce() const {
void AndroidEventBeat::induce() const {
tick();
}

void AsyncEventBeat::request() const {
void AndroidEventBeat::request() const {
bool alreadyRequested = isRequested_;
EventBeat::request();
if (!alreadyRequested) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@

namespace facebook::react {

class AsyncEventBeat final : public EventBeat, public EventBeatManagerObserver {
class AndroidEventBeat final : public EventBeat,
public EventBeatManagerObserver {
public:
AsyncEventBeat(
const EventBeat::SharedOwnerBox& ownerBox,
AndroidEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
EventBeatManager* eventBeatManager,
RuntimeExecutor runtimeExecutor,
jni::global_ref<jobject> javaUIManager);

~AsyncEventBeat() override;
~AndroidEventBeat() override;

void tick() const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "FabricUIManagerBinding.h"

#include "AsyncEventBeat.h"
#include "AndroidEventBeat.h"
#include "ComponentFactory.h"
#include "EventBeatManager.h"
#include "EventEmitterWrapper.h"
Expand Down 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);
return std::make_unique<AndroidEventBeat>(
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,17 +11,13 @@

namespace facebook::react {

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

void EventBeat::request() const {
isRequested_ = true;
}

void EventBeat::induce() const {
// Default implementation does nothing.
}

void EventBeat::setBeatCallback(BeatCallback beatCallback) {
beatCallback_ = std::move(beatCallback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ class EventBeat {
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;

// not copyable
EventBeat(const EventBeat& other) = delete;
EventBeat& operator=(const EventBeat& other) = delete;

/*
* Communicates to the Beat that a consumer is waiting for the coming beat.
* A consumer must request coming beat after the previous beat happened
Expand All @@ -60,7 +63,6 @@ class EventBeat {
virtual void request() const;

/*
* Sets the beat callback function.
* The callback is must be called on the proper thread.
*/
void setBeatCallback(BeatCallback beatCallback);
Expand All @@ -70,10 +72,10 @@ class EventBeat {
* Induces the next beat to happen as soon as possible.
* Receiver might ignore the call if a beat was not requested.
*/
virtual void induce() const;
virtual void induce() const = 0;

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,15 +17,12 @@ namespace facebook::react {

EventDispatcher::EventDispatcher(
const EventQueueProcessor& eventProcessor,
const EventBeat::Factory& asynchronousEventBeatFactory,
const EventBeat::SharedOwnerBox& ownerBox,
std::unique_ptr<EventBeat> eventBeat,
RuntimeScheduler& runtimeScheduler,
StatePipe statePipe,
std::weak_ptr<EventLogger> eventLogger)
: eventQueue_(EventQueue(
eventProcessor,
asynchronousEventBeatFactory(ownerBox),
runtimeScheduler)),
: eventQueue_(
EventQueue(eventProcessor, std::move(eventBeat), 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,7 @@ class EventDispatcher {

EventDispatcher(
const EventQueueProcessor& eventProcessor,
const EventBeat::Factory& asynchronousEventBeatFactory,
const EventBeat::SharedOwnerBox& ownerBox,
std::unique_ptr<EventBeat> eventBeat,
RuntimeScheduler& runtimeScheduler,
StatePipe statePipe,
std::weak_ptr<EventLogger> eventLogger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ Scheduler::Scheduler(
uiManager->updateState(stateUpdate);
};

auto eventBeat = schedulerToolbox.eventBeatFactory(std::move(eventOwnerBox));

// Creating an `EventDispatcher` instance inside the already allocated
// container (inside the optional).
eventDispatcher_->emplace(
EventQueueProcessor(
eventPipe, eventPipeConclusion, statePipe, eventPerformanceLogger_),
schedulerToolbox.asynchronousEventBeatFactory,
eventOwnerBox,
std::move(eventBeat),
*runtimeScheduler,
statePipe,
eventPerformanceLogger_);
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@

namespace facebook::react {

RunLoopObserver::RunLoopObserver(
Activity activities,
const WeakOwner& owner) noexcept
: activities_(activities), owner_(owner) {}
RunLoopObserver::RunLoopObserver(Activity activities, WeakOwner owner) noexcept
: activities_(activities), owner_(std::move(owner)) {}

void RunLoopObserver::setDelegate(const Delegate* delegate) const noexcept {
// We need these constraints to ensure basic thread-safety.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class RunLoopObserver {
/*
* Constructs a run loop observer.
*/
RunLoopObserver(Activity activities, const WeakOwner& owner) noexcept;
RunLoopObserver(Activity activities, WeakOwner owner) noexcept;
virtual ~RunLoopObserver() noexcept = default;

/*
Expand Down