tighter ContextContainer semantics
Summary: shergin mentioned that he'd like to move away from RTTI a bit and use explicit key strings for context container instances rather than relying on the `typeid`, so this does this. We also fatal with a useful error message if we get a collision, rather than failing silently. Reviewed By: shergin Differential Revision: D13384308 fbshipit-source-id: 0b06d7555b082be89e8f130c23e94be99749a7a3
This commit is contained in:
Родитель
a3b348eacb
Коммит
34ea65e3d9
|
@ -173,7 +173,7 @@ using namespace facebook::react;
|
||||||
|
|
||||||
_scheduler = [[RCTScheduler alloc] initWithContextContainer:self.contextContainer];
|
_scheduler = [[RCTScheduler alloc] initWithContextContainer:self.contextContainer];
|
||||||
_scheduler.delegate = self;
|
_scheduler.delegate = self;
|
||||||
|
|
||||||
return _scheduler;
|
return _scheduler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,14 +182,14 @@ using namespace facebook::react;
|
||||||
- (SharedContextContainer)contextContainer
|
- (SharedContextContainer)contextContainer
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(_contextContainerMutex);
|
std::lock_guard<std::mutex> lock(_contextContainerMutex);
|
||||||
|
|
||||||
if (_contextContainer) {
|
if (_contextContainer) {
|
||||||
return _contextContainer;
|
return _contextContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
_contextContainer = std::make_shared<ContextContainer>();
|
_contextContainer = std::make_shared<ContextContainer>();
|
||||||
|
|
||||||
_contextContainer->registerInstance(_reactNativeConfig);
|
_contextContainer->registerInstance(_reactNativeConfig, "ReactNativeConfig");
|
||||||
|
|
||||||
auto messageQueueThread = _batchedBridge.jsMessageThread;
|
auto messageQueueThread = _batchedBridge.jsMessageThread;
|
||||||
auto runtime = (facebook::jsi::Runtime *)((RCTCxxBridge *)_batchedBridge).runtime;
|
auto runtime = (facebook::jsi::Runtime *)((RCTCxxBridge *)_batchedBridge).runtime;
|
||||||
|
@ -214,7 +214,7 @@ using namespace facebook::react;
|
||||||
|
|
||||||
_contextContainer->registerInstance(runtimeExecutor, "runtime-executor");
|
_contextContainer->registerInstance(runtimeExecutor, "runtime-executor");
|
||||||
|
|
||||||
_contextContainer->registerInstance(std::make_shared<ImageManager>((__bridge void *)[_bridge imageLoader]));
|
_contextContainer->registerInstance(std::make_shared<ImageManager>((__bridge void *)[_bridge imageLoader]), "ImageManager");
|
||||||
return _contextContainer;
|
return _contextContainer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -27,7 +27,8 @@ class ImageComponentDescriptor final
|
||||||
: ConcreteComponentDescriptor(eventDispatcher),
|
: ConcreteComponentDescriptor(eventDispatcher),
|
||||||
imageManager_(
|
imageManager_(
|
||||||
contextContainer
|
contextContainer
|
||||||
? contextContainer->getInstance<SharedImageManager>()
|
? contextContainer->getInstance<SharedImageManager>(
|
||||||
|
"ImageManager")
|
||||||
: nullptr) {}
|
: nullptr) {}
|
||||||
|
|
||||||
void adopt(UnsharedShadowNode shadowNode) const override {
|
void adopt(UnsharedShadowNode shadowNode) const override {
|
||||||
|
|
|
@ -27,36 +27,39 @@ using SharedContextContainer = std::shared_ptr<ContextContainer>;
|
||||||
class ContextContainer final {
|
class ContextContainer final {
|
||||||
public:
|
public:
|
||||||
/*
|
/*
|
||||||
* Registers an instance of the particular type `T` in the container.
|
* Registers an instance of the particular type `T` in the container
|
||||||
* If `key` parameter is specified, the instance is registered
|
* using the provided `key`. Only one instance can be registered per key.
|
||||||
* by `{type, key}` pair.
|
*
|
||||||
|
* Convention is to use the plain base class name for the key, so for
|
||||||
|
* example if the type `T` is `std::shared_ptr<const ReactNativeConfig>`,
|
||||||
|
* then one would use `"ReactNativeConfig"` for the `key`, even if the
|
||||||
|
* instance is actually a `shared_ptr` of derived class
|
||||||
|
*`EmptyReactNativeConfig`.
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void registerInstance(const T &instance, const std::string &key = {}) {
|
void registerInstance(const T &instance, const std::string &key) {
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
|
||||||
instances_.insert(
|
const auto res = instances_.insert({key, std::make_shared<T>(instance)});
|
||||||
{{std::type_index(typeid(T)), key}, std::make_shared<T>(instance)});
|
if (res.second == false) {
|
||||||
|
LOG(FATAL) << "ContextContainer already had instance for key '" << key
|
||||||
|
<< "'";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Returns a previously registered instance of the particular type `T`.
|
* Returns a previously registered instance of the particular type `T`
|
||||||
* If `key` parameter is specified, the lookup will be performed
|
* for `key`.
|
||||||
* by {type, key} pair.
|
|
||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T getInstance(const std::string &key = {}) const {
|
T getInstance(const std::string &key) const {
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
|
|
||||||
return *std::static_pointer_cast<T>(
|
return *std::static_pointer_cast<T>(instances_.at(key));
|
||||||
instances_.at({std::type_index(typeid(T)), key}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unordered_map<
|
std::unordered_map<std::string, std::shared_ptr<void>> instances_;
|
||||||
std::pair<std::type_index, std::string>,
|
|
||||||
std::shared_ptr<void>>
|
|
||||||
instances_;
|
|
||||||
|
|
||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -28,7 +28,8 @@ Scheduler::Scheduler(const SharedContextContainer &contextContainer) {
|
||||||
contextContainer->getInstance<RuntimeExecutor>("runtime-executor");
|
contextContainer->getInstance<RuntimeExecutor>("runtime-executor");
|
||||||
|
|
||||||
reactNativeConfig_ =
|
reactNativeConfig_ =
|
||||||
contextContainer->getInstance<std::shared_ptr<const ReactNativeConfig>>();
|
contextContainer->getInstance<std::shared_ptr<const ReactNativeConfig>>(
|
||||||
|
"ReactNativeConfig");
|
||||||
|
|
||||||
auto uiManager = std::make_unique<UIManager>();
|
auto uiManager = std::make_unique<UIManager>();
|
||||||
auto &uiManagerRef = *uiManager;
|
auto &uiManagerRef = *uiManager;
|
||||||
|
|
Загрузка…
Ссылка в новой задаче