Skip to content

Commit

Permalink
introduce native api to access RuntimeExecutor (facebook#42882)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [Android][Added] - introduce native api to access RuntimeExecutor

This is the android equivalent of [PR#42758](facebook#42758).

From [PR#42758](facebook#42758)

> The goal of this API is to provide a safe way to access the jsi::runtime in bridgeless mode. The decision to limit access to the runtime in bridgeless was a conscious one - the runtime pointer is not thread-safe and its lifecycle must be managed correctly by owners.

> However, interacting with the runtime is an advanced use case we would want to support. Our recommended ways to access the runtime in bridgeless mode is either 1) via the RuntimeExecutor, or 2) via a C++ TurboModule.

This diff introduces the API that would allow for 1). because react context can be non-null before react instance is ready, i created an async api that will guarantee the runtime executor will be ready.

Differential Revision: D53461821
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 6, 2024
1 parent 608029a commit bb2ee82
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@
* CatalystInstance}
*/
public class ReactContext extends ContextWrapper {

@DoNotStrip
public interface RCTDeviceEventEmitter extends JavaScriptModule {

void emit(@NonNull String eventName, @Nullable Object data);
}

public interface ReactRuntimeExecutor {

void execute(RuntimeExecutor runtimeExecutor);
}

private static final String TAG = "ReactContext";
private static final String EARLY_JS_ACCESS_EXCEPTION_MESSAGE =
"Tried to access a JS module before the React instance was fully set up. Calls to "
Expand Down Expand Up @@ -481,6 +488,7 @@ public void handleException(Exception e) {
}

public class ExceptionHandlerWrapper implements JSExceptionHandler {

@Override
public void handleException(Exception e) {
ReactContext.this.handleException(e);
Expand Down Expand Up @@ -547,6 +555,12 @@ public boolean isBridgeless() {
return null;
}

public void executeRuntimeExecutor(ReactRuntimeExecutor runtimeExecutor) {
if (mCatalystInstance != null) {
runtimeExecutor.execute(mCatalystInstance.getRuntimeExecutor());
}
}

@DeprecatedInNewArchitecture(
message =
"This method will be deprecated later as part of Stable APIs with bridge removal and not encouraged usage.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.RuntimeExecutor;
import com.facebook.react.common.ReactConstants;
import com.facebook.react.internal.turbomodule.core.interfaces.TurboModule;
import com.facebook.react.module.annotations.ReactModule;
Expand Down Expand Up @@ -129,6 +130,15 @@ public void initialize() {

ReactApplicationContext reactContext = getReactApplicationContext();
reactContext.addLifecycleEventListener(this);

reactContext.executeRuntimeExecutor(
new ReactContext.ReactRuntimeExecutor() {
@Override
public void execute(RuntimeExecutor runtimeExecutor) {
// do something
}
});

if (!hasBeenInitialized()) {
if (mConfig == null) {
mConfig = getDefaultConfig(reactContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ public void handleException(Exception e) {
mReactHost.handleHostException(e);
}

@Override
public void executeRuntimeExecutor(ReactRuntimeExecutor runtimeExecutor) {
mReactHost.executeRuntimeExecutor(runtimeExecutor);
}

DefaultHardwareBackBtnHandler getDefaultHardwareBackBtnHandler() {
return mReactHost.getDefaultBackButtonHandler();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.facebook.react.bridge.NativeArray;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContext.ReactRuntimeExecutor;
import com.facebook.react.bridge.ReactMarker;
import com.facebook.react.bridge.ReactMarkerConstants;
import com.facebook.react.bridge.ReactNoCrashBridgeNotAllowedSoftException;
Expand Down Expand Up @@ -542,7 +543,8 @@ private void setCurrentActivity(@Nullable Activity activity) {
return reactInstance.getEventDispatcher();
}

/* package */ @Nullable
/* package */
@Nullable
FabricUIManager getUIManager() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
if (reactInstance == null) {
Expand All @@ -567,7 +569,23 @@ FabricUIManager getUIManager() {
return new ArrayList<>();
}

/* package */ @Nullable
void executeRuntimeExecutor(ReactRuntimeExecutor reactRuntimeExecutor) {
mReactInstanceTaskRef
.get()
.onSuccess(
task -> {
final ReactInstance reactInstance = task.getResult();
if (reactInstance == null) {
return false;
}
reactRuntimeExecutor.execute(reactInstance.getBufferedRuntimeExecutor());
return true;
},
mBGExecutor);
}

/* package */
@Nullable
<T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface) {
if (nativeModuleInterface == UIManagerModule.class) {
ReactSoftExceptionLogger.logSoftExceptionVerbose(
Expand Down Expand Up @@ -699,6 +717,7 @@ public void removeBeforeDestroyListener(@NonNull Function0<Unit> onBeforeDestroy
}

/* package */ interface VeniceThenable<T> {

void then(T t);
}

Expand Down Expand Up @@ -913,6 +932,7 @@ private Task<ReactInstance> getOrCreateReactInstanceTask() {
});

class Result {

final ReactInstance mInstance = instance;
final ReactContext mContext = reactContext;
final boolean mIsReloading = mReloadTask != null;
Expand Down Expand Up @@ -1087,6 +1107,7 @@ private void startAttachedSurfaces(String method, ReactInstance reactInstance) {
private @Nullable Task<ReactInstance> mReloadTask = null;

private interface ReactInstanceTaskUnwrapper {

@Nullable
ReactInstance unwrap(Task<ReactInstance> task, String stage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private native HybridData initHybrid(

private native RuntimeExecutor getUnbufferedRuntimeExecutor();

private native RuntimeExecutor getBufferedRuntimeExecutor();
/* package */ native RuntimeExecutor getBufferedRuntimeExecutor();

private native RuntimeScheduler getRuntimeScheduler();

Expand Down

0 comments on commit bb2ee82

Please sign in to comment.