enable the usage of the BGUI thread

Reviewed By: javache

Differential Revision: D4928729

fbshipit-source-id: 2a3baa01856cb7145d5f0155da0b959c330b7425
This commit is contained in:
Aaron Chiu 2017-04-25 16:10:00 -07:00 committed by Facebook Github Bot
parent 28aaa88808
commit 513da6fcf2
8 changed files with 55 additions and 13 deletions

View File

@ -933,12 +933,14 @@ public class ReactInstanceManager {
? mNativeModuleCallExceptionHandler
: mDevSupportManager;
CatalystInstanceImpl.Builder catalystInstanceBuilder = new CatalystInstanceImpl.Builder()
.setReactQueueConfigurationSpec(ReactQueueConfigurationSpec.createDefault())
.setJSExecutor(jsExecutor)
.setRegistry(nativeModuleRegistry)
.setJSModuleRegistry(jsModulesBuilder.build())
.setJSBundleLoader(jsBundleLoader)
.setNativeModuleCallExceptionHandler(exceptionHandler);
.setReactQueueConfigurationSpec(mUseSeparateUIBackgroundThread ?
ReactQueueConfigurationSpec.createWithSeparateUIBackgroundThread() :
ReactQueueConfigurationSpec.createDefault())
.setJSExecutor(jsExecutor)
.setRegistry(nativeModuleRegistry)
.setJSModuleRegistry(jsModulesBuilder.build())
.setJSBundleLoader(jsBundleLoader)
.setNativeModuleCallExceptionHandler(exceptionHandler);
ReactMarker.logMarker(CREATE_CATALYST_INSTANCE_START);
// CREATE_CATALYST_INSTANCE_END is in JSCExecutor.cpp

View File

@ -308,6 +308,10 @@ public class ReactContext extends ContextWrapper {
Assertions.assertNotNull(mJSMessageQueueThread).runOnQueue(runnable);
}
public boolean hasUIBackgroundRunnableThread() {
return mUiBackgroundMessageQueueThread != null;
}
public void runUIBackgroundRunnable(Runnable runnable) {
if (mUiBackgroundMessageQueueThread == null) {
runOnNativeModulesQueueThread(runnable);

View File

@ -89,6 +89,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
private final NativeModuleRegistry mJavaRegistry;
private final NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler;
private final MessageQueueThread mNativeModulesQueueThread;
private final @Nullable MessageQueueThread mUIBackgroundQueueThread;
private boolean mInitialized = false;
private volatile boolean mAcceptCalls = false;
@ -118,6 +119,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
mJSBundleLoader = jsBundleLoader;
mNativeModuleCallExceptionHandler = nativeModuleCallExceptionHandler;
mNativeModulesQueueThread = mReactQueueConfiguration.getNativeModulesQueueThread();
mUIBackgroundQueueThread = mReactQueueConfiguration.getUIBackgroundQueueThread();
mTraceListener = new JSProfilerTraceListener(this);
FLog.d(ReactConstants.TAG, "Initializing React Xplat Bridge before initializeBridge");
@ -126,6 +128,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
jsExecutor,
mReactQueueConfiguration.getJSQueueThread(),
mNativeModulesQueueThread,
mUIBackgroundQueueThread,
mJavaRegistry.getJavaModules(this),
mJavaRegistry.getCxxModules());
FLog.d(ReactConstants.TAG, "Initializing React Xplat Bridge after initializeBridge");
@ -171,6 +174,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
JavaScriptExecutor jsExecutor,
MessageQueueThread jsQueue,
MessageQueueThread moduleQueue,
MessageQueueThread uiBackgroundQueue,
Collection<JavaModuleWrapper> javaModules,
Collection<ModuleHolder> cxxModules);

View File

@ -86,8 +86,10 @@ CatalystInstanceImpl::CatalystInstanceImpl()
: instance_(folly::make_unique<Instance>()) {}
CatalystInstanceImpl::~CatalystInstanceImpl() {
// TODO: 16669252: this prevents onCatalystInstanceDestroy from being called
moduleMessageQueue_->quitSynchronous();
if (uiBackgroundMessageQueue_ != NULL) {
uiBackgroundMessageQueue_->quitSynchronous();
}
}
void CatalystInstanceImpl::registerNatives() {
@ -118,11 +120,15 @@ void CatalystInstanceImpl::initializeBridge(
JavaScriptExecutorHolder* jseh,
jni::alias_ref<JavaMessageQueueThread::javaobject> jsQueue,
jni::alias_ref<JavaMessageQueueThread::javaobject> nativeModulesQueue,
jni::alias_ref<JavaMessageQueueThread::javaobject> uiBackgroundQueue,
jni::alias_ref<jni::JCollection<JavaModuleWrapper::javaobject>::javaobject> javaModules,
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules) {
// TODO mhorowitz: how to assert here?
// Assertions.assertCondition(mBridge == null, "initializeBridge should be called once");
moduleMessageQueue_ = std::make_shared<JMessageQueueThread>(nativeModulesQueue);
if (uiBackgroundQueue.get() != nullptr) {
uiBackgroundMessageQueue_ = std::make_shared<JMessageQueueThread>(uiBackgroundQueue);
}
// This used to be:
//
@ -141,11 +147,17 @@ void CatalystInstanceImpl::initializeBridge(
// stack.
instance_->initializeBridge(
folly::make_unique<JInstanceCallback>(callback, moduleMessageQueue_),
folly::make_unique<JInstanceCallback>(
callback,
uiBackgroundMessageQueue_ != NULL ? uiBackgroundMessageQueue_ : moduleMessageQueue_),
jseh->getExecutorFactory(),
folly::make_unique<JMessageQueueThread>(jsQueue),
buildModuleRegistry(
std::weak_ptr<Instance>(instance_), javaModules, cxxModules, moduleMessageQueue_));
std::weak_ptr<Instance>(instance_),
javaModules,
cxxModules,
moduleMessageQueue_,
uiBackgroundMessageQueue_));
}
void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) {

View File

@ -48,6 +48,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
JavaScriptExecutorHolder* jseh,
jni::alias_ref<JavaMessageQueueThread::javaobject> jsQueue,
jni::alias_ref<JavaMessageQueueThread::javaobject> moduleQueue,
jni::alias_ref<JavaMessageQueueThread::javaobject> uiBackgroundQueue,
jni::alias_ref<jni::JCollection<JavaModuleWrapper::javaobject>::javaobject> javaModules,
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules);
@ -74,6 +75,7 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
// will have a weak reference.
std::shared_ptr<Instance> instance_;
std::shared_ptr<JMessageQueueThread> moduleMessageQueue_;
std::shared_ptr<JMessageQueueThread> uiBackgroundMessageQueue_;
};
}}

