Refactor MobileConfig Access for "react_fabric: treat_auto_as_undefined" (#37208)

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

Reviewed By: javache

Differential Revision: D45434603

fbshipit-source-id: 9081ea11c87a05e0d8ff95e55d8aa8bcd52b4c39
This commit is contained in:
Nick Gerleman
2023-05-07 01:53:04 -07:00
committed by Facebook GitHub Bot
parent bde38d543e
commit e1876af923
4 changed files with 42 additions and 8 deletions
@@ -11,7 +11,6 @@
#include <folly/Conv.h>
#include <folly/dynamic.h>
#include <glog/logging.h>
#include <react/config/ReactNativeConfig.h>
#include <react/debug/react_native_expect.h>
#include <react/renderer/components/view/primitives.h>
#include <react/renderer/core/LayoutMetrics.h>
@@ -408,19 +407,14 @@ inline void fromRawValue(
const PropsParserContext &context,
const RawValue &value,
YGStyle::ValueRepr &result) {
// For bug compatibility, pass "auto" as YGValueUndefined
static bool treatAutoAsUndefined =
context.contextContainer
.at<std::shared_ptr<ReactNativeConfig const>>("ReactNativeConfig")
->getBool("react_fabric:treat_auto_as_undefined");
if (value.hasType<Float>()) {
result = yogaStyleValueFromFloat((Float)value);
return;
} else if (value.hasType<std::string>()) {
const auto stringValue = (std::string)value;
if (stringValue == "auto") {
result = treatAutoAsUndefined ? YGValueUndefined : YGValueAuto;
result = context.treatAutoAsYGValueUndefined() ? YGValueUndefined
: YGValueAuto;
return;
} else {
if (stringValue.back() == '%') {
@@ -23,6 +23,7 @@ target_link_libraries(react_render_core
folly_runtime
glog
jsi
react_config
react_debug
react_render_debug
react_render_graphics
@@ -0,0 +1,26 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "PropsParserContext.h"
#include <react/config/ReactNativeConfig.h>
namespace facebook::react {
bool PropsParserContext::treatAutoAsYGValueUndefined() const {
if (treatAutoAsYGValueUndefined_ == std::nullopt) {
auto config = contextContainer.at<std::shared_ptr<const ReactNativeConfig>>(
"ReactNativeConfig");
treatAutoAsYGValueUndefined_ = config
? config->getBool("react_fabric:treat_auto_as_undefined")
: false;
}
return *treatAutoAsYGValueUndefined_;
}
} // namespace facebook::react
@@ -7,6 +7,8 @@
#pragma once
#include <optional>
#include <react/renderer/core/ReactPrimitives.h>
#include <react/utils/ContextContainer.h>
@@ -16,12 +18,23 @@ namespace facebook::react {
// It should be used as infrequently as possible - most props can and should
// be parsed without any context.
struct PropsParserContext {
PropsParserContext(
SurfaceId const surfaceId,
ContextContainer const &contextContainer)
: surfaceId(surfaceId), contextContainer(contextContainer) {}
// Non-copyable
PropsParserContext(const PropsParserContext &) = delete;
PropsParserContext &operator=(const PropsParserContext &) = delete;
SurfaceId const surfaceId;
ContextContainer const &contextContainer;
// Temporary feature flags
bool treatAutoAsYGValueUndefined() const;
private:
mutable std::optional<bool> treatAutoAsYGValueUndefined_;
};
} // namespace facebook::react