Rename Instance::getNativeCallinvoker to Instance::getDecoratedNativeCallInvoker

Summary:
Now, instead of accepting a `std::function` that schedules work, and returning a `CallInvoker`, `Instance::getDecoratedNativeCallInvoker`  will accept a `CallInvoker` that schedules work, and return a decorated `CallInvoker`.

I think this change will help with readability. It also clarifies that the bridge is adding additional behaviour to the native `CallInvoker`.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D20826885

fbshipit-source-id: a2c5681d10a4544ee3d2a0d1f1cbd386ef06d0e6
This commit is contained in:
Ramanpreet Nara
2020-04-03 09:47:41 -07:00
committed by Facebook GitHub Bot
parent 83fee73ae6
commit eb4e2baaa9
5 changed files with 38 additions and 25 deletions
@@ -297,12 +297,27 @@ CatalystInstanceImpl::getJSCallInvokerHolder() {
jni::alias_ref<CallInvokerHolder::javaobject>
CatalystInstanceImpl::getNativeCallInvokerHolder() {
if (!nativeCallInvokerHolder_) {
class NativeThreadCallInvoker : public CallInvoker {
private:
std::shared_ptr<JMessageQueueThread> messageQueueThread_;
public:
NativeThreadCallInvoker(
std::shared_ptr<JMessageQueueThread> messageQueueThread)
: messageQueueThread_(messageQueueThread) {}
void invokeAsync(std::function<void()> &&work) override {
messageQueueThread_->runOnQueue(std::move(work));
}
};
std::shared_ptr<CallInvoker> nativeInvoker =
std::make_shared<NativeThreadCallInvoker>(moduleMessageQueue_);
std::shared_ptr<CallInvoker> decoratedNativeInvoker =
instance_->getDecoratedNativeCallInvoker(nativeInvoker);
nativeCallInvokerHolder_ = jni::make_global(
CallInvokerHolder::newObjectCxxArgs(instance_->getNativeCallInvoker(
[moduleMessageQueue =
moduleMessageQueue_](std::function<void()> &&work) {
moduleMessageQueue->runOnQueue(std::move(work));
})));
CallInvokerHolder::newObjectCxxArgs(decoratedNativeInvoker));
}
return nativeCallInvokerHolder_;
+3 -3
View File
@@ -228,9 +228,9 @@ std::shared_ptr<CallInvoker> Instance::getJSCallInvoker() {
return std::static_pointer_cast<CallInvoker>(jsCallInvoker_);
}
std::shared_ptr<CallInvoker> Instance::getNativeCallInvoker(
std::function<void(std::function<void()> &&work)> &&scheduleWork) {
return nativeToJsBridge_->getNativeCallInvoker(std::move(scheduleWork));
std::shared_ptr<CallInvoker> Instance::getDecoratedNativeCallInvoker(
std::shared_ptr<CallInvoker> nativeInvoker) {
return nativeToJsBridge_->getDecoratedNativeCallInvoker(nativeInvoker);
}
void Instance::JSCallInvoker::setNativeToJsBridgeAndFlushCalls(
+6 -7
View File
@@ -106,7 +106,7 @@ class RN_EXPORT Instance {
* Native CallInvoker is used by TurboModules to schedule work on the
* NativeModule thread(s).
*
* Why is the bridge creating JS CallInvoker?
* Why is the bridge decorating native CallInvoker?
*
* - The bridge must be informed of all TurboModule async method calls. Why?
* When all queued NativeModule method calls are flushed by a call from
@@ -118,17 +118,16 @@ class RN_EXPORT Instance {
* since the last time the bridge was flushed. If this number is non-zero,
* we fire onBatchComplete.
*
* Why must we pass in a scheduleWork function?
* Why can't we just create and return a new native CallInvoker?
*
* - On Android, we have one NativeModule thread. That thread is created and
* managed outisde of NativeToJsBridge. On iOS, we have one MethodQueue per
* module. Those MethodQueues are also created and managed outside of
* NativeToJsBridge. Therefore, we need to pass in a function that schedules
* work on the respective thread.
*
* NativeToJsBridge. Therefore, we need to pass in a CallInvoker that
* schedules work on the respective thread.
*/
std::shared_ptr<CallInvoker> getNativeCallInvoker(
std::function<void(std::function<void()> &&work)> &&scheduleWork);
std::shared_ptr<CallInvoker> getDecoratedNativeCallInvoker(
std::shared_ptr<CallInvoker> nativeInvoker);
private:
void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch);
+7 -8
View File
@@ -302,30 +302,29 @@ void NativeToJsBridge::runOnExecutorQueue(
});
}
std::shared_ptr<CallInvoker> NativeToJsBridge::getNativeCallInvoker(
std::function<void(std::function<void()> &&work)> &&scheduleWork) {
std::shared_ptr<CallInvoker> NativeToJsBridge::getDecoratedNativeCallInvoker(
std::shared_ptr<CallInvoker> nativeInvoker) {
class NativeCallInvoker : public CallInvoker {
private:
std::weak_ptr<JsToNativeBridge> m_jsToNativeBridge;
std::function<void(std::function<void()> &&work)> m_scheduleWork;
std::shared_ptr<CallInvoker> m_nativeInvoker;
public:
NativeCallInvoker(
std::weak_ptr<JsToNativeBridge> jsToNativeBridge,
std::function<void(std::function<void()> &&work)> &&scheduleWork)
std::shared_ptr<CallInvoker> nativeInvoker)
: m_jsToNativeBridge(jsToNativeBridge),
m_scheduleWork(std::move(scheduleWork)) {}
m_nativeInvoker(nativeInvoker) {}
void invokeAsync(std::function<void()> &&func) override {
if (auto strongJsToNativeBridge = m_jsToNativeBridge.lock()) {
strongJsToNativeBridge->recordTurboModuleAsyncMethodCall();
}
m_scheduleWork(std::move(func));
m_nativeInvoker->invokeAsync(std::move(func));
}
};
return std::make_shared<NativeCallInvoker>(
m_delegate, std::move(scheduleWork));
return std::make_shared<NativeCallInvoker>(m_delegate, nativeInvoker);
}
} // namespace react
+2 -2
View File
@@ -103,8 +103,8 @@ class NativeToJsBridge {
* Native CallInvoker is used by TurboModules to schedule work on the
* NativeModule thread(s).
*/
std::shared_ptr<CallInvoker> getNativeCallInvoker(
std::function<void(std::function<void()> &&work)> &&scheduleWork);
std::shared_ptr<CallInvoker> getDecoratedNativeCallInvoker(
std::shared_ptr<CallInvoker> nativeInvoker);
private:
// This is used to avoid a race condition where a proxyCallback gets queued