View File

@ -34,6 +34,11 @@ struct JavaModuleWrapper : jni::JavaClass<JavaModuleWrapper> {
return getModule(self());
}
std::string getName() const {
static auto getName = javaClassStatic()->getMethod<jstring()>("getName");
return getName(self())->toStdString();
}
jni::local_ref<jni::JList<JMethodDescriptor::javaobject>::javaobject> getMethodDescriptors() {
static auto getMethods = getClass()
->getMethod<jni::JList<JMethodDescriptor::javaobject>::javaobject()>("getMethodDescriptors");

View File

@ -33,12 +33,25 @@ std::unique_ptr<ModuleRegistry> buildModuleRegistry(
std::weak_ptr<Instance> winstance,
jni::alias_ref<jni::JCollection<JavaModuleWrapper::javaobject>::javaobject> javaModules,
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules,
std::shared_ptr<MessageQueueThread> moduleMessageQueue) {
std::shared_ptr<MessageQueueThread> moduleMessageQueue,
std::shared_ptr<MessageQueueThread> uiBackgroundMessageQueue) {
std::vector<std::unique_ptr<NativeModule>> modules;
if (javaModules) {
for (const auto& jm : *javaModules) {
modules.emplace_back(folly::make_unique<JavaNativeModule>(
std::string name = jm->getName();
if (uiBackgroundMessageQueue != NULL &&
// This is techinically a hack. Perhaps we should bind the specific queue to the module
// in the module holder or wrapper.
// TODO expose as module configuration option
(name == "UIManager" ||
name == "NativeAnimatedModule" ||
name == "FBFacebookReactNavigator")) {
modules.emplace_back(folly::make_unique<JavaNativeModule>(
winstance, jm, uiBackgroundMessageQueue));
} else {
modules.emplace_back(folly::make_unique<JavaNativeModule>(
winstance, jm, moduleMessageQueue));
}
}
}
if (cxxModules) {

View File

@ -27,7 +27,7 @@ std::unique_ptr<ModuleRegistry> buildModuleRegistry(
std::weak_ptr<Instance> winstance,
jni::alias_ref<jni::JCollection<JavaModuleWrapper::javaobject>::javaobject> javaModules,
jni::alias_ref<jni::JCollection<ModuleHolder::javaobject>::javaobject> cxxModules,
std::shared_ptr<MessageQueueThread> moduleMessageQueue);
std::shared_ptr<MessageQueueThread> moduleMessageQueue,
std::shared_ptr<MessageQueueThread> uiBackgroundMessageQueue);
}
}