xplat: added ReactNativeConfig to access runtime specific config values

Summary: Each app may provide different impl for its runtime specific behaviors, then Fabric and other new infra can share the same config instance to configure stuffs.

Reviewed By: sahrens

Differential Revision: D13290319

fbshipit-source-id: 30e3eeedc6ff6ef250ed233b27e38cb7c1062b55
This commit is contained in:
Kevin Gozali 2018-12-05 15:00:34 -08:00 committed by Facebook Github Bot
parent 44878ea9bc
commit 3b6f229eb9
5 changed files with 142 additions and 2 deletions

View File

@ -11,6 +11,7 @@
#import <React/RCTBridge.h>
#import <React/RCTComponentViewFactory.h>
#import <React/RCTPrimitives.h>
#import <react/config/ReactNativeConfig.h>
NS_ASSUME_NONNULL_BEGIN
@ -25,7 +26,8 @@ NS_ASSUME_NONNULL_BEGIN
*/
@interface RCTSurfacePresenter : NSObject
- (instancetype)initWithBridge:(RCTBridge *)bridge;
- (instancetype)initWithBridge:(RCTBridge *)bridge
config:(std::shared_ptr<const facebook::react::ReactNativeConfig>)config;
@property (nonatomic, readonly) RCTComponentViewFactory *componentViewFactory;

View File

@ -50,9 +50,10 @@ using namespace facebook::react;
RCTSurfaceRegistry *_surfaceRegistry; // Thread-safe.
RCTBridge *_bridge; // Unsafe. We are moving away from Bridge.
RCTBridge *_batchedBridge;
std::shared_ptr<const ReactNativeConfig> _reactNativeConfig;
}
- (instancetype)initWithBridge:(RCTBridge *)bridge
- (instancetype)initWithBridge:(RCTBridge *)bridge config:(std::shared_ptr<const ReactNativeConfig>)config
{
if (self = [super init]) {
_bridge = bridge;
@ -63,6 +64,12 @@ using namespace facebook::react;
_mountingManager = [[RCTMountingManager alloc] init];
_mountingManager.delegate = self;
if (config != nullptr) {
_reactNativeConfig = config;
} else {
_reactNativeConfig = std::make_shared<const EmptyReactNativeConfig>();
}
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleBridgeWillReloadNotification:)
name:RCTBridgeWillReloadNotification
@ -161,6 +168,8 @@ using namespace facebook::react;
auto contextContainer = std::make_shared<ContextContainer>();
contextContainer->registerInstance(_reactNativeConfig);
auto messageQueueThread = _batchedBridge.jsMessageThread;
auto runtime = (facebook::jsi::Runtime *)((RCTCxxBridge *)_batchedBridge).runtime;

45
ReactCommon/config/BUCK Normal file
View File

@ -0,0 +1,45 @@
load("@fbsource//tools/build_defs/apple:flag_defs.bzl", "flags", "get_debug_preprocessor_flags", "get_static_library_ios_flags")
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "get_apple_inspector_flags", "rn_xplat_cxx_library")
load("@fbsource//tools/build_defs:default_platform_defs.bzl", "ANDROID", "APPLE")
load("@fbsource//tools/build_defs:glob_defs.bzl", "subdir_glob")
APPLE_COMPILER_FLAGS = flags.get_flag_value(
get_static_library_ios_flags(),
"compiler_flags",
)
rn_xplat_cxx_library(
name = "config",
srcs = glob(["**/*.cpp"]),
header_namespace = "",
exported_headers = subdir_glob(
[
("", "**/*.h"),
],
prefix = "react/config",
),
compiler_flags = [
"-fexceptions",
"-frtti",
"-std=c++14",
"-Wall",
],
fbobjc_compiler_flags = APPLE_COMPILER_FLAGS,
fbobjc_preprocessor_flags = get_debug_preprocessor_flags() + get_apple_inspector_flags(),
force_static = True,
platforms = (ANDROID, APPLE),
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",
"-DWITH_FBSYSTRACE=1",
],
visibility = [
"PUBLIC",
],
deps = [
"xplat//fbsystrace:fbsystrace",
"xplat//folly:headers_only",
"xplat//folly:memory",
"xplat//folly:molly",
"xplat//third-party/glog:glog",
],
)

View File

@ -0,0 +1,40 @@
/**
* 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.
*/
#include "ReactNativeConfig.h"
namespace facebook {
namespace react {
/**
* ReactNative configuration as provided by the hosting app.
* Provide a sub-class implementation to allow app specific customization.
*/
ReactNativeConfig::ReactNativeConfig() {}
ReactNativeConfig::~ReactNativeConfig() {}
EmptyReactNativeConfig::EmptyReactNativeConfig() {}
bool EmptyReactNativeConfig::getBool(const std::string &param) const {
return false;
}
std::string EmptyReactNativeConfig::getString(const std::string &param) const {
return "";
}
int64_t EmptyReactNativeConfig::getInt64(const std::string &param) const {
return 0;
}
double EmptyReactNativeConfig::getDouble(const std::string &param) const {
return 0.0;
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,44 @@
/**
* 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.
*/
#pragma once
#include <string>
namespace facebook {
namespace react {
/**
* ReactNative configuration as provided by the hosting app.
* Provide a sub-class implementation to allow app specific customization.
*/
class ReactNativeConfig {
public:
ReactNativeConfig();
virtual ~ReactNativeConfig();
virtual bool getBool(const std::string &param) const = 0;
virtual std::string getString(const std::string &param) const = 0;
virtual int64_t getInt64(const std::string &param) const = 0;
virtual double getDouble(const std::string &param) const = 0;
};
/**
* Empty configuration that will always provide "falsy" values.
*/
class EmptyReactNativeConfig : public ReactNativeConfig {
public:
EmptyReactNativeConfig();
bool getBool(const std::string &param) const override;
std::string getString(const std::string &param) const override;
int64_t getInt64(const std::string &param) const override;
double getDouble(const std::string &param) const override;
};
} // namespace react
} // namespace facebook