Permalink
Browse files

Allow apps to provide JSBundleLoader of their choice

Reviewed By: tadeuzagallo

Differential Revision: D3522798

fbshipit-source-id: 90324e44a02ad78885ff3c2a33ba58d4ee6a021a
  • Loading branch information...
1 parent 2f73ca8 commit e632025917a000bf3fb5d296c6712a81435626d3 @michalgr michalgr committed with Facebook Github Bot 2 Jul 12, 2016
@@ -23,6 +23,7 @@
import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
+import com.facebook.react.cxxbridge.JSBundleLoader;
import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.devsupport.DevSupportManager;
import com.facebook.react.devsupport.RedBoxHandler;
@@ -184,6 +185,7 @@ public static Builder builder() {
protected final List<ReactPackage> mPackages = new ArrayList<>();
protected @Nullable String mJSBundleFile;
+ protected @Nullable JSBundleLoader mJSBundleLoader;
protected @Nullable String mJSMainModuleName;
protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener;
protected @Nullable Application mApplication;
@@ -225,6 +227,19 @@ public Builder setBundleAssetName(String bundleAssetName) {
*/
public Builder setJSBundleFile(String jsBundleFile) {
mJSBundleFile = jsBundleFile;
+ mJSBundleLoader = null;
+ return this;
+ }
+
+ /**
+ * Bundle loader to use when setting up JS environment. This supersedes
+ * prior invcations of {@link setJSBundleFile} and {@link setBundleAssetName}.
+ *
+ * Example: {@code JSBundleLoader.createFileLoader(application, bundleFile)}
+ */
+ public Builder setJSBundleLoader(JSBundleLoader jsBundleLoader) {
+ mJSBundleLoader = jsBundleLoader;
+ mJSBundleFile = null;
return this;
}
@@ -326,12 +341,20 @@ public Builder setUseOldBridge(boolean enable) {
* </ul>
*/
public ReactInstanceManager build() {
+ Assertions.assertNotNull(
+ mApplication,
+ "Application property has not been set with this builder");
+
Assertions.assertCondition(
- mUseDeveloperSupport || mJSBundleFile != null,
+ mJSBundleLoader == null || !mUseOldBridge,
+ "JSBundleLoader can't be used with the old bridge");
+
+ Assertions.assertCondition(
+ mUseDeveloperSupport || mJSBundleFile != null || mJSBundleLoader != null,
"JS Bundle File has to be provided when dev support is disabled");
Assertions.assertCondition(
- mJSMainModuleName != null || mJSBundleFile != null,
+ mJSMainModuleName != null || mJSBundleFile != null || mJSBundleLoader != null,
"Either MainModuleName or JS Bundle File needs to be provided");
if (mUIImplementationProvider == null) {
@@ -341,9 +364,7 @@ public ReactInstanceManager build() {
if (mUseOldBridge) {
return new ReactInstanceManagerImpl(
- Assertions.assertNotNull(
- mApplication,
- "Application property has not been set with this builder"),
+ mApplication,
mCurrentActivity,
mDefaultHardwareBackBtnHandler,
mJSBundleFile,
@@ -358,12 +379,11 @@ public ReactInstanceManager build() {
mRedBoxHandler);
} else {
return new XReactInstanceManagerImpl(
- Assertions.assertNotNull(
- mApplication,
- "Application property has not been set with this builder"),
+ mApplication,
mCurrentActivity,
mDefaultHardwareBackBtnHandler,
- mJSBundleFile,
+ (mJSBundleLoader == null && mJSBundleFile != null) ?
+ JSBundleLoader.createFileLoader(mApplication, mJSBundleFile) : mJSBundleLoader,
mJSMainModuleName,
mPackages,
mUseDeveloperSupport,
@@ -109,7 +109,7 @@
private @Nullable ReactContextInitAsyncTask mReactContextInitAsyncTask;
/* accessed from any thread */
- private @Nullable String mJSBundleFile; /* path to JS bundle on file system */
+ private final @Nullable JSBundleLoader mBundleLoader; /* path to JS bundle on file system */
private final @Nullable String mJSMainModuleName; /* path to JS bundle root on packager server */
private final List<ReactPackage> mPackages;
private final DevSupportManager mDevSupportManager;
@@ -275,7 +275,7 @@ public T get() throws Exception {
Context applicationContext,
@Nullable Activity currentActivity,
@Nullable DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler,
- @Nullable String jsBundleFile,
+ @Nullable JSBundleLoader bundleLoader,
@Nullable String jsMainModuleName,
List<ReactPackage> packages,
boolean useDeveloperSupport,
@@ -295,7 +295,7 @@ public T get() throws Exception {
mApplicationContext = applicationContext;
mCurrentActivity = currentActivity;
mDefaultBackButtonImpl = defaultHardwareBackBtnHandler;
- mJSBundleFile = jsBundleFile;
+ mBundleLoader = bundleLoader;
mJSMainModuleName = jsMainModuleName;
mPackages = packages;
mUseDeveloperSupport = useDeveloperSupport;
@@ -382,7 +382,7 @@ private void recreateReactContextInBackgroundInner() {
// If there is a up-to-date bundle downloaded from server,
// with remote JS debugging disabled, always use that.
onJSBundleLoadedFromServer();
- } else if (mJSBundleFile == null) {
+ } else if (mBundleLoader == null) {
mDevSupportManager.handleReloadJS();
} else {
mDevSupportManager.isPackagerRunning(
@@ -398,7 +398,7 @@ public void run() {
} else {
// If dev server is down, disable the remote JS debugging.
devSettings.setRemoteJSDebugEnabled(false);
- recreateReactContextInBackgroundFromBundleFile();
+ recreateReactContextInBackgroundFromBundleLoader();
}
}
});
@@ -408,16 +408,13 @@ public void run() {
return;
}
- recreateReactContextInBackgroundFromBundleFile();
+ recreateReactContextInBackgroundFromBundleLoader();
}
- private void recreateReactContextInBackgroundFromBundleFile() {
- boolean useLazyBundle = mJSCConfig.getConfigMap().hasKey("useLazyBundle") ?
- mJSCConfig.getConfigMap().getBoolean("useLazyBundle") : false;
-
+ private void recreateReactContextInBackgroundFromBundleLoader() {
recreateReactContextInBackground(
new JSCJavaScriptExecutor.Factory(mJSCConfig.getConfigMap()),
- JSBundleLoader.createFileLoader(mApplicationContext, mJSBundleFile, useLazyBundle));
+ mBundleLoader);
}
/**

0 comments on commit e632025

Please sign in to comment.