Use SurfaceRegistry globals whenever available (#37410)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37410

Incremental adoption of new bridgeless API's, where they are semantically equivalent to the old one.

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D45736529

fbshipit-source-id: e41f6840e7c329f6051530e53773fae760584842
This commit is contained in:
Pieter De Baets authored and Facebook GitHub Bot committed 2023-05-30 07:20:25 -07:00
1 parent b0864fd9df
commit da3dcd7326
1 file changed
+47 -73
@@ -13,6 +13,22 @@
namespace facebook::react {
namespace {
void throwIfBridgeless(
jsi::Runtime &runtime,
jsi::Object &global,
const char *methodName) {
auto isBridgeless = global.getProperty(runtime, "RN$Bridgeless");
if (isBridgeless.isBool() && isBridgeless.asBool()) {
throw std::runtime_error(
"SurfaceRegistryBinding::" + std::string(methodName) +
" failed. Global was not installed.");
}
}
} // namespace
void SurfaceRegistryBinding::startSurface(
jsi::Runtime &runtime,
SurfaceId surfaceId,
@@ -26,43 +42,24 @@ void SurfaceRegistryBinding::startSurface(
parameters["fabric"] = true;
auto global = runtime.global();
auto isBridgeless = global.hasProperty(runtime, "RN$Bridgeless") &&
global.getProperty(runtime, "RN$Bridgeless").asBool();
if (isBridgeless) {
if (!global.hasProperty(runtime, "RN$SurfaceRegistry")) {
throw std::runtime_error(
"SurfaceRegistryBinding::startSurface: Failed to start Surface \"" +
moduleName + "\". global.RN$SurfaceRegistry was not installed.");
}
auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
auto method = registry.getPropertyAsFunction(runtime, "renderSurface");
auto registry = global.getProperty(runtime, "RN$AppRegistry");
if (registry.isObject()) {
auto method = std::move(registry).asObject(runtime).getPropertyAsFunction(
runtime, "runApplication");
method.call(
runtime,
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
} else {
if (moduleName != "LogBox" &&
global.hasProperty(runtime, "RN$SurfaceRegistry")) {
auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
auto method = registry.getPropertyAsFunction(runtime, "renderSurface");
method.call(
runtime,
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
} else {
callMethodOfModule(
runtime,
"AppRegistry",
"runApplication",
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
}
throwIfBridgeless(runtime, global, "startSurface");
callMethodOfModule(
runtime,
"AppRegistry",
"runApplication",
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
}
}
@@ -79,44 +76,24 @@ void SurfaceRegistryBinding::setSurfaceProps(
parameters["fabric"] = true;
auto global = runtime.global();
auto isBridgeless = global.hasProperty(runtime, "RN$Bridgeless") &&
global.getProperty(runtime, "RN$Bridgeless").asBool();
if (isBridgeless) {
if (!global.hasProperty(runtime, "RN$SurfaceRegistry")) {
throw std::runtime_error(
"SurfaceRegistryBinding::setSurfaceProps: Failed to set Surface props for \"" +
moduleName + "\". global.RN$SurfaceRegistry was not installed.");
}
auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
auto method = registry.getPropertyAsFunction(runtime, "setSurfaceProps");
auto registry = global.getProperty(runtime, "RN$AppRegistry");
if (registry.isObject()) {
auto method = std::move(registry).asObject(runtime).getPropertyAsFunction(
runtime, "setSurfaceProps");
method.call(
runtime,
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
} else {
if (moduleName != "LogBox" &&
global.hasProperty(runtime, "RN$SurfaceRegistry")) {
auto registry = global.getPropertyAsObject(runtime, "RN$SurfaceRegistry");
auto method = registry.getPropertyAsFunction(runtime, "setSurfaceProps");
method.call(
runtime,
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
} else {
callMethodOfModule(
runtime,
"AppRegistry",
"setSurfaceProps",
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
}
throwIfBridgeless(runtime, global, "setSurfaceProps");
callMethodOfModule(
runtime,
"AppRegistry",
"setSurfaceProps",
{jsi::String::createFromUtf8(runtime, moduleName),
jsi::valueFromDynamic(runtime, parameters),
jsi::Value(runtime, displayModeToInt(displayMode))});
}
}
@@ -124,18 +101,15 @@ void SurfaceRegistryBinding::stopSurface(
jsi::Runtime &runtime,
SurfaceId surfaceId) {
auto global = runtime.global();
auto isBridgeless = global.hasProperty(runtime, "RN$Bridgeless") &&
global.getProperty(runtime, "RN$Bridgeless").asBool();
if (isBridgeless) {
if (!global.hasProperty(runtime, "RN$stopSurface")) {
// ReactFabric module has not been loaded yet; there's no surface to stop.
return;
}
// Bridgeless mode uses a custom JSI binding instead of callable module.
global.getPropertyAsFunction(runtime, "RN$stopSurface")
auto stopFunction = global.getProperty(runtime, "RN$stopSurface");
if (stopFunction.isObject() &&
stopFunction.asObject(runtime).isFunction(runtime)) {
std::move(stopFunction)
.asObject(runtime)
.asFunction(runtime)
.call(runtime, {jsi::Value{surfaceId}});
} else {
throwIfBridgeless(runtime, global, "stopSurface");
callMethodOfModule(
runtime,
"ReactFabric",