Provide sync vs async interface for bundle loading via parameter

Reviewed By: javache

Differential Revision: D5104317

fbshipit-source-id: ffacb57d85c24795a3acc2faba2ff5824cc739b2
This commit is contained in:
Kathy Gray
2017-05-24 07:27:06 -07:00
committed by Facebook Github Bot
parent d59fd9e419
commit f46eaa30cf
15 changed files with 113 additions and 119 deletions

View File

@@ -233,7 +233,8 @@ public class ReactInstanceManagerBuilder {
mCurrentActivity,
mDefaultHardwareBackBtnHandler,
(mJSBundleLoader == null && mJSBundleAssetUrl != null) ?
JSBundleLoader.createAssetLoader(mApplication, mJSBundleAssetUrl) : mJSBundleLoader,
JSBundleLoader.createAssetLoader(mApplication, mJSBundleAssetUrl, false /*Asynchronous*/) :
mJSBundleLoader,
mJSMainModuleName,
mPackages,
mUseDeveloperSupport,

View File

@@ -27,6 +27,9 @@ public interface CatalystInstance
extends MemoryPressureListener, JSInstance {
void runJSBundle();
// Returns the status of running the JS bundle; waits for an answer if runJSBundle is running
boolean hasRunJSBundle();
/**
* Return the source URL of the JS Bundle that was run, or {@code null} if no JS
* bundle has been run yet.

View File

@@ -85,6 +85,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
private boolean mInitialized = false;
private volatile boolean mAcceptCalls = false;
private boolean mJSBundleHasStartedLoading;
private boolean mJSBundleHasLoaded;
private @Nullable String mSourceURL;
@@ -185,29 +186,31 @@ public class CatalystInstanceImpl implements CatalystInstance {
jniSetSourceURL(remoteURL);
}
/* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL) {
/* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) {
mSourceURL = assetURL;
jniLoadScriptFromAssets(assetManager, assetURL);
jniLoadScriptFromAssets(assetManager, assetURL, loadSynchronously);
}
/* package */ void loadScriptFromFile(String fileName, String sourceURL) {
/* package */ void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously) {
mSourceURL = sourceURL;
jniLoadScriptFromFile(fileName, sourceURL);
jniLoadScriptFromFile(fileName, sourceURL, loadSynchronously);
}
private native void jniSetSourceURL(String sourceURL);
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL);
private native void jniLoadScriptFromFile(String fileName, String sourceURL);
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously);
private native void jniLoadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously);
@Override
public void runJSBundle() {
Assertions.assertCondition(!mJSBundleHasLoaded, "JS bundle was already loaded!");
mJSBundleHasLoaded = true;
mJSBundleHasStartedLoading = true;
// incrementPendingJSCalls();
mJSBundleLoader.loadScript(CatalystInstanceImpl.this);
synchronized (mJSCallsPendingInitLock) {
// Loading the bundle is queued on the JS thread, but may not have
// run yet. It's safe to set this here, though, since any work it
// gates will be queued on the JS thread behind the load.
@@ -217,12 +220,20 @@ public class CatalystInstanceImpl implements CatalystInstance {
jniCallJSFunction(call.mModule, call.mMethod, call.mArguments);
}
mJSCallsPendingInit.clear();
mJSBundleHasLoaded = true;
}
// This is registered after JS starts since it makes a JS call
Systrace.registerListener(mTraceListener);
}
@Override
public boolean hasRunJSBundle() {
synchronized (mJSCallsPendingInitLock) {
return mJSBundleHasLoaded && mAcceptCalls;
}
}
@Override
public @Nullable String getSourceURL() {
return mSourceURL;

View File

@@ -26,11 +26,12 @@ public abstract class JSBundleLoader {
*/
public static JSBundleLoader createAssetLoader(
final Context context,
final String assetUrl) {
final String assetUrl,
final boolean loadSynchronously) {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
instance.loadScriptFromAssets(context.getAssets(), assetUrl);
instance.loadScriptFromAssets(context.getAssets(), assetUrl, loadSynchronously);
return assetUrl;
}
};
@@ -41,16 +42,17 @@ public abstract class JSBundleLoader {
* passing large strings from java to native memorory.
*/
public static JSBundleLoader createFileLoader(final String fileName) {
return createFileLoader(fileName, fileName);
return createFileLoader(fileName, fileName, false);
}
public static JSBundleLoader createFileLoader(
final String fileName,
final String assetUrl) {
final String assetUrl,
final boolean loadSynchronously) {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
instance.loadScriptFromFile(fileName, assetUrl);
instance.loadScriptFromFile(fileName, assetUrl, loadSynchronously);
return fileName;
}
};
@@ -70,7 +72,7 @@ public abstract class JSBundleLoader {
@Override
public String loadScript(CatalystInstanceImpl instance) {
try {
instance.loadScriptFromFile(cachedFileLocation, sourceURL);
instance.loadScriptFromFile(cachedFileLocation, sourceURL, false);
return sourceURL;
} catch (Exception e) {
throw DebugServerException.makeGeneric(e.getMessage(), e);