Files
react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/ThemedReactContext.java
T
Emily Janzer d6800616f2 Delegate to the ReactApplicationContext in ThemedReactContext for certain methods
Summary:
In bridgeless mode, we use BridgelessReactContext, which overrides some methods on ReactApplicationContext like `getJSIModule` and returns true for `isBridgeless`. This is needed for things like getting the EventDispatcher, which is currently accessed from the UIManagerModule (which doesn't exist in bridgeless mode).

However, when we create Views in React Native we don't use the ReactApplicationContext directly; instead, we create a ThemedReactContext, which holds a reference to the RAC. It also initializes itself with the RAC's CatalystInstance, so that when you call methods on the TRC it can access native modules, etc.

This doesn't work in bridgeless mode, because the methods are overridden on the RAC, *not* the TRC. So in order for this work as expected, we need to delegate these methods to the RAC member variable. In this diff I'm just doing this for `isBridgeless` and  `getJSIModule` so that accessing the EventDispatcher works.

Changelog: [Internal]

Reviewed By: makovkastar

Differential Revision: D19190760

fbshipit-source-id: 6dc38560edc1061aec782707306590fa1012d5cb
2020-01-03 13:17:18 -08:00

87 lines
2.8 KiB
Java

/*
* 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.uimanager;
import android.app.Activity;
import android.content.Context;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.JSIModule;
import com.facebook.react.bridge.JSIModuleType;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
/**
* Wraps {@link ReactContext} with the base {@link Context} passed into the constructor. It provides
* also a way to start activities using the viewContext to which RN native views belong. It
* delegates lifecycle listener registration to the original instance of {@link ReactContext} which
* is supposed to receive the lifecycle events. At the same time we disallow receiving lifecycle
* events for this wrapper instances. TODO: T7538544 Rename ThemedReactContext to be in alignment
* with name of ReactApplicationContext
*/
public class ThemedReactContext extends ReactContext {
private final ReactApplicationContext mReactApplicationContext;
@Nullable private final String mSurfaceID;
public ThemedReactContext(ReactApplicationContext reactApplicationContext, Context base) {
this(reactApplicationContext, base, null);
}
public ThemedReactContext(
ReactApplicationContext reactApplicationContext, Context base, @Nullable String surfaceID) {
super(base);
if (reactApplicationContext.hasCatalystInstance()) {
initializeWithInstance(reactApplicationContext.getCatalystInstance());
}
mReactApplicationContext = reactApplicationContext;
mSurfaceID = surfaceID;
}
@Override
public void addLifecycleEventListener(LifecycleEventListener listener) {
mReactApplicationContext.addLifecycleEventListener(listener);
}
@Override
public void removeLifecycleEventListener(LifecycleEventListener listener) {
mReactApplicationContext.removeLifecycleEventListener(listener);
}
@Override
public boolean hasCurrentActivity() {
return mReactApplicationContext.hasCurrentActivity();
}
@Override
public @Nullable Activity getCurrentActivity() {
return mReactApplicationContext.getCurrentActivity();
}
/**
* @return a {@link String} that represents the ID of the js application that is being rendered
* with this {@link ThemedReactContext}
*/
public @Nullable String getSurfaceID() {
return mSurfaceID;
}
@Override
public boolean isBridgeless() {
return mReactApplicationContext.isBridgeless();
}
@Override
public JSIModule getJSIModule(JSIModuleType moduleType) {
if (isBridgeless()) {
return mReactApplicationContext.getJSIModule(moduleType);
}
return super.getJSIModule(moduleType);
}
}