diff --git a/React/Fabric/Utils/MainRunLoopEventBeat.mm b/React/Fabric/Utils/MainRunLoopEventBeat.mm index 5762c6990..d3bf8d553 100644 --- a/React/Fabric/Utils/MainRunLoopEventBeat.mm +++ b/React/Fabric/Utils/MainRunLoopEventBeat.mm @@ -61,14 +61,18 @@ void MainRunLoopEventBeat::lockExecutorAndBeat() const { mutex2.lock(); mutex3.lock(); + jsi::Runtime *runtimePtr; + runtimeExecutor_([&](jsi::Runtime &runtime) { + runtimePtr = &runtime; mutex1.unlock(); + // `beat` is called somewhere here. mutex2.lock(); mutex3.unlock(); }); mutex1.lock(); - beat(); + beat(*runtimePtr); mutex2.unlock(); mutex3.lock(); } diff --git a/React/Fabric/Utils/RuntimeEventBeat.mm b/React/Fabric/Utils/RuntimeEventBeat.mm index 911ab6061..042fe8ed1 100644 --- a/React/Fabric/Utils/RuntimeEventBeat.mm +++ b/React/Fabric/Utils/RuntimeEventBeat.mm @@ -59,7 +59,7 @@ void RuntimeEventBeat::induce() const { isBusy_ = true; runtimeExecutor_([=](jsi::Runtime &runtime) mutable { - this->beat(); + this->beat(runtime); isBusy_ = false; #ifndef NDEBUG *wasExecuted = true; diff --git a/ReactCommon/fabric/events/BUCK b/ReactCommon/fabric/events/BUCK index f666259af..b75663932 100644 --- a/ReactCommon/fabric/events/BUCK +++ b/ReactCommon/fabric/events/BUCK @@ -55,6 +55,7 @@ rn_xplat_cxx_library( "xplat//folly:headers_only", "xplat//folly:memory", "xplat//folly:molly", + "xplat//jsi:jsi", "xplat//third-party/glog:glog", react_native_xplat_target("fabric/debug:debug"), ], diff --git a/ReactCommon/fabric/events/EventBeat.cpp b/ReactCommon/fabric/events/EventBeat.cpp index 48d175ec5..2ce95b4d3 100644 --- a/ReactCommon/fabric/events/EventBeat.cpp +++ b/ReactCommon/fabric/events/EventBeat.cpp @@ -14,7 +14,7 @@ void EventBeat::request() const { isRequested_ = true; } -void EventBeat::beat() const { +void EventBeat::beat(jsi::Runtime &runtime) const { if (!this->isRequested_) { return; } @@ -22,7 +22,7 @@ void EventBeat::beat() const { isRequested_ = false; if (beatCallback_) { - beatCallback_(); + beatCallback_(runtime); } } diff --git a/ReactCommon/fabric/events/EventBeat.h b/ReactCommon/fabric/events/EventBeat.h index 7a991a69e..2f86600a4 100644 --- a/ReactCommon/fabric/events/EventBeat.h +++ b/ReactCommon/fabric/events/EventBeat.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -22,7 +23,7 @@ class EventBeat { public: virtual ~EventBeat() = default; - using BeatCallback = std::function; + using BeatCallback = std::function; using FailCallback = std::function; /* @@ -62,7 +63,7 @@ class EventBeat { * Should be used by sublasses to send a beat. * Receiver might ignore the call if a beat was not requested. */ - void beat() const; + void beat(jsi::Runtime &runtime) const; BeatCallback beatCallback_; FailCallback failCallback_; diff --git a/ReactCommon/fabric/events/EventQueue.cpp b/ReactCommon/fabric/events/EventQueue.cpp index 56389d3ca..2ec13d5c1 100644 --- a/ReactCommon/fabric/events/EventQueue.cpp +++ b/ReactCommon/fabric/events/EventQueue.cpp @@ -16,7 +16,8 @@ EventQueue::EventQueue( EventPipe eventPipe, std::unique_ptr eventBeat) : eventPipe_(std::move(eventPipe)), eventBeat_(std::move(eventBeat)) { - eventBeat_->setBeatCallback(std::bind(&EventQueue::onBeat, this)); + eventBeat_->setBeatCallback( + std::bind(&EventQueue::onBeat, this, std::placeholders::_1)); } void EventQueue::enqueueEvent(const RawEvent &rawEvent) const { @@ -24,7 +25,7 @@ void EventQueue::enqueueEvent(const RawEvent &rawEvent) const { queue_.push_back(rawEvent); } -void EventQueue::onBeat() const { +void EventQueue::onBeat(jsi::Runtime &runtime) const { std::vector queue; { @@ -42,7 +43,8 @@ void EventQueue::onBeat() const { std::lock_guard lock(EventEmitter::DispatchMutex()); for (const auto &event : queue) { - eventPipe_(event.eventTarget.lock().get(), event.type, event.payload); + eventPipe_( + runtime, event.eventTarget.lock().get(), event.type, event.payload); } } } diff --git a/ReactCommon/fabric/events/EventQueue.h b/ReactCommon/fabric/events/EventQueue.h index 152937c83..aec7a3c9f 100644 --- a/ReactCommon/fabric/events/EventQueue.h +++ b/ReactCommon/fabric/events/EventQueue.h @@ -14,6 +14,7 @@ #include #include #include +#include namespace facebook { namespace react { @@ -34,12 +35,12 @@ class EventQueue { virtual void enqueueEvent(const RawEvent &rawEvent) const; protected: - void onBeat() const; + void onBeat(jsi::Runtime &runtime) const; const EventPipe eventPipe_; const std::unique_ptr eventBeat_; - mutable std::vector - queue_; // Thread-safe, protected by `queueMutex_`. + // Thread-safe, protected by `queueMutex_`. + mutable std::vector queue_; mutable std::mutex queueMutex_; }; diff --git a/ReactCommon/fabric/events/primitives.h b/ReactCommon/fabric/events/primitives.h index 9428f446e..cd8a61a9e 100644 --- a/ReactCommon/fabric/events/primitives.h +++ b/ReactCommon/fabric/events/primitives.h @@ -8,6 +8,7 @@ #pragma once #include +#include namespace facebook { namespace react { @@ -47,6 +48,7 @@ using SharedEventTarget = std::shared_ptr; using WeakEventTarget = std::weak_ptr; using EventPipe = std::function; diff --git a/ReactCommon/fabric/uimanager/FabricUIManager.cpp b/ReactCommon/fabric/uimanager/FabricUIManager.cpp index 82ef1d8a9..79d850a60 100644 --- a/ReactCommon/fabric/uimanager/FabricUIManager.cpp +++ b/ReactCommon/fabric/uimanager/FabricUIManager.cpp @@ -93,6 +93,7 @@ void FabricUIManager::setStopSurfaceFunction( } void FabricUIManager::dispatchEventToTarget( + jsi::Runtime &runtime, const EventTarget *eventTarget, const std::string &type, const folly::dynamic &payload) const { diff --git a/ReactCommon/fabric/uimanager/FabricUIManager.h b/ReactCommon/fabric/uimanager/FabricUIManager.h index 4b47dfae6..70496fa11 100644 --- a/ReactCommon/fabric/uimanager/FabricUIManager.h +++ b/ReactCommon/fabric/uimanager/FabricUIManager.h @@ -85,6 +85,7 @@ class FabricUIManager { #pragma mark - Native-facing Interface void dispatchEventToTarget( + jsi::Runtime &runtime, const EventTarget *eventTarget, const std::string &type, const folly::dynamic &payload) const; diff --git a/ReactCommon/fabric/uimanager/Scheduler.cpp b/ReactCommon/fabric/uimanager/Scheduler.cpp index 92436b626..4fd424398 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.cpp +++ b/ReactCommon/fabric/uimanager/Scheduler.cpp @@ -44,7 +44,8 @@ Scheduler::Scheduler(const SharedContextContainer &contextContainer) uiManager_.get(), std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3), + std::placeholders::_3, + std::placeholders::_4), synchronousEventBeatFactory, asynchronousEventBeatFactory);