Add separate JSBunldeLoader for assets
Reviewed By: mhorowitz Differential Revision: D3735897 fbshipit-source-id: 990d45e9cb40a9afce1df8f8fd0b73c62e13158a
This commit is contained in:
parent
96de161304
commit
0c2fdf4b6a
|
@ -73,7 +73,7 @@ public class ReactTestHelper {
|
||||||
.setJSExecutor(executor)
|
.setJSExecutor(executor)
|
||||||
.setRegistry(mNativeModuleRegistryBuilder.build())
|
.setRegistry(mNativeModuleRegistryBuilder.build())
|
||||||
.setJSModuleRegistry(mJSModuleRegistryBuilder.build())
|
.setJSModuleRegistry(mJSModuleRegistryBuilder.build())
|
||||||
.setJSBundleLoader(JSBundleLoader.createFileLoader(
|
.setJSBundleLoader(JSBundleLoader.createAssetLoader(
|
||||||
mContext,
|
mContext,
|
||||||
"assets://AndroidTestBundle.js"))
|
"assets://AndroidTestBundle.js"))
|
||||||
.setNativeModuleCallExceptionHandler(
|
.setNativeModuleCallExceptionHandler(
|
||||||
|
|
|
@ -220,7 +220,7 @@ public abstract class ReactInstanceManager {
|
||||||
|
|
||||||
protected final List<ReactPackage> mPackages = new ArrayList<>();
|
protected final List<ReactPackage> mPackages = new ArrayList<>();
|
||||||
|
|
||||||
protected @Nullable String mJSBundleFile;
|
protected @Nullable String mJSBundleAssetUrl;
|
||||||
protected @Nullable JSBundleLoader mJSBundleLoader;
|
protected @Nullable JSBundleLoader mJSBundleLoader;
|
||||||
protected @Nullable String mJSMainModuleName;
|
protected @Nullable String mJSMainModuleName;
|
||||||
protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener;
|
protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener;
|
||||||
|
@ -252,7 +252,9 @@ public abstract class ReactInstanceManager {
|
||||||
* Example: {@code "index.android.js"}
|
* Example: {@code "index.android.js"}
|
||||||
*/
|
*/
|
||||||
public Builder setBundleAssetName(String bundleAssetName) {
|
public Builder setBundleAssetName(String bundleAssetName) {
|
||||||
return this.setJSBundleFile(bundleAssetName == null ? null : "assets://" + bundleAssetName);
|
mJSBundleAssetUrl = (bundleAssetName == null ? null : "assets://" + bundleAssetName);
|
||||||
|
mJSBundleLoader = null;
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -261,10 +263,13 @@ public abstract class ReactInstanceManager {
|
||||||
* Example: {@code "assets://index.android.js" or "/sdcard/main.jsbundle"}
|
* Example: {@code "assets://index.android.js" or "/sdcard/main.jsbundle"}
|
||||||
*/
|
*/
|
||||||
public Builder setJSBundleFile(String jsBundleFile) {
|
public Builder setJSBundleFile(String jsBundleFile) {
|
||||||
mJSBundleFile = jsBundleFile;
|
if (jsBundleFile.startsWith("assets://")) {
|
||||||
|
mJSBundleAssetUrl = jsBundleFile;
|
||||||
mJSBundleLoader = null;
|
mJSBundleLoader = null;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
return setJSBundleLoader(JSBundleLoader.createFileLoader(jsBundleFile));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Bundle loader to use when setting up JS environment. This supersedes
|
* Bundle loader to use when setting up JS environment. This supersedes
|
||||||
|
@ -274,7 +279,7 @@ public abstract class ReactInstanceManager {
|
||||||
*/
|
*/
|
||||||
public Builder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
|
public Builder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
|
||||||
mJSBundleLoader = jsBundleLoader;
|
mJSBundleLoader = jsBundleLoader;
|
||||||
mJSBundleFile = null;
|
mJSBundleAssetUrl = null;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -376,11 +381,11 @@ public abstract class ReactInstanceManager {
|
||||||
"Application property has not been set with this builder");
|
"Application property has not been set with this builder");
|
||||||
|
|
||||||
Assertions.assertCondition(
|
Assertions.assertCondition(
|
||||||
mUseDeveloperSupport || mJSBundleFile != null || mJSBundleLoader != null,
|
mUseDeveloperSupport || mJSBundleAssetUrl != null || mJSBundleLoader != null,
|
||||||
"JS Bundle File has to be provided when dev support is disabled");
|
"JS Bundle File or Asset URL has to be provided when dev support is disabled");
|
||||||
|
|
||||||
Assertions.assertCondition(
|
Assertions.assertCondition(
|
||||||
mJSMainModuleName != null || mJSBundleFile != null || mJSBundleLoader != null,
|
mJSMainModuleName != null || mJSBundleAssetUrl != null || mJSBundleLoader != null,
|
||||||
"Either MainModuleName or JS Bundle File needs to be provided");
|
"Either MainModuleName or JS Bundle File needs to be provided");
|
||||||
|
|
||||||
if (mUIImplementationProvider == null) {
|
if (mUIImplementationProvider == null) {
|
||||||
|
@ -392,8 +397,8 @@ public abstract class ReactInstanceManager {
|
||||||
mApplication,
|
mApplication,
|
||||||
mCurrentActivity,
|
mCurrentActivity,
|
||||||
mDefaultHardwareBackBtnHandler,
|
mDefaultHardwareBackBtnHandler,
|
||||||
(mJSBundleLoader == null && mJSBundleFile != null) ?
|
(mJSBundleLoader == null && mJSBundleAssetUrl != null) ?
|
||||||
JSBundleLoader.createFileLoader(mApplication, mJSBundleFile) : mJSBundleLoader,
|
JSBundleLoader.createAssetLoader(mApplication, mJSBundleAssetUrl) : mJSBundleLoader,
|
||||||
mJSMainModuleName,
|
mJSMainModuleName,
|
||||||
mPackages,
|
mPackages,
|
||||||
mUseDeveloperSupport,
|
mUseDeveloperSupport,
|
||||||
|
|
|
@ -24,20 +24,34 @@ public abstract class JSBundleLoader {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This loader is recommended one for release version of your app. In that case local JS executor
|
* This loader is recommended one for release version of your app. In that case local JS executor
|
||||||
* should be used. JS bundle will be read from assets directory in native code to save on passing
|
* should be used. JS bundle will be read from assets in native code to save on passing large
|
||||||
* large strings from java to native memory.
|
* strings from java to native memory.
|
||||||
*/
|
*/
|
||||||
public static JSBundleLoader createFileLoader(
|
public static JSBundleLoader createAssetLoader(
|
||||||
final Context context,
|
final Context context,
|
||||||
final String fileName) {
|
final String assetUrl) {
|
||||||
return new JSBundleLoader() {
|
return new JSBundleLoader() {
|
||||||
@Override
|
@Override
|
||||||
public void loadScript(CatalystInstanceImpl instance) {
|
public void loadScript(CatalystInstanceImpl instance) {
|
||||||
if (fileName.startsWith("assets://")) {
|
instance.loadScriptFromAssets(context.getAssets(), assetUrl);
|
||||||
instance.loadScriptFromAssets(context.getAssets(), fileName);
|
|
||||||
} else {
|
|
||||||
instance.loadScriptFromFile(fileName, fileName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getSourceUrl() {
|
||||||
|
return assetUrl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This loader loads bundle from file system. The bundle will be read in native code to save on
|
||||||
|
* passing large strings from java to native memorory.
|
||||||
|
*/
|
||||||
|
public static JSBundleLoader createFileLoader(final String fileName) {
|
||||||
|
return new JSBundleLoader() {
|
||||||
|
@Override
|
||||||
|
public void loadScript(CatalystInstanceImpl instance) {
|
||||||
|
instance.loadScriptFromFile(fileName, fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue