Add a field + getter for instanceKey to ReactContext

Summary:
Adding a new String field for `instanceKey` to ReactContext, which is set via a new constructor on ReactApplicationContext. Also adding getters to ReactContext and ThemedReactContext so that it's accessible from any instance/subclass of ReactContext.

This will only be used in bridgeless mode.

Reviewed By: mdvacca

Differential Revision: D18316556

fbshipit-source-id: 9757da72fde4ba36034c1e129326461fed496229
This commit is contained in:
Emily Janzer
2019-11-05 18:58:33 -08:00
committed by Facebook Github Bot
parent f77abc583c
commit 6598292ede
3 changed files with 38 additions and 0 deletions
@@ -20,4 +20,15 @@ public class ReactApplicationContext extends ReactContext {
public ReactApplicationContext(Context context) {
super(context.getApplicationContext());
}
/**
* A constructor that takes a unique string identifier for the React instance. For bridgeless mode
* only - do not use.
*
* <p>TODO T43898341 Make this package-private once we've consolidated the classes that need this
* in this package
*/
public ReactApplicationContext(Context context, String instanceKey) {
super(context.getApplicationContext(), instanceKey);
}
}
@@ -53,9 +53,20 @@ public class ReactContext extends ContextWrapper {
private @Nullable NativeModuleCallExceptionHandler mNativeModuleCallExceptionHandler;
private @Nullable NativeModuleCallExceptionHandler mExceptionHandlerWrapper;
private @Nullable WeakReference<Activity> mCurrentActivity;
private final @Nullable String mInstanceKey;
public ReactContext(Context base) {
super(base);
mInstanceKey = null;
}
/**
* A constructor that takes a unique string identifier for the React instance. For bridgeless mode
* only - do not use.
*/
/* package */ ReactContext(Context base, String instanceKey) {
super(base);
mInstanceKey = instanceKey;
}
/** Set and initialize CatalystInstance for this Context. This should be called exactly once. */
@@ -408,4 +419,14 @@ public class ReactContext extends ContextWrapper {
public JavaScriptContextHolder getJavaScriptContextHolder() {
return mCatalystInstance.getJavaScriptContextHolder();
}
/**
* TODO T43898341 Make this package-private once we've consolidated the classes that need this in
* this package
*
* @return The key for the associated React instance
*/
public @Nullable String getInstanceKey() {
return mInstanceKey;
}
}
@@ -53,4 +53,10 @@ public class ThemedReactContext extends ReactContext {
public @Nullable Activity getCurrentActivity() {
return mReactApplicationContext.getCurrentActivity();
}
@Nullable
@Override
public String getInstanceKey() {
return mReactApplicationContext.getInstanceKey();
}
}