Decouple JSBundleLoader from CatalystInstanceImpl

Summary: Right now JSBundleLoader is tightly coupled to CatalystInstanceImpl; this diffs adds an interface, JSBundleLoaderDelegate, that CatalystInstanceImpl implements so that we can use the bundle loader with other classes.

Reviewed By: mdvacca

Differential Revision: D13216752

fbshipit-source-id: fc406ef30f12ed9d3ed13a062dedd7b33f3b7985
This commit is contained in:
Emily Janzer 2018-11-30 16:48:15 -08:00 committed by Facebook Github Bot
parent 668341a294
commit a2ead1c7b5
5 changed files with 82 additions and 31 deletions

View File

@ -21,7 +21,7 @@ import javax.annotation.Nullable;
*/
@DoNotStrip
public interface CatalystInstance
extends MemoryPressureListener, JSInstance {
extends MemoryPressureListener, JSInstance, JSBundleLoaderDelegate {
void runJSBundle();
// Returns the status of running the JS bundle; waits for an answer if runJSBundle is running

View File

@ -201,17 +201,8 @@ public class CatalystInstanceImpl implements CatalystInstance {
Collection<JavaModuleWrapper> javaModules,
Collection<ModuleHolder> cxxModules);
/**
* This API is used in situations where the JS bundle is being executed not on
* the device, but on a host machine. In that case, we must provide two source
* URLs for the JS bundle: One to be used on the device, and one to be used on
* the remote debugging machine.
*
* @param deviceURL A source URL that is accessible from this device.
* @param remoteURL A source URL that is accessible from the remote machine
* executing the JS.
*/
/* package */ void setSourceURLs(String deviceURL, String remoteURL) {
@Override
public void setSourceURLs(String deviceURL, String remoteURL) {
mSourceURL = deviceURL;
jniSetSourceURL(remoteURL);
}
@ -221,17 +212,20 @@ public class CatalystInstanceImpl implements CatalystInstance {
jniRegisterSegment(segmentId, path);
}
/* package */ void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) {
@Override
public void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously) {
mSourceURL = assetURL;
jniLoadScriptFromAssets(assetManager, assetURL, loadSynchronously);
}
/* package */ void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously) {
@Override
public void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously) {
mSourceURL = sourceURL;
jniLoadScriptFromFile(fileName, sourceURL, loadSynchronously);
}
/* package */ void loadScriptFromDeltaBundle(
@Override
public void loadScriptFromDeltaBundle(
String sourceURL,
NativeDeltaClient deltaClient,
boolean loadSynchronously) {

View File

@ -50,10 +50,10 @@ public final class FallbackJSBundleLoader extends JSBundleLoader {
* it is replaced by the next most preferred loader.
*/
@Override
public String loadScript(CatalystInstanceImpl instance) {
public String loadScript(JSBundleLoaderDelegate delegate) {
while (true) {
try {
return getDelegateLoader().loadScript(instance);
return getDelegateLoader().loadScript(delegate);
} catch (Exception e) {
if (e.getMessage() == null || !e.getMessage().startsWith(RECOVERABLE)) {
throw e;

View File

@ -8,12 +8,11 @@
package com.facebook.react.bridge;
import android.content.Context;
import com.facebook.react.bridge.NativeDeltaClient;
import com.facebook.react.common.DebugServerException;
/**
* A class that stores JS bundle information and allows {@link CatalystInstance} to load a correct
* bundle through {@link ReactBridge}.
* A class that stores JS bundle information and allows a {@link JSBundleLoaderDelegate}
* (e.g. {@link CatalystInstance}) to load a correct bundle through {@link ReactBridge}.
*/
public abstract class JSBundleLoader {
@ -28,8 +27,8 @@ public abstract class JSBundleLoader {
final boolean loadSynchronously) {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
instance.loadScriptFromAssets(context.getAssets(), assetUrl, loadSynchronously);
public String loadScript(JSBundleLoaderDelegate delegate) {
delegate.loadScriptFromAssets(context.getAssets(), assetUrl, loadSynchronously);
return assetUrl;
}
};
@ -49,8 +48,8 @@ public abstract class JSBundleLoader {
final boolean loadSynchronously) {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
instance.loadScriptFromFile(fileName, assetUrl, loadSynchronously);
public String loadScript(JSBundleLoaderDelegate delegate) {
delegate.loadScriptFromFile(fileName, assetUrl, loadSynchronously);
return fileName;
}
};
@ -68,9 +67,9 @@ public abstract class JSBundleLoader {
final String cachedFileLocation) {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
public String loadScript(JSBundleLoaderDelegate delegate) {
try {
instance.loadScriptFromFile(cachedFileLocation, sourceURL, false);
delegate.loadScriptFromFile(cachedFileLocation, sourceURL, false);
return sourceURL;
} catch (Exception e) {
throw DebugServerException.makeGeneric(e.getMessage(), e);
@ -90,9 +89,9 @@ public abstract class JSBundleLoader {
final NativeDeltaClient nativeDeltaClient) {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
public String loadScript(JSBundleLoaderDelegate delegate) {
try {
instance.loadScriptFromDeltaBundle(sourceURL, nativeDeltaClient, false);
delegate.loadScriptFromDeltaBundle(sourceURL, nativeDeltaClient, false);
return sourceURL;
} catch (Exception e) {
throw DebugServerException.makeGeneric(e.getMessage(), e);
@ -110,13 +109,13 @@ public abstract class JSBundleLoader {
final String realSourceURL) {
return new JSBundleLoader() {
@Override
public String loadScript(CatalystInstanceImpl instance) {
instance.setSourceURLs(realSourceURL, proxySourceURL);
public String loadScript(JSBundleLoaderDelegate delegate) {
delegate.setSourceURLs(realSourceURL, proxySourceURL);
return realSourceURL;
}
};
}
/** Loads the script, returning the URL of the source it loaded. */
public abstract String loadScript(CatalystInstanceImpl instance);
public abstract String loadScript(JSBundleLoaderDelegate delegate);
}

View File

@ -0,0 +1,58 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.bridge;
import android.content.Context;
import android.content.res.AssetManager;
/**
* An interface for classes that initialize JavaScript using {@link JSBundleLoader}
*/
public interface JSBundleLoaderDelegate {
/**
* Load a JS bundle from Android assets. See {@link JSBundleLoader#createAssetLoader(Context, String, boolean)}
* @param assetManager
* @param assetURL
* @param loadSynchronously
*/
void loadScriptFromAssets(AssetManager assetManager, String assetURL, boolean loadSynchronously);
/**
* Load a JS bundle from the filesystem.
* See {@link JSBundleLoader#createFileLoader(String)} and {@link JSBundleLoader#createCachedBundleFromNetworkLoader(String, String)}
* @param fileName
* @param sourceURL
* @param loadSynchronously
*/
void loadScriptFromFile(String fileName, String sourceURL, boolean loadSynchronously);
/**
* Load a delta bundle from Metro.
* See {@link JSBundleLoader#createDeltaFromNetworkLoader(String, NativeDeltaClient)}
* @param sourceURL
* @param deltaClient
* @param loadSynchronously
*/
void loadScriptFromDeltaBundle(
String sourceURL,
NativeDeltaClient deltaClient,
boolean loadSynchronously);
/**
* This API is used in situations where the JS bundle is being executed not on
* the device, but on a host machine. In that case, we must provide two source
* URLs for the JS bundle: One to be used on the device, and one to be used on
* the remote debugging machine.
*
* @param deviceURL A source URL that is accessible from this device.
* @param remoteURL A source URL that is accessible from the remote machine
* executing the JS.
*/
void setSourceURLs(String deviceURL, String remoteURL);
}