/
githubmr
/
facebook-react-native
Обзор
Документация
Войти
/
githubmr
/
facebook-react-native
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
packages/react-native/scripts/featureflags/templates/common-cxx/ReactNativeFeatureFlagsAccessor.cpp-template.js
115 строк
3 KB
Rubén Norte
Add new dangerouslyForceOverride method to feature flags (#46963)
11 окт 2024, 20:05
11 окт 2024, 20:05
c86b5c1
Код
Авторство
О чём код?
/** * 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. * * @flow strict * @format */ import type {FeatureFlagDefinitions} from '../../types'; import {DO_NOT_MODIFY_COMMENT, getCxxTypeFromDefaultValue} from '../../utils'; import signedsource from 'signedsource'; export default function (definitions: FeatureFlagDefinitions): string { return signedsource.signFile(`/* * 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. * * ${signedsource.getSigningToken()} */ ${DO_NOT_MODIFY_COMMENT} #include <react/featureflags/ReactNativeFeatureFlagsDefaults.h> #include <sstream> #include <stdexcept> #include <string> #include "ReactNativeFeatureFlags.h" namespace facebook::react { ReactNativeFeatureFlagsAccessor::ReactNativeFeatureFlagsAccessor() : currentProvider_(std::make_unique<ReactNativeFeatureFlagsDefaults>()), wasOverridden_(false) {} ${Object.entries(definitions.common) .map( ([flagName, flagConfig], flagPosition) => `${getCxxTypeFromDefaultValue( flagConfig.defaultValue, )} ReactNativeFeatureFlagsAccessor::${flagName}() { auto flagValue = ${flagName}_.load(); if (!flagValue.has_value()) { // This block is not exclusive but it is not necessary. // If multiple threads try to initialize the feature flag, we would only // be accessing the provider multiple times but the end state of this // instance and the returned flag value would be the same. markFlagAsAccessed(${flagPosition}, "${flagName}"); flagValue = currentProvider_->${flagName}(); ${flagName}_ = flagValue; } return flagValue.value(); }`, ) .join('\n\n')} void ReactNativeFeatureFlagsAccessor::override( std::unique_ptr<ReactNativeFeatureFlagsProvider> provider) { if (wasOverridden_) { throw std::runtime_error( "Feature flags cannot be overridden more than once"); } ensureFlagsNotAccessed(); wasOverridden_ = true; currentProvider_ = std::move(provider); } std::optional<std::string> ReactNativeFeatureFlagsAccessor::getAccessedFeatureFlagNames() const { std::ostringstream featureFlagListBuilder; for (const auto& featureFlagName : accessedFeatureFlags_) { if (featureFlagName != nullptr) { featureFlagListBuilder << featureFlagName << ", "; } } std::string accessedFeatureFlagNames = featureFlagListBuilder.str(); if (!accessedFeatureFlagNames.empty()) { accessedFeatureFlagNames = accessedFeatureFlagNames.substr(0, accessedFeatureFlagNames.size() - 2); } return accessedFeatureFlagNames.empty() ? std::nullopt : std::optional{accessedFeatureFlagNames}; } void ReactNativeFeatureFlagsAccessor::markFlagAsAccessed( int position, const char* flagName) { accessedFeatureFlags_[position] = flagName; } void ReactNativeFeatureFlagsAccessor::ensureFlagsNotAccessed() { auto accessedFeatureFlagNames = getAccessedFeatureFlagNames(); if (accessedFeatureFlagNames.has_value()) { throw std::runtime_error( "Feature flags were accessed before being overridden: " + accessedFeatureFlagNames.value()); } } } // namespace facebook::react `); }