Skip to content

Commit a47431e

Browse files
fromcelticparkfacebook-github-bot
authored andcommitted
Adopt split segments registration approach on Android
Differential Revision: D6284863 fbshipit-source-id: 0df6b90eb0cbeab4c8a2b11f1e4dcbd5d5dfab72
1 parent 6812789 commit a47431e

File tree

6 files changed

+15
-37
lines changed

6 files changed

+15
-37
lines changed

ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstance.java

+3
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ void callFunction(
8686
*/
8787
void removeBridgeIdleDebugListener(NotThreadSafeBridgeIdleDebugListener listener);
8888

89+
/** This method registers the file path of an additional JS segment by its ID. */
90+
void registerSegment(int segmentId, String path);
91+
8992
@VisibleForTesting
9093
void setGlobalVariable(String propName, String jsonValue);
9194

ReactAndroid/src/main/java/com/facebook/react/bridge/CatalystInstanceImpl.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ private native void initializeBridge(
210210
jniSetSourceURL(remoteURL);
211211
}
212212

213-
/* package */ void setJsSegmentsDirectory(String directoryPath) {
214-
jniSetJsSegmentsDirectory(directoryPath);
213+
@Override
214+
public void registerSegment(int segmentId, String path) {
215+
jniRegisterSegment(segmentId, path);
215216
}
216217

217218
/* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) {
@@ -225,7 +226,7 @@ private native void initializeBridge(
225226
}
226227

227228
private native void jniSetSourceURL(String sourceURL);
228-
private native void jniSetJsSegmentsDirectory(String directoryPath);
229+
private native void jniRegisterSegment(int segmentId, String path);
229230
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously);
230231
private native void jniLoadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously);
231232

ReactAndroid/src/main/java/com/facebook/react/bridge/JSBundleLoader.java

-15
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,6 @@ public String loadScript(CatalystInstanceImpl instance) {
9696
};
9797
}
9898

99-
/**
100-
* This loader is used to wrap other loaders and set js segments directory before executing
101-
* application script.
102-
*/
103-
public static JSBundleLoader createSplitBundlesLoader(
104-
final String jsSegmentsDirectory, final JSBundleLoader delegate) {
105-
return new JSBundleLoader() {
106-
@Override
107-
public String loadScript(CatalystInstanceImpl instance) {
108-
instance.setJsSegmentsDirectory(jsSegmentsDirectory);
109-
return delegate.loadScript(instance);
110-
}
111-
};
112-
}
113-
11499
/** Loads the script, returning the URL of the source it loaded. */
115100
public abstract String loadScript(CatalystInstanceImpl instance);
116101
}

ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp

+4-13
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ void CatalystInstanceImpl::registerNatives() {
100100
makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge),
101101
makeNativeMethod("jniExtendNativeModules", CatalystInstanceImpl::extendNativeModules),
102102
makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL),
103-
makeNativeMethod("jniSetJsSegmentsDirectory", CatalystInstanceImpl::jniSetJsSegmentsDirectory),
103+
makeNativeMethod("jniRegisterSegment", CatalystInstanceImpl::jniRegisterSegment),
104104
makeNativeMethod("jniLoadScriptFromAssets", CatalystInstanceImpl::jniLoadScriptFromAssets),
105105
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
106106
makeNativeMethod("jniCallJSFunction", CatalystInstanceImpl::jniCallJSFunction),
@@ -177,8 +177,8 @@ void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) {
177177
instance_->setSourceURL(sourceURL);
178178
}
179179

180-
void CatalystInstanceImpl::jniSetJsSegmentsDirectory(const std::string& directoryPath) {
181-
jsSegmentsDirectory_ = directoryPath;
180+
void CatalystInstanceImpl::jniRegisterSegment(int segmentId, const std::string& path) {
181+
instance_->registerBundle((uint32_t)segmentId, path);
182182
}
183183

184184
void CatalystInstanceImpl::jniLoadScriptFromAssets(
@@ -208,16 +208,7 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
208208
const std::string& sourceURL,
209209
bool loadSynchronously) {
210210
if (Instance::isIndexedRAMBundle(fileName.c_str())) {
211-
auto bundle = folly::make_unique<JSIndexedRAMBundle>(fileName.c_str());
212-
auto script = bundle->getStartupCode();
213-
auto registry = jsSegmentsDirectory_.empty()
214-
? RAMBundleRegistry::singleBundleRegistry(std::move(bundle))
215-
: RAMBundleRegistry::multipleBundlesRegistry(std::move(bundle), JSIndexedRAMBundle::buildFactory());
216-
instance_->loadRAMBundle(
217-
std::move(registry),
218-
std::move(script),
219-
sourceURL,
220-
loadSynchronously);
211+
instance_->loadRAMBundleFromFile(fileName, sourceURL, loadSynchronously);
221212
} else {
222213
std::unique_ptr<const JSBigFileString> script;
223214
RecoverableError::runRethrowingAsRecoverable<std::system_error>(

ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
6060
void jniSetSourceURL(const std::string& sourceURL);
6161

6262
/**
63-
* Sets the path to folder where additional bundles are located.
64-
* Needs to be invoked before "loadScript" methods are called.
63+
* Registers the file path of an additional JS segment by its ID.
64+
*
6565
*/
66-
void jniSetJsSegmentsDirectory(const std::string& directoryPath);
66+
void jniRegisterSegment(int segmentId, const std::string& path);
6767

6868
void jniLoadScriptFromAssets(jni::alias_ref<JAssetManager::javaobject> assetManager, const std::string& assetURL, bool loadSynchronously);
6969
void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL, bool loadSynchronously);
@@ -74,8 +74,6 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
7474
jlong getJavaScriptContext();
7575
void handleMemoryPressure(int pressureLevel);
7676

77-
std::string jsSegmentsDirectory_;
78-
7977
// This should be the only long-lived strong reference, but every C++ class
8078
// will have a weak reference.
8179
std::shared_ptr<Instance> instance_;

ReactCommon/cxxreact/Instance.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void Instance::loadRAMBundleFromFile(const std::string& sourcePath,
111111
bool loadSynchronously) {
112112
auto bundle = folly::make_unique<JSIndexedRAMBundle>(sourcePath.c_str());
113113
auto startupScript = bundle->getStartupCode();
114-
auto registry = RAMBundleRegistry::singleBundleRegistry(std::move(bundle));
114+
auto registry = RAMBundleRegistry::multipleBundlesRegistry(std::move(bundle), JSIndexedRAMBundle::buildFactory());
115115
loadRAMBundle(
116116
std::move(registry),
117117
std::move(startupScript),

0 commit comments

Comments
 (0)