mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Summary: ## Context For now, assume TurboModules doesn't exist. **What happens when we call an async NativeModule method?** Everytime JS calls an async NativeModule method, we don't immediately execute it. The legacy infra pushes the call into some queue managed by `MessageQueue.js`. This queue is "flushed" or "emptied" by the following events: - **Flushed:** A C++ -> JS call. NativeModule async methods can called with an `onSuccess` and/or `onFail` callback(s). Calling `NativeToJsBridge::invokeCallback` to invoke one of these callbacks is one way for ObjC++/C++/Java to call into JS. Another way is via JSModule method calls, which are initiated by `NativeToJsBridge::callFunction`. - **Flushed:** When `JSIExecutor::flush` is called. Since TurboModules don't exist, this only happens when we call `JSIExecutor::loadApplicationScript`. - **Emptied:** When more than 5 ms have passed, and the queue hasn't been flushed/emptied, on the next async NativeModule method call, we add to the queue. Afterwards, we empty it, and invoke all the NativeModule method calls. **So, what's the difference between flushed and emptied?** > Note: These are two terms I just made up, but the distinction is important. If the queue was "flushed", and it contained at least one NativeModule method call, `JsToNativeBridge` dispatches the `onBatchComplete` event. On Android, the UIManager module is the only module that listens to this event. This `onBatchComplete` event doesn't fire if the queue was "emptied". **Why does any of this matter?** 1. TurboModules exist. 2. We need the TurboModules infra to have `JsToNativeBridge` dispatch `onBatchComplete`, which depends on: - **Problem 1:** The queue being flushed on calls into JS from Java/C++/ObjC++. - **Problem 2:** There being queued up NativeModule async method calls when the queue is flushed. In D14656466, fkgozali fixed Problem 1 by making every C++/Java/Obj -> JS call from TurboModules also execute `JSIExecutor::flush()`. This means that, with TurboModules, we flush the NativeModule async method call queue as often as we do without TurboModules. So far, so good. However, we still have one big problem: As we convert more NativeModules to TurboModules, the average size of the queue of NativeModule method calls will become smaller and smaller, because more NativeModule method calls will be TurboModule method calls. This queue will more often be empty than not. Therefore, we'll end up dispatching the `onBatchComplete` event less often with TurboModules enabled. So, somehow, when we're about to flush the NativeModule method call queue, we need `JsToNativeBridge` to understand that we've executed TurboModule method calls in the batch. These calls would have normally been queued, which would have led the queue size to be non-zero. So if, during a batch, some TurboModule async method calls were executed, `JsToNativeBridge` should dispatch `onBatchComplete`. **So, what does this diff do?** 1. Make `Instance` responsible for creating the JS `CallInvoker`. 2. Make `NativeToJsBridge` responsible for creating the native `CallInvoker`. `Instance` calls into `NativeToJsBridge` to get the native `CallInvoker`. 3. Hook up `CatalystInstanceImpl`, the Android bridge, with the new JS `CallInvoker`, and the new native `CallInvoker`. This fixes `onBatchComplete` on Android. iOS work is pending. Changelog: [Android][Fixed] - Ensure `onBatchComplete` is dispatched correctly with TurboModules Reviewed By: mdvacca Differential Revision: D20717931 fbshipit-source-id: bc3ccbd6c135b7f084edbc6ddb4d1e3c0c7e0875
171 lines
5.5 KiB
C++
171 lines
5.5 KiB
C++
/*
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <condition_variable>
|
|
#include <list>
|
|
#include <memory>
|
|
#include <mutex>
|
|
|
|
#include <cxxreact/NativeToJsBridge.h>
|
|
|
|
#ifndef RN_EXPORT
|
|
#define RN_EXPORT __attribute__((visibility("default")))
|
|
#endif
|
|
|
|
namespace folly {
|
|
struct dynamic;
|
|
}
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
class JSBigString;
|
|
class JSExecutorFactory;
|
|
class MessageQueueThread;
|
|
class ModuleRegistry;
|
|
class RAMBundleRegistry;
|
|
|
|
struct InstanceCallback {
|
|
virtual ~InstanceCallback() {}
|
|
virtual void onBatchComplete() {}
|
|
virtual void incrementPendingJSCalls() {}
|
|
virtual void decrementPendingJSCalls() {}
|
|
};
|
|
|
|
class RN_EXPORT Instance {
|
|
public:
|
|
~Instance();
|
|
void initializeBridge(
|
|
std::unique_ptr<InstanceCallback> callback,
|
|
std::shared_ptr<JSExecutorFactory> jsef,
|
|
std::shared_ptr<MessageQueueThread> jsQueue,
|
|
std::shared_ptr<ModuleRegistry> moduleRegistry);
|
|
|
|
void setSourceURL(std::string sourceURL);
|
|
|
|
void loadScriptFromString(
|
|
std::unique_ptr<const JSBigString> string,
|
|
std::string sourceURL,
|
|
bool loadSynchronously);
|
|
static bool isIndexedRAMBundle(const char *sourcePath);
|
|
static bool isIndexedRAMBundle(std::unique_ptr<const JSBigString> *string);
|
|
void loadRAMBundleFromString(
|
|
std::unique_ptr<const JSBigString> script,
|
|
const std::string &sourceURL);
|
|
void loadRAMBundleFromFile(
|
|
const std::string &sourcePath,
|
|
const std::string &sourceURL,
|
|
bool loadSynchronously);
|
|
void loadRAMBundle(
|
|
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
std::string startupScriptSourceURL,
|
|
bool loadSynchronously);
|
|
bool supportsProfiling();
|
|
void setGlobalVariable(
|
|
std::string propName,
|
|
std::unique_ptr<const JSBigString> jsonValue);
|
|
void *getJavaScriptContext();
|
|
bool isInspectable();
|
|
bool isBatchActive();
|
|
void callJSFunction(
|
|
std::string &&module,
|
|
std::string &&method,
|
|
folly::dynamic &¶ms);
|
|
void callJSCallback(uint64_t callbackId, folly::dynamic &¶ms);
|
|
|
|
// This method is experimental, and may be modified or removed.
|
|
void registerBundle(uint32_t bundleId, const std::string &bundlePath);
|
|
|
|
const ModuleRegistry &getModuleRegistry() const;
|
|
ModuleRegistry &getModuleRegistry();
|
|
|
|
void handleMemoryPressure(int pressureLevel);
|
|
|
|
/**
|
|
* JS CallInvoker is used by TurboModules to schedule work on the JS thread.
|
|
*
|
|
* Why is the bridge creating JS CallInvoker?
|
|
*
|
|
* - After every Native -> JS call in the TurboModule system, the bridge
|
|
* needs to flush all queued NativeModule method calls. The bridge must
|
|
* also dispatch onBatchComplete if the queue of NativeModule method calls
|
|
* was not empty.
|
|
*/
|
|
std::shared_ptr<CallInvoker> getJSCallInvoker();
|
|
|
|
/**
|
|
* Native CallInvoker is used by TurboModules to schedule work on the
|
|
* NativeModule thread(s).
|
|
*
|
|
* Why is the bridge creating JS 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
|
|
* Native -> JS, if that queue was non-zero in size, JsToNativeBridge
|
|
* dispatches onBatchComplete. When we turn our NativeModules to
|
|
* TurboModuels, there will be less and less pending NativeModule method
|
|
* calls, so onBatchComplete will not fire as often. Therefore, the bridge
|
|
* needs to know how many TurboModule async method calls have been completed
|
|
* 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?
|
|
*
|
|
* - 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.
|
|
*
|
|
*/
|
|
std::shared_ptr<CallInvoker> getNativeCallInvoker(
|
|
std::function<void(std::function<void()> &&work)> &&scheduleWork);
|
|
|
|
private:
|
|
void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch);
|
|
void loadApplication(
|
|
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
std::string startupScriptSourceURL);
|
|
void loadApplicationSync(
|
|
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
std::string startupScriptSourceURL);
|
|
|
|
std::shared_ptr<InstanceCallback> callback_;
|
|
std::shared_ptr<NativeToJsBridge> nativeToJsBridge_;
|
|
std::shared_ptr<ModuleRegistry> moduleRegistry_;
|
|
|
|
std::mutex m_syncMutex;
|
|
std::condition_variable m_syncCV;
|
|
bool m_syncReady = false;
|
|
|
|
class JSCallInvoker : public CallInvoker {
|
|
private:
|
|
std::weak_ptr<NativeToJsBridge> m_nativeToJsBridge;
|
|
std::mutex m_mutex;
|
|
bool m_shouldBuffer = true;
|
|
std::list<std::function<void()>> m_workBuffer;
|
|
|
|
void scheduleAsync(std::function<void()> &&work);
|
|
|
|
public:
|
|
void setNativeToJsBridgeAndFlushCalls(
|
|
std::weak_ptr<NativeToJsBridge> nativeToJsBridge);
|
|
void invokeAsync(std::function<void()> &&work) override;
|
|
};
|
|
|
|
std::shared_ptr<JSCallInvoker> jsCallInvoker_ =
|
|
std::make_shared<JSCallInvoker>();
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|