Add separate JSBunldeLoader for assets

Reviewed By: mhorowitz

Differential Revision: D3735897

fbshipit-source-id: 990d45e9cb40a9afce1df8f8fd0b73c62e13158a
This commit is contained in:
Michał Gregorczyk 2016-09-06 19:39:27 -07:00 committed by Facebook Github Bot 8
parent 96de161304
commit 0c2fdf4b6a
3 changed files with 40 additions and 21 deletions

View File

@ -73,7 +73,7 @@ public class ReactTestHelper {
.setJSExecutor(executor)
.setRegistry(mNativeModuleRegistryBuilder.build())
.setJSModuleRegistry(mJSModuleRegistryBuilder.build())
.setJSBundleLoader(JSBundleLoader.createFileLoader(
.setJSBundleLoader(JSBundleLoader.createAssetLoader(
mContext,
"assets://AndroidTestBundle.js"))
.setNativeModuleCallExceptionHandler(

View File

@ -220,7 +220,7 @@ public abstract class ReactInstanceManager {
protected final List<ReactPackage> mPackages = new ArrayList<>();
protected @Nullable String mJSBundleFile;
protected @Nullable String mJSBundleAssetUrl;
protected @Nullable JSBundleLoader mJSBundleLoader;
protected @Nullable String mJSMainModuleName;
protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener;
@ -252,7 +252,9 @@ public abstract class ReactInstanceManager {
* Example: {@code "index.android.js"}
*/
public Builder setBundleAssetName(String bundleAssetName) {
return this.setJSBundleFile(bundleAssetName == null ? null : "assets://" + bundleAssetName);
mJSBundleAssetUrl = (bundleAssetName == null ? null : "assets://" + bundleAssetName);
mJSBundleLoader = null;
return this;
}
/**
@ -261,9 +263,12 @@ public abstract class ReactInstanceManager {
* Example: {@code "assets://index.android.js" or "/sdcard/main.jsbundle"}
*/
public Builder setJSBundleFile(String jsBundleFile) {
mJSBundleFile = jsBundleFile;
mJSBundleLoader = null;
return this;
if (jsBundleFile.startsWith("assets://")) {
mJSBundleAssetUrl = jsBundleFile;
mJSBundleLoader = null;
return this;
}
return setJSBundleLoader(JSBundleLoader.createFileLoader(jsBundleFile));
}
/**
@ -274,7 +279,7 @@ public abstract class ReactInstanceManager {
*/
public Builder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
mJSBundleLoader = jsBundleLoader;
mJSBundleFile = null;
mJSBundleAssetUrl = null;
return this;
}
@ -376,11 +381,11 @@ public abstract class ReactInstanceManager {
"Application property has not been set with this builder");
Assertions.assertCondition(
mUseDeveloperSupport || mJSBundleFile != null || mJSBundleLoader != null,
"JS Bundle File has to be provided when dev support is disabled");
mUseDeveloperSupport || mJSBundleAssetUrl != null || mJSBundleLoader != null,
"JS Bundle File or Asset URL has to be provided when dev support is disabled");
Assertions.assertCondition(
mJSMainModuleName != null || mJSBundleFile != null || mJSBundleLoader != null,
mJSMainModuleName != null || mJSBundleAssetUrl != null || mJSBundleLoader != null,
"Either MainModuleName or JS Bundle File needs to be provided");
if (mUIImplementationProvider == null) {
@ -392,8 +397,8 @@ public abstract class ReactInstanceManager {
mApplication,
mCurrentActivity,
mDefaultHardwareBackBtnHandler,
(mJSBundleLoader == null && mJSBundleFile != null) ?
JSBundleLoader.createFileLoader(mApplication, mJSBundleFile) : mJSBundleLoader,
(mJSBundleLoader == null && mJSBundleAssetUrl != null) ?
JSBundleLoader.createAssetLoader(mApplication, mJSBundleAssetUrl) : mJSBundleLoader,
mJSMainModuleName,
mPackages,
mUseDeveloperSupport,

View File

@ -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
* should be used. JS bundle will be read from assets directory in native code to save on passing
* large strings from java to native memory.
* should be used. JS bundle will be read from assets in native code to save on passing large
* strings from java to native memory.
*/
public static JSBundleLoader createFileLoader(
public static JSBundleLoader createAssetLoader(
final Context context,
final String fileName) {
final String assetUrl) {
return new JSBundleLoader() {
@Override
public void loadScript(CatalystInstanceImpl instance) {
if (fileName.startsWith("assets://")) {
instance.loadScriptFromAssets(context.getAssets(), fileName);
} else {
instance.loadScriptFromFile(fileName, fileName);
}
instance.loadScriptFromAssets(context.getAssets(), assetUrl);
}
@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