Support sync loading of the initial bundle/source

Reviewed By: javache

Differential Revision: D3897521

fbshipit-source-id: a4f234c7003a5f4be952d315eb62f382836e24dc
This commit is contained in:
Marc Horowitz
2016-09-26 16:01:42 -07:00
committed by Facebook Github Bot 7
parent 51df83d7d5
commit 6071e8ca2c
4 changed files with 54 additions and 4 deletions

View File

@@ -39,6 +39,10 @@ void Instance::initializeBridge(
nativeQueue=folly::makeMoveWrapper(std::move(nativeQueue))] () mutable {
nativeToJsBridge_ = folly::make_unique<NativeToJsBridge>(
jsef.get(), moduleRegistry, jsQueue, nativeQueue.move(), callback_);
std::lock_guard<std::mutex> lock(m_syncMutex);
m_syncReady = true;
m_syncCV.notify_all();
});
CHECK(nativeToJsBridge_);
@@ -52,6 +56,14 @@ void Instance::loadScriptFromString(std::unique_ptr<const JSBigString> string,
nativeToJsBridge_->loadApplication(nullptr, std::move(string), std::move(sourceURL));
}
void Instance::loadScriptFromStringSync(std::unique_ptr<const JSBigString> string,
std::string sourceURL) {
std::unique_lock<std::mutex> lock(m_syncMutex);
m_syncCV.wait(lock, [this] { return m_syncReady; });
nativeToJsBridge_->loadApplicationSync(nullptr, std::move(string), std::move(sourceURL));
}
void Instance::loadScriptFromFile(const std::string& filename,
const std::string& sourceURL) {
// TODO mhorowitz: ReactMarker around file read
@@ -88,11 +100,21 @@ void Instance::loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL) {
callback_->incrementPendingJSCalls();
SystraceSection s("reactbridge_xplat_setJSModulesUnbundle");
nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(startupScript),
std::move(startupScriptSourceURL));
}
void Instance::loadUnbundleSync(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL) {
std::unique_lock<std::mutex> lock(m_syncMutex);
m_syncCV.wait(lock, [this] { return m_syncReady; });
SystraceSection s("reactbridge_xplat_loadApplicationSync");
nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(startupScript),
std::move(startupScriptSourceURL));
}
bool Instance::supportsProfiling() {
return nativeToJsBridge_->supportsProfiling();
}

View File

@@ -35,12 +35,17 @@ class Instance {
std::unique_ptr<MessageQueueThread> nativeQueue,
std::shared_ptr<ModuleRegistry> moduleRegistry);
void loadScriptFromString(std::unique_ptr<const JSBigString> string, std::string sourceURL);
void loadScriptFromStringSync(std::unique_ptr<const JSBigString> string, std::string sourceURL);
void loadScriptFromFile(const std::string& filename, const std::string& sourceURL);
void loadScriptFromOptimizedBundle(std::string bundlePath, std::string sourceURL, int flags);
void loadUnbundle(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);
void loadUnbundleSync(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);
bool supportsProfiling();
void startProfiler(const std::string& title);
void stopProfiler(const std::string& title, const std::string& filename);
@@ -50,6 +55,7 @@ class Instance {
void callJSCallback(ExecutorToken token, uint64_t callbackId, folly::dynamic&& params);
MethodCallResult callSerializableNativeHook(ExecutorToken token, unsigned int moduleId,
unsigned int methodId, folly::dynamic&& args);
ExecutorToken getMainExecutorToken();
void handleMemoryPressureUiHidden();
void handleMemoryPressureModerate();
@@ -60,6 +66,10 @@ class Instance {
std::shared_ptr<InstanceCallback> callback_;
std::unique_ptr<NativeToJsBridge> nativeToJsBridge_;
std::mutex m_syncMutex;
std::condition_variable m_syncCV;
bool m_syncReady = false;
};
}

View File

@@ -152,6 +152,17 @@ void NativeToJsBridge::loadApplication(
});
}
void NativeToJsBridge::loadApplicationSync(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL) {
if (unbundle) {
m_mainExecutor->setJSModulesUnbundle(std::move(unbundle));
}
m_mainExecutor->loadApplicationScript(std::move(startupScript),
std::move(startupScriptSourceURL));
}
void NativeToJsBridge::callFunction(
ExecutorToken executorToken,
std::string&& module,

View File

@@ -44,9 +44,12 @@ public:
class JsToNativeBridge;
// This class manages calls from native code to JS. It also manages
// executors and their threads. This part is used by both bridges for
// now, but further refactorings should separate the bridges more
// fully #11247981.
// executors and their threads. All functions here can be called from
// any thread.
//
// Except for loadApplicationScriptSync(), all void methods will queue
// work to run on the jsQueue passed to the ctor, and return
// immediately.
class NativeToJsBridge {
public:
friend class JsToNativeBridge;
@@ -86,6 +89,10 @@ public:
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupCode,
std::string sourceURL);
void loadApplicationSync(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupCode,
std::string sourceURL);
/**
* Similar to loading a "bundle", but instead of passing js source this method accepts