Fabric: Making jsi::Runtime a part of EventBeat and EventPipe

Summary:
We double down on JSI in Fabric. So, practically, JSI is now a hard dependency for Fabric. I hope it's for good.
Now `jsi::Runtime` is coupled with scheduling via `EventExecuter`, so we have to make `jsi::Runtime` a part of `EventBeat` to proxy runtime reference to bindgings.

Reviewed By: sahrens

Differential Revision: D12837225

fbshipit-source-id: 98edc33d6a3358e6c2905f2f03ce0004a9ca0503
This commit is contained in:
Valentin Shergin 2018-11-06 10:58:49 -08:00 committed by Facebook Github Bot
parent 3110a67047
commit df4521e6c4
11 changed files with 27 additions and 13 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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"),
],

View File

@ -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);
}
}

View File

@ -7,6 +7,7 @@
#pragma once
#include <jsi/jsi.h>
#include <atomic>
#include <functional>
#include <memory>
@ -22,7 +23,7 @@ class EventBeat {
public:
virtual ~EventBeat() = default;
using BeatCallback = std::function<void()>;
using BeatCallback = std::function<void(jsi::Runtime &runtime)>;
using FailCallback = std::function<void()>;
/*
@ -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_;

View File

@ -16,7 +16,8 @@ EventQueue::EventQueue(
EventPipe eventPipe,
std::unique_ptr<EventBeat> 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<RawEvent> queue;
{
@ -42,7 +43,8 @@ void EventQueue::onBeat() const {
std::lock_guard<std::recursive_mutex> 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);
}
}
}

View File

@ -14,6 +14,7 @@
#include <fabric/events/EventBeat.h>
#include <fabric/events/RawEvent.h>
#include <fabric/events/primitives.h>
#include <jsi/jsi.h>
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> eventBeat_;
mutable std::vector<RawEvent>
queue_; // Thread-safe, protected by `queueMutex_`.
// Thread-safe, protected by `queueMutex_`.
mutable std::vector<RawEvent> queue_;
mutable std::mutex queueMutex_;
};

View File

@ -8,6 +8,7 @@
#pragma once
#include <folly/dynamic.h>
#include <jsi/jsi.h>
namespace facebook {
namespace react {
@ -47,6 +48,7 @@ using SharedEventTarget = std::shared_ptr<const EventTarget>;
using WeakEventTarget = std::weak_ptr<const EventTarget>;
using EventPipe = std::function<void(
jsi::Runtime &runtime,
const EventTarget *eventTarget,
const std::string &type,
const folly::dynamic &payload)>;

View File

@ -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 {

View File

@ -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;

View File

@ -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);