Skip to content

Commit b5651d9

Browse files
fromcelticparkfacebook-github-bot
authored andcommitted
Add "setJsBundlesDirectory" method to CatalystInstanceImpl
Differential Revision: D6042493 fbshipit-source-id: 950c6d6bdc2e0b62b14c9bcfc86233159a002c67
1 parent 21917ab commit b5651d9

File tree

8 files changed

+42
-24
lines changed

8 files changed

+42
-24
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ private native void initializeBridge(
210210
jniSetSourceURL(remoteURL);
211211
}
212212

213+
/* package */ void setJsBundlesDirectory(String directoryPath) {
214+
jniSetJsBundlesDirectory(directoryPath);
215+
}
216+
213217
/* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) {
214218
mSourceURL = assetURL;
215219
jniLoadScriptFromAssets(assetManager, assetURL, loadSynchronously);
@@ -221,6 +225,7 @@ private native void initializeBridge(
221225
}
222226

223227
private native void jniSetSourceURL(String sourceURL);
228+
private native void jniSetJsBundlesDirectory(String directoryPath);
224229
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously);
225230
private native void jniLoadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously);
226231

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
package com.facebook.react.bridge;
1111

1212
import android.content.Context;
13-
1413
import com.facebook.react.common.DebugServerException;
1514

1615
/**
@@ -98,7 +97,20 @@ public String loadScript(CatalystInstanceImpl instance) {
9897
}
9998

10099
/**
101-
* Loads the script, returning the URL of the source it loaded.
100+
* This loader is used to wrap other loaders and set js bundles directory before executing
101+
* application script.
102102
*/
103+
public static JSBundleLoader createSplitBundlesLoader(
104+
final String jsBundlesDirectory, final JSBundleLoader delegate) {
105+
return new JSBundleLoader() {
106+
@Override
107+
public String loadScript(CatalystInstanceImpl instance) {
108+
instance.setJsBundlesDirectory(jsBundlesDirectory);
109+
return delegate.loadScript(instance);
110+
}
111+
};
112+
}
113+
114+
/** Loads the script, returning the URL of the source it loaded. */
103115
public abstract String loadScript(CatalystInstanceImpl instance);
104116
}

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
#include <cxxreact/JSBigString.h>
1111
#include <cxxreact/JSBundleType.h>
1212
#include <cxxreact/JSIndexedRAMBundle.h>
13+
#include <cxxreact/JSIndexedRAMBundleRegistry.h>
1314
#include <cxxreact/MethodCall.h>
1415
#include <cxxreact/RecoverableError.h>
1516
#include <cxxreact/ModuleRegistry.h>
16-
#include <cxxreact/RAMBundleRegistry.h>
1717
#include <fb/log.h>
1818
#include <folly/dynamic.h>
1919
#include <folly/Memory.h>
@@ -101,6 +101,7 @@ void CatalystInstanceImpl::registerNatives() {
101101
makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge),
102102
makeNativeMethod("jniExtendNativeModules", CatalystInstanceImpl::extendNativeModules),
103103
makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL),
104+
makeNativeMethod("jniSetJsBundlesDirectory", CatalystInstanceImpl::jniSetJsBundlesDirectory),
104105
makeNativeMethod("jniLoadScriptFromAssets", CatalystInstanceImpl::jniLoadScriptFromAssets),
105106
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
106107
makeNativeMethod("jniCallJSFunction", CatalystInstanceImpl::jniCallJSFunction),
@@ -177,6 +178,10 @@ void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) {
177178
instance_->setSourceURL(sourceURL);
178179
}
179180

181+
void CatalystInstanceImpl::jniSetJsBundlesDirectory(const std::string& directoryPath) {
182+
jsBundlesDirectory_ = directoryPath;
183+
}
184+
180185
void CatalystInstanceImpl::jniLoadScriptFromAssets(
181186
jni::alias_ref<JAssetManager::javaobject> assetManager,
182187
const std::string& assetURL,
@@ -188,7 +193,9 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
188193
auto script = loadScriptFromAssets(manager, sourceURL);
189194
if (JniJSModulesUnbundle::isUnbundle(manager, sourceURL)) {
190195
auto bundle = JniJSModulesUnbundle::fromEntryFile(manager, sourceURL);
191-
auto registry = folly::make_unique<JniRAMBundleRegistry>(std::move(bundle), manager, sourceURL);
196+
auto registry = jsBundlesDirectory_.empty()
197+
? folly::make_unique<RAMBundleRegistry>(std::move(bundle))
198+
: folly::make_unique<JniRAMBundleRegistry>(std::move(bundle), manager, sourceURL);
192199
instance_->loadRAMBundle(
193200
std::move(registry),
194201
std::move(script),
@@ -218,7 +225,9 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
218225
if (isIndexedRAMBundle(zFileName)) {
219226
auto bundle = folly::make_unique<JSIndexedRAMBundle>(zFileName);
220227
auto startupScript = bundle->getStartupCode();
221-
auto registry = folly::make_unique<RAMBundleRegistry>(std::move(bundle));
228+
auto registry = jsBundlesDirectory_.empty()
229+
? folly::make_unique<RAMBundleRegistry>(std::move(bundle))
230+
: folly::make_unique<JSIndexedRAMBundleRegistry>(std::move(bundle), jsBundlesDirectory_);
222231
instance_->loadRAMBundle(
223232
std::move(registry),
224233
std::move(startupScript),

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
6161
*/
6262
void jniSetSourceURL(const std::string& sourceURL);
6363

64+
/**
65+
* Sets the path to folder where additional bundles are located.
66+
* Needs to be invoked before "loadScript" methods are called.
67+
*/
68+
void jniSetJsBundlesDirectory(const std::string& directoryPath);
69+
6470
void jniLoadScriptFromAssets(jni::alias_ref<JAssetManager::javaobject> assetManager, const std::string& assetURL, bool loadSynchronously);
6571
void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL, bool loadSynchronously);
6672
void jniCallJSFunction(std::string module, std::string method, NativeArray* arguments);
@@ -70,6 +76,8 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
7076
jlong getJavaScriptContext();
7177
void handleMemoryPressure(int pressureLevel);
7278

79+
std::string jsBundlesDirectory_;
80+
7381
// This should be the only long-lived strong reference, but every C++ class
7482
// will have a weak reference.
7583
std::shared_ptr<Instance> instance_;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
namespace facebook {
1111
namespace react {
1212

13-
JniRAMBundleRegistry::JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile) :
13+
JniRAMBundleRegistry::JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& baseDirectoryPath) :
1414
RAMBundleRegistry(std::move(mainBundle)),
1515
m_assetManager(assetManager),
16-
m_baseDirectoryPath(jsBundlesDir(entryFile)) {}
16+
m_baseDirectoryPath(baseDirectoryPath) {}
1717

1818
std::unique_ptr<JSModulesUnbundle> JniRAMBundleRegistry::bundleById(uint32_t index) const {
1919
std::string bundlePathById = m_baseDirectoryPath + folly::to<std::string>(index) + "/js-modules/";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace react {
88

99
class JniRAMBundleRegistry : public RAMBundleRegistry {
1010
public:
11-
JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile);
11+
JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& baseDirectoryPath);
1212

1313
protected:
1414
virtual std::unique_ptr<JSModulesUnbundle> bundleById(uint32_t index) const override;

ReactCommon/cxxreact/RAMBundleRegistry.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,5 @@ JSModulesUnbundle *RAMBundleRegistry::getBundle(uint32_t bundleId) const {
2525
return m_bundles.at(bundleId).get();
2626
}
2727

28-
std::string RAMBundleRegistry::jsBundlesDir(std::string entryFile) {
29-
char *pEntryFile = const_cast<char *>(entryFile.c_str());
30-
std::string dir = dirname(pEntryFile);
31-
std::string entryName = basename(pEntryFile);
32-
33-
std::size_t dotPosition = entryName.find(".");
34-
if (dotPosition != std::string::npos) {
35-
entryName.erase(dotPosition, std::string::npos);
36-
}
37-
38-
std::string path = "js-bundles/" + entryName + "/";
39-
// android's asset manager does not work with paths that start with a dot
40-
return dir == "." ? path : dir + "/" + path;
41-
}
42-
4328
} // namespace react
4429
} // namespace facebook

ReactCommon/cxxreact/RAMBundleRegistry.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class RN_EXPORT RAMBundleRegistry : noncopyable {
2828
JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId);
2929
virtual ~RAMBundleRegistry() {};
3030
protected:
31-
std::string jsBundlesDir(std::string entryFile);
3231
virtual std::unique_ptr<JSModulesUnbundle> bundleById(uint32_t index) const {
3332
throw std::runtime_error("Please, override this method in a subclass to support multiple RAM bundles.");
3433
}

0 commit comments

Comments
 (0)