Allow apps to provide JSBundleLoader of their choice

Reviewed By: tadeuzagallo

Differential Revision: D3522798

fbshipit-source-id: 90324e44a02ad78885ff3c2a33ba58d4ee6a021a
This commit is contained in:
Michał Gregorczyk 2016-07-12 08:03:04 -07:00 committed by Facebook Github Bot 2
parent 2f73ca8f76
commit e632025917
2 changed files with 37 additions and 20 deletions

View File

@ -23,6 +23,7 @@ import com.facebook.react.bridge.NativeModuleCallExceptionHandler;
import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener; import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener;
import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContext;
import com.facebook.react.cxxbridge.JSBundleLoader;
import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.devsupport.DevSupportManager; import com.facebook.react.devsupport.DevSupportManager;
import com.facebook.react.devsupport.RedBoxHandler; import com.facebook.react.devsupport.RedBoxHandler;
@ -184,6 +185,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 mJSBundleFile;
protected @Nullable JSBundleLoader mJSBundleLoader;
protected @Nullable String mJSMainModuleName; protected @Nullable String mJSMainModuleName;
protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener; protected @Nullable NotThreadSafeBridgeIdleDebugListener mBridgeIdleDebugListener;
protected @Nullable Application mApplication; protected @Nullable Application mApplication;
@ -225,6 +227,19 @@ public abstract class ReactInstanceManager {
*/ */
public Builder setJSBundleFile(String jsBundleFile) { public Builder setJSBundleFile(String jsBundleFile) {
mJSBundleFile = 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; return this;
} }
@ -326,12 +341,20 @@ public abstract class ReactInstanceManager {
* </ul> * </ul>
*/ */
public ReactInstanceManager build() { public ReactInstanceManager build() {
Assertions.assertNotNull(
mApplication,
"Application property has not been set with this builder");
Assertions.assertCondition( 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"); "JS Bundle File has to be provided when dev support is disabled");
Assertions.assertCondition( Assertions.assertCondition(
mJSMainModuleName != null || mJSBundleFile != null, mJSMainModuleName != null || mJSBundleFile != 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) {
@ -341,9 +364,7 @@ public abstract class ReactInstanceManager {
if (mUseOldBridge) { if (mUseOldBridge) {
return new ReactInstanceManagerImpl( return new ReactInstanceManagerImpl(
Assertions.assertNotNull( mApplication,
mApplication,
"Application property has not been set with this builder"),
mCurrentActivity, mCurrentActivity,
mDefaultHardwareBackBtnHandler, mDefaultHardwareBackBtnHandler,
mJSBundleFile, mJSBundleFile,
@ -358,12 +379,11 @@ public abstract class ReactInstanceManager {
mRedBoxHandler); mRedBoxHandler);
} else { } else {
return new XReactInstanceManagerImpl( return new XReactInstanceManagerImpl(
Assertions.assertNotNull( mApplication,
mApplication,
"Application property has not been set with this builder"),
mCurrentActivity, mCurrentActivity,
mDefaultHardwareBackBtnHandler, mDefaultHardwareBackBtnHandler,
mJSBundleFile, (mJSBundleLoader == null && mJSBundleFile != null) ?
JSBundleLoader.createFileLoader(mApplication, mJSBundleFile) : mJSBundleLoader,
mJSMainModuleName, mJSMainModuleName,
mPackages, mPackages,
mUseDeveloperSupport, mUseDeveloperSupport,

View File

@ -109,7 +109,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
private @Nullable ReactContextInitAsyncTask mReactContextInitAsyncTask; private @Nullable ReactContextInitAsyncTask mReactContextInitAsyncTask;
/* accessed from any thread */ /* 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 @Nullable String mJSMainModuleName; /* path to JS bundle root on packager server */
private final List<ReactPackage> mPackages; private final List<ReactPackage> mPackages;
private final DevSupportManager mDevSupportManager; private final DevSupportManager mDevSupportManager;
@ -275,7 +275,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
Context applicationContext, Context applicationContext,
@Nullable Activity currentActivity, @Nullable Activity currentActivity,
@Nullable DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler, @Nullable DefaultHardwareBackBtnHandler defaultHardwareBackBtnHandler,
@Nullable String jsBundleFile, @Nullable JSBundleLoader bundleLoader,
@Nullable String jsMainModuleName, @Nullable String jsMainModuleName,
List<ReactPackage> packages, List<ReactPackage> packages,
boolean useDeveloperSupport, boolean useDeveloperSupport,
@ -295,7 +295,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
mApplicationContext = applicationContext; mApplicationContext = applicationContext;
mCurrentActivity = currentActivity; mCurrentActivity = currentActivity;
mDefaultBackButtonImpl = defaultHardwareBackBtnHandler; mDefaultBackButtonImpl = defaultHardwareBackBtnHandler;
mJSBundleFile = jsBundleFile; mBundleLoader = bundleLoader;
mJSMainModuleName = jsMainModuleName; mJSMainModuleName = jsMainModuleName;
mPackages = packages; mPackages = packages;
mUseDeveloperSupport = useDeveloperSupport; mUseDeveloperSupport = useDeveloperSupport;
@ -382,7 +382,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
// If there is a up-to-date bundle downloaded from server, // If there is a up-to-date bundle downloaded from server,
// with remote JS debugging disabled, always use that. // with remote JS debugging disabled, always use that.
onJSBundleLoadedFromServer(); onJSBundleLoadedFromServer();
} else if (mJSBundleFile == null) { } else if (mBundleLoader == null) {
mDevSupportManager.handleReloadJS(); mDevSupportManager.handleReloadJS();
} else { } else {
mDevSupportManager.isPackagerRunning( mDevSupportManager.isPackagerRunning(
@ -398,7 +398,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
} else { } else {
// If dev server is down, disable the remote JS debugging. // If dev server is down, disable the remote JS debugging.
devSettings.setRemoteJSDebugEnabled(false); devSettings.setRemoteJSDebugEnabled(false);
recreateReactContextInBackgroundFromBundleFile(); recreateReactContextInBackgroundFromBundleLoader();
} }
} }
}); });
@ -408,16 +408,13 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE;
return; return;
} }
recreateReactContextInBackgroundFromBundleFile(); recreateReactContextInBackgroundFromBundleLoader();
} }
private void recreateReactContextInBackgroundFromBundleFile() { private void recreateReactContextInBackgroundFromBundleLoader() {
boolean useLazyBundle = mJSCConfig.getConfigMap().hasKey("useLazyBundle") ?
mJSCConfig.getConfigMap().getBoolean("useLazyBundle") : false;
recreateReactContextInBackground( recreateReactContextInBackground(
new JSCJavaScriptExecutor.Factory(mJSCConfig.getConfigMap()), new JSCJavaScriptExecutor.Factory(mJSCConfig.getConfigMap()),
JSBundleLoader.createFileLoader(mApplicationContext, mJSBundleFile, useLazyBundle)); mBundleLoader);
} }
/** /**