mirror of
https://github.com/status-im/react-native.git
synced 2025-02-04 21:53:30 +00:00
Decouple module callback mechanism from CatalystInstance
Summary: Create a JSInstance superinterface which doesn't include all the lifecycle stuff. Reviewed By: AaaChiuuu Differential Revision: D4614410 fbshipit-source-id: 16047bbcb1bb69bf36a0a13ef68f3a6aa396a991
This commit is contained in:
parent
7e4b8ff000
commit
6410e256c5
@ -59,14 +59,14 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
}
|
||||
|
||||
public abstract @Nullable T extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex);
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex);
|
||||
}
|
||||
|
||||
static final private ArgumentExtractor<Boolean> ARGUMENT_EXTRACTOR_BOOLEAN =
|
||||
new ArgumentExtractor<Boolean>() {
|
||||
@Override
|
||||
public Boolean extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return jsArguments.getBoolean(atIndex);
|
||||
}
|
||||
};
|
||||
@ -75,7 +75,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<Double>() {
|
||||
@Override
|
||||
public Double extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return jsArguments.getDouble(atIndex);
|
||||
}
|
||||
};
|
||||
@ -84,7 +84,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<Float>() {
|
||||
@Override
|
||||
public Float extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return (float) jsArguments.getDouble(atIndex);
|
||||
}
|
||||
};
|
||||
@ -93,7 +93,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<Integer>() {
|
||||
@Override
|
||||
public Integer extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return (int) jsArguments.getDouble(atIndex);
|
||||
}
|
||||
};
|
||||
@ -102,7 +102,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<String>() {
|
||||
@Override
|
||||
public String extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return jsArguments.getString(atIndex);
|
||||
}
|
||||
};
|
||||
@ -111,7 +111,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<ReadableNativeArray>() {
|
||||
@Override
|
||||
public ReadableNativeArray extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return jsArguments.getArray(atIndex);
|
||||
}
|
||||
};
|
||||
@ -120,7 +120,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<Dynamic>() {
|
||||
@Override
|
||||
public Dynamic extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return DynamicFromArray.create(jsArguments, atIndex);
|
||||
}
|
||||
};
|
||||
@ -129,7 +129,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<ReadableMap>() {
|
||||
@Override
|
||||
public ReadableMap extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
return jsArguments.getMap(atIndex);
|
||||
}
|
||||
};
|
||||
@ -138,12 +138,12 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
new ArgumentExtractor<Callback>() {
|
||||
@Override
|
||||
public @Nullable Callback extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
if (jsArguments.isNull(atIndex)) {
|
||||
return null;
|
||||
} else {
|
||||
int id = (int) jsArguments.getDouble(atIndex);
|
||||
return new CallbackImpl(catalystInstance, executorToken, id);
|
||||
return new CallbackImpl(jsInstance, executorToken, id);
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -157,11 +157,11 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
|
||||
@Override
|
||||
public Promise extractArgument(
|
||||
CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray jsArguments, int atIndex) {
|
||||
Callback resolve = ARGUMENT_EXTRACTOR_CALLBACK
|
||||
.extractArgument(catalystInstance, executorToken, jsArguments, atIndex);
|
||||
.extractArgument(jsInstance, executorToken, jsArguments, atIndex);
|
||||
Callback reject = ARGUMENT_EXTRACTOR_CALLBACK
|
||||
.extractArgument(catalystInstance, executorToken, jsArguments, atIndex + 1);
|
||||
.extractArgument(jsInstance, executorToken, jsArguments, atIndex + 1);
|
||||
return new PromiseImpl(resolve, reject);
|
||||
}
|
||||
};
|
||||
@ -307,7 +307,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void invoke(CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray parameters) {
|
||||
public void invoke(JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray parameters) {
|
||||
SystraceMessage.beginSection(TRACE_TAG_REACT_JAVA_BRIDGE, "callJavaModuleMethod")
|
||||
.arg("method", mTraceName)
|
||||
.flush();
|
||||
@ -329,7 +329,7 @@ public abstract class BaseJavaModule implements NativeModule {
|
||||
try {
|
||||
for (; i < mArgumentExtractors.length; i++) {
|
||||
mArguments[i + executorTokenOffset] = mArgumentExtractors[i].extractArgument(
|
||||
catalystInstance, executorToken, parameters, jsArgumentsConsumed);
|
||||
jsInstance, executorToken, parameters, jsArgumentsConsumed);
|
||||
jsArgumentsConsumed += mArgumentExtractors[i].getJSArgumentsNeeded();
|
||||
}
|
||||
} catch (UnexpectedNativeTypeException e) {
|
||||
|
@ -14,13 +14,13 @@ package com.facebook.react.bridge;
|
||||
*/
|
||||
public final class CallbackImpl implements Callback {
|
||||
|
||||
private final CatalystInstance mCatalystInstance;
|
||||
private final JSInstance mJSInstance;
|
||||
private final ExecutorToken mExecutorToken;
|
||||
private final int mCallbackId;
|
||||
private boolean mInvoked;
|
||||
|
||||
public CallbackImpl(CatalystInstance bridge, ExecutorToken executorToken, int callbackId) {
|
||||
mCatalystInstance = bridge;
|
||||
public CallbackImpl(JSInstance jsInstance, ExecutorToken executorToken, int callbackId) {
|
||||
mJSInstance = jsInstance;
|
||||
mExecutorToken = executorToken;
|
||||
mCallbackId = callbackId;
|
||||
mInvoked = false;
|
||||
@ -33,7 +33,7 @@ public final class CallbackImpl implements Callback {
|
||||
"module. This callback type only permits a single invocation from "+
|
||||
"native code.");
|
||||
}
|
||||
mCatalystInstance.invokeCallback(mExecutorToken, mCallbackId, Arguments.fromJavaArgs(args));
|
||||
mJSInstance.invokeCallback(mExecutorToken, mCallbackId, Arguments.fromJavaArgs(args));
|
||||
mInvoked = true;
|
||||
}
|
||||
}
|
||||
|
@ -23,7 +23,8 @@ import com.facebook.react.common.annotations.VisibleForTesting;
|
||||
* Java APIs be invokable from JavaScript as well.
|
||||
*/
|
||||
@DoNotStrip
|
||||
public interface CatalystInstance extends MemoryPressureListener {
|
||||
public interface CatalystInstance
|
||||
extends MemoryPressureListener, JSInstance {
|
||||
void runJSBundle();
|
||||
|
||||
/**
|
||||
@ -34,8 +35,11 @@ public interface CatalystInstance extends MemoryPressureListener {
|
||||
|
||||
// This is called from java code, so it won't be stripped anyway, but proguard will rename it,
|
||||
// which this prevents.
|
||||
@DoNotStrip
|
||||
void invokeCallback(ExecutorToken executorToken, final int callbackID, final NativeArray arguments);
|
||||
@Override @DoNotStrip
|
||||
void invokeCallback(
|
||||
ExecutorToken executorToken,
|
||||
int callbackID,
|
||||
NativeArray arguments);
|
||||
@DoNotStrip
|
||||
void callFunction(
|
||||
ExecutorToken executorToken,
|
||||
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Copyright (c) 2015-present, Facebook, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This source code is licensed under the BSD-style license found in the
|
||||
* LICENSE file in the root directory of this source tree. An additional grant
|
||||
* of patent rights can be found in the PATENTS file in the same directory.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
/**
|
||||
* This interface includes the methods needed to use a running JS
|
||||
* instance, without specifying any of the bridge-specific
|
||||
* initialization or lifecycle management.
|
||||
*/
|
||||
public interface JSInstance {
|
||||
void invokeCallback(
|
||||
ExecutorToken executorToken,
|
||||
int callbackID,
|
||||
NativeArray arguments);
|
||||
// TODO if this interface survives refactoring, think about adding
|
||||
// callFunction.
|
||||
}
|
@ -20,7 +20,7 @@ import java.util.Map;
|
||||
*/
|
||||
public interface NativeModule {
|
||||
interface NativeMethod {
|
||||
void invoke(CatalystInstance catalystInstance, ExecutorToken executorToken, ReadableNativeArray parameters);
|
||||
void invoke(JSInstance jsInstance, ExecutorToken executorToken, ReadableNativeArray parameters);
|
||||
String getType();
|
||||
}
|
||||
|
||||
|
@ -176,12 +176,13 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
}
|
||||
}
|
||||
|
||||
private native void initializeBridge(ReactCallback callback,
|
||||
JavaScriptExecutor jsExecutor,
|
||||
MessageQueueThread jsQueue,
|
||||
MessageQueueThread moduleQueue,
|
||||
Collection<JavaModuleWrapper> javaModules,
|
||||
Collection<CxxModuleWrapper> cxxModules);
|
||||
private native void initializeBridge(
|
||||
ReactCallback callback,
|
||||
JavaScriptExecutor jsExecutor,
|
||||
MessageQueueThread jsQueue,
|
||||
MessageQueueThread moduleQueue,
|
||||
Collection<JavaModuleWrapper> javaModules,
|
||||
Collection<CxxModuleWrapper> cxxModules);
|
||||
|
||||
/**
|
||||
* This API is used in situations where the JS bundle is being executed not on
|
||||
@ -232,7 +233,6 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
mJSCallsPendingInit.clear();
|
||||
}
|
||||
|
||||
|
||||
// This is registered after JS starts since it makes a JS call
|
||||
Systrace.registerListener(mTraceListener);
|
||||
}
|
||||
@ -302,7 +302,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
mReactQueueConfiguration.getNativeModulesQueueThread().runOnQueue(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mJavaRegistry.notifyCatalystInstanceDestroy();
|
||||
mJavaRegistry.notifyJSInstanceDestroy();
|
||||
}
|
||||
});
|
||||
boolean wasIdle = (mPendingJSCalls.getAndSet(0) == 0);
|
||||
@ -341,7 +341,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
mReactQueueConfiguration.getNativeModulesQueueThread().runOnQueue(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
mJavaRegistry.notifyCatalystInstanceInitialized();
|
||||
mJavaRegistry.notifyJSInstanceInitialized();
|
||||
}
|
||||
});
|
||||
}
|
||||
@ -390,7 +390,7 @@ public class CatalystInstanceImpl implements CatalystInstance {
|
||||
if (mDestroyed) {
|
||||
return;
|
||||
}
|
||||
switch(level) {
|
||||
switch (level) {
|
||||
case UI_HIDDEN:
|
||||
handleMemoryPressureUiHidden();
|
||||
break;
|
||||
|
@ -16,8 +16,8 @@ import java.util.Map;
|
||||
|
||||
import com.facebook.proguard.annotations.DoNotStrip;
|
||||
import com.facebook.react.bridge.BaseJavaModule;
|
||||
import com.facebook.react.bridge.CatalystInstance;
|
||||
import com.facebook.react.bridge.ExecutorToken;
|
||||
import com.facebook.react.bridge.JSInstance;
|
||||
import com.facebook.react.bridge.NativeArray;
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.ReactMarker;
|
||||
@ -54,12 +54,12 @@ public class JavaModuleWrapper {
|
||||
String type;
|
||||
}
|
||||
|
||||
private final CatalystInstance mCatalystInstance;
|
||||
private final JSInstance mJSInstance;
|
||||
private final ModuleHolder mModuleHolder;
|
||||
private final ArrayList<NativeModule.NativeMethod> mMethods;
|
||||
|
||||
public JavaModuleWrapper(CatalystInstance catalystinstance, ModuleHolder moduleHolder) {
|
||||
mCatalystInstance = catalystinstance;
|
||||
public JavaModuleWrapper(JSInstance jsInstance, ModuleHolder moduleHolder) {
|
||||
mJSInstance = jsInstance;
|
||||
mModuleHolder = moduleHolder;
|
||||
mMethods = new ArrayList<>();
|
||||
}
|
||||
@ -135,6 +135,6 @@ public class JavaModuleWrapper {
|
||||
return;
|
||||
}
|
||||
|
||||
mMethods.get(methodId).invoke(mCatalystInstance, token, parameters);
|
||||
mMethods.get(methodId).invoke(mJSInstance, token, parameters);
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.facebook.infer.annotation.Assertions;
|
||||
import com.facebook.react.bridge.JSInstance;
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.OnBatchCompleteListener;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
@ -41,12 +42,12 @@ public class NativeModuleRegistry {
|
||||
}
|
||||
|
||||
/* package */ Collection<JavaModuleWrapper> getJavaModules(
|
||||
CatalystInstanceImpl catalystInstanceImpl) {
|
||||
JSInstance jsInstance) {
|
||||
ArrayList<JavaModuleWrapper> javaModules = new ArrayList<>();
|
||||
for (Map.Entry<Class<? extends NativeModule>, ModuleHolder> entry : mModules.entrySet()) {
|
||||
Class<?> type = entry.getKey();
|
||||
if (!CxxModuleWrapper.class.isAssignableFrom(type)) {
|
||||
javaModules.add(new JavaModuleWrapper(catalystInstanceImpl, entry.getValue()));
|
||||
javaModules.add(new JavaModuleWrapper(jsInstance, entry.getValue()));
|
||||
}
|
||||
}
|
||||
return javaModules;
|
||||
@ -63,11 +64,11 @@ public class NativeModuleRegistry {
|
||||
return cxxModules;
|
||||
}
|
||||
|
||||
/* package */ void notifyCatalystInstanceDestroy() {
|
||||
/* package */ void notifyJSInstanceDestroy() {
|
||||
mReactApplicationContext.assertOnNativeModulesQueueThread();
|
||||
Systrace.beginSection(
|
||||
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
|
||||
"NativeModuleRegistry_notifyCatalystInstanceDestroy");
|
||||
"NativeModuleRegistry_notifyJSInstanceDestroy");
|
||||
try {
|
||||
for (ModuleHolder module : mModules.values()) {
|
||||
module.destroy();
|
||||
@ -77,7 +78,7 @@ public class NativeModuleRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
/* package */ void notifyCatalystInstanceInitialized() {
|
||||
/* package */ void notifyJSInstanceInitialized() {
|
||||
mReactApplicationContext.assertOnNativeModulesQueueThread("From version React Native v0.44, " +
|
||||
"native modules are explicitly not initialized on the UI thread. See " +
|
||||
"https://github.com/facebook/react-native/wiki/Breaking-Changes#d4611211-reactnativeandroidbreaking-move-nativemodule-initialization-off-ui-thread---aaachiuuu " +
|
||||
@ -85,7 +86,7 @@ public class NativeModuleRegistry {
|
||||
ReactMarker.logMarker(ReactMarkerConstants.NATIVE_MODULE_INITIALIZE_START);
|
||||
Systrace.beginSection(
|
||||
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE,
|
||||
"NativeModuleRegistry_notifyCatalystInstanceInitialized");
|
||||
"NativeModuleRegistry_notifyJSInstanceInitialized");
|
||||
try {
|
||||
for (ModuleHolder module : mModules.values()) {
|
||||
module.initialize();
|
||||
|
Loading…
x
Reference in New Issue
Block a user