mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-23 20:01:01 +08:00
Add "setJsBundlesDirectory" method to CatalystInstanceImpl
Differential Revision: D6042493 fbshipit-source-id: 950c6d6bdc2e0b62b14c9bcfc86233159a002c67
This commit is contained in:
committed by
Facebook Github Bot
parent
21917ab7bf
commit
b5651d945c
@@ -210,6 +210,10 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
jniSetSourceURL(remoteURL);
|
||||
}
|
||||
|
||||
/* package */ void setJsBundlesDirectory(String directoryPath) {
|
||||
jniSetJsBundlesDirectory(directoryPath);
|
||||
}
|
||||
|
||||
/* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) {
|
||||
mSourceURL = assetURL;
|
||||
jniLoadScriptFromAssets(assetManager, assetURL, loadSynchronously);
|
||||
@@ -221,6 +225,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
}
|
||||
|
||||
private native void jniSetSourceURL(String sourceURL);
|
||||
private native void jniSetJsBundlesDirectory(String directoryPath);
|
||||
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously);
|
||||
private native void jniLoadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously);
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.facebook.react.common.DebugServerException;
|
||||
|
||||
/**
|
||||
@@ -98,7 +97,20 @@ public abstract class JSBundleLoader {
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the script, returning the URL of the source it loaded.
|
||||
* This loader is used to wrap other loaders and set js bundles directory before executing
|
||||
* application script.
|
||||
*/
|
||||
public static JSBundleLoader createSplitBundlesLoader(
|
||||
final String jsBundlesDirectory, final JSBundleLoader delegate) {
|
||||
return new JSBundleLoader() {
|
||||
@Override
|
||||
public String loadScript(CatalystInstanceImpl instance) {
|
||||
instance.setJsBundlesDirectory(jsBundlesDirectory);
|
||||
return delegate.loadScript(instance);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** Loads the script, returning the URL of the source it loaded. */
|
||||
public abstract String loadScript(CatalystInstanceImpl instance);
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
#include <cxxreact/JSBigString.h>
|
||||
#include <cxxreact/JSBundleType.h>
|
||||
#include <cxxreact/JSIndexedRAMBundle.h>
|
||||
#include <cxxreact/JSIndexedRAMBundleRegistry.h>
|
||||
#include <cxxreact/MethodCall.h>
|
||||
#include <cxxreact/RecoverableError.h>
|
||||
#include <cxxreact/ModuleRegistry.h>
|
||||
#include <cxxreact/RAMBundleRegistry.h>
|
||||
#include <fb/log.h>
|
||||
#include <folly/dynamic.h>
|
||||
#include <folly/Memory.h>
|
||||
@@ -101,6 +101,7 @@ void CatalystInstanceImpl::registerNatives() {
|
||||
makeNativeMethod("initializeBridge", CatalystInstanceImpl::initializeBridge),
|
||||
makeNativeMethod("jniExtendNativeModules", CatalystInstanceImpl::extendNativeModules),
|
||||
makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL),
|
||||
makeNativeMethod("jniSetJsBundlesDirectory", CatalystInstanceImpl::jniSetJsBundlesDirectory),
|
||||
makeNativeMethod("jniLoadScriptFromAssets", CatalystInstanceImpl::jniLoadScriptFromAssets),
|
||||
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
|
||||
makeNativeMethod("jniCallJSFunction", CatalystInstanceImpl::jniCallJSFunction),
|
||||
@@ -177,6 +178,10 @@ void CatalystInstanceImpl::jniSetSourceURL(const std::string& sourceURL) {
|
||||
instance_->setSourceURL(sourceURL);
|
||||
}
|
||||
|
||||
void CatalystInstanceImpl::jniSetJsBundlesDirectory(const std::string& directoryPath) {
|
||||
jsBundlesDirectory_ = directoryPath;
|
||||
}
|
||||
|
||||
void CatalystInstanceImpl::jniLoadScriptFromAssets(
|
||||
jni::alias_ref<JAssetManager::javaobject> assetManager,
|
||||
const std::string& assetURL,
|
||||
@@ -188,7 +193,9 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets(
|
||||
auto script = loadScriptFromAssets(manager, sourceURL);
|
||||
if (JniJSModulesUnbundle::isUnbundle(manager, sourceURL)) {
|
||||
auto bundle = JniJSModulesUnbundle::fromEntryFile(manager, sourceURL);
|
||||
auto registry = folly::make_unique<JniRAMBundleRegistry>(std::move(bundle), manager, sourceURL);
|
||||
auto registry = jsBundlesDirectory_.empty()
|
||||
? folly::make_unique<RAMBundleRegistry>(std::move(bundle))
|
||||
: folly::make_unique<JniRAMBundleRegistry>(std::move(bundle), manager, sourceURL);
|
||||
instance_->loadRAMBundle(
|
||||
std::move(registry),
|
||||
std::move(script),
|
||||
@@ -218,7 +225,9 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
|
||||
if (isIndexedRAMBundle(zFileName)) {
|
||||
auto bundle = folly::make_unique<JSIndexedRAMBundle>(zFileName);
|
||||
auto startupScript = bundle->getStartupCode();
|
||||
auto registry = folly::make_unique<RAMBundleRegistry>(std::move(bundle));
|
||||
auto registry = jsBundlesDirectory_.empty()
|
||||
? folly::make_unique<RAMBundleRegistry>(std::move(bundle))
|
||||
: folly::make_unique<JSIndexedRAMBundleRegistry>(std::move(bundle), jsBundlesDirectory_);
|
||||
instance_->loadRAMBundle(
|
||||
std::move(registry),
|
||||
std::move(startupScript),
|
||||
|
||||
@@ -61,6 +61,12 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
|
||||
*/
|
||||
void jniSetSourceURL(const std::string& sourceURL);
|
||||
|
||||
/**
|
||||
* Sets the path to folder where additional bundles are located.
|
||||
* Needs to be invoked before "loadScript" methods are called.
|
||||
*/
|
||||
void jniSetJsBundlesDirectory(const std::string& directoryPath);
|
||||
|
||||
void jniLoadScriptFromAssets(jni::alias_ref<JAssetManager::javaobject> assetManager, const std::string& assetURL, bool loadSynchronously);
|
||||
void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL, bool loadSynchronously);
|
||||
void jniCallJSFunction(std::string module, std::string method, NativeArray* arguments);
|
||||
@@ -70,6 +76,8 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
|
||||
jlong getJavaScriptContext();
|
||||
void handleMemoryPressure(int pressureLevel);
|
||||
|
||||
std::string jsBundlesDirectory_;
|
||||
|
||||
// This should be the only long-lived strong reference, but every C++ class
|
||||
// will have a weak reference.
|
||||
std::shared_ptr<Instance> instance_;
|
||||
|
||||
@@ -10,10 +10,10 @@
|
||||
namespace facebook {
|
||||
namespace react {
|
||||
|
||||
JniRAMBundleRegistry::JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile) :
|
||||
JniRAMBundleRegistry::JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& baseDirectoryPath) :
|
||||
RAMBundleRegistry(std::move(mainBundle)),
|
||||
m_assetManager(assetManager),
|
||||
m_baseDirectoryPath(jsBundlesDir(entryFile)) {}
|
||||
m_baseDirectoryPath(baseDirectoryPath) {}
|
||||
|
||||
std::unique_ptr<JSModulesUnbundle> JniRAMBundleRegistry::bundleById(uint32_t index) const {
|
||||
std::string bundlePathById = m_baseDirectoryPath + folly::to<std::string>(index) + "/js-modules/";
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace react {
|
||||
|
||||
class JniRAMBundleRegistry : public RAMBundleRegistry {
|
||||
public:
|
||||
JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& entryFile);
|
||||
JniRAMBundleRegistry(std::unique_ptr<JSModulesUnbundle> mainBundle, AAssetManager *assetManager, const std::string& baseDirectoryPath);
|
||||
|
||||
protected:
|
||||
virtual std::unique_ptr<JSModulesUnbundle> bundleById(uint32_t index) const override;
|
||||
|
||||
Reference in New Issue
Block a user