Allow reactinstancemanager to set an initialization function

Reviewed By: javache

Differential Revision: D5227811

fbshipit-source-id: e7868481de2a8799af5d6a6bcad26369d054b35e
This commit is contained in:
Kathy Gray
2017-08-14 06:50:30 -07:00
committed by Facebook Github Bot
parent 6783694158
commit 84e80eb781
4 changed files with 70 additions and 25 deletions

View File

@@ -43,20 +43,27 @@ public class CatalystInstanceImpl implements CatalystInstance {
private static final AtomicInteger sNextInstanceIdForTrace = new AtomicInteger(1);
private static class PendingJSCall {
public static class PendingJSCall {
public String mModule;
public String mMethod;
public NativeArray mArguments;
public @Nullable NativeArray mArguments;
public PendingJSCall(
String module,
String method,
NativeArray arguments) {
public PendingJSCall(String module, String method, @Nullable NativeArray arguments) {
mModule = module;
mMethod = method;
mArguments = arguments;
}
void call(CatalystInstanceImpl catalystInstance) {
NativeArray arguments = mArguments != null ? mArguments : new WritableNativeArray();
catalystInstance.jniCallJSFunction(mModule, mMethod, arguments);
}
public String toString() {
return mModule + "." + mMethod + "("
+ (mArguments == null ? "" : mArguments.toString()) + ")";
}
}
// Access from any thread
@@ -216,7 +223,6 @@ public class CatalystInstanceImpl implements CatalystInstance {
public void runJSBundle() {
Log.d(ReactConstants.TAG, "CatalystInstanceImpl.runJSBundle()");
Assertions.assertCondition(!mJSBundleHasLoaded, "JS bundle was already loaded!");
// incrementPendingJSCalls();
mJSBundleLoader.loadScript(CatalystInstanceImpl.this);
@@ -227,8 +233,8 @@ public class CatalystInstanceImpl implements CatalystInstance {
// gates will be queued on the JS thread behind the load.
mAcceptCalls = true;
for (PendingJSCall call : mJSCallsPendingInit) {
jniCallJSFunction(call.mModule, call.mMethod, call.mArguments);
for (PendingJSCall function : mJSCallsPendingInit) {
function.call(this);
}
mJSCallsPendingInit.clear();
mJSBundleHasLoaded = true;
@@ -260,8 +266,12 @@ public class CatalystInstanceImpl implements CatalystInstance {
final String module,
final String method,
final NativeArray arguments) {
callFunction(new PendingJSCall(module, method, arguments));
}
public void callFunction(PendingJSCall function) {
if (mDestroyed) {
final String call = module + "." + method + "(" + arguments.toString() + ")";
final String call = function.toString();
FLog.w(ReactConstants.TAG, "Calling JS function after bridge has been destroyed: " + call);
return;
}
@@ -269,13 +279,12 @@ public class CatalystInstanceImpl implements CatalystInstance {
// Most of the time the instance is initialized and we don't need to acquire the lock
synchronized (mJSCallsPendingInitLock) {
if (!mAcceptCalls) {
mJSCallsPendingInit.add(new PendingJSCall(module, method, arguments));
mJSCallsPendingInit.add(function);
return;
}
}
}
jniCallJSFunction(module, method, arguments);
function.call(this);
}
private native void jniCallJSCallback(int callbackID, NativeArray arguments);