Fabric: Introducing `ContextContainer`

Summary:
@public
`ContextContainer` is general purpose DI container for Fabric.
We need this to communicate some enviroment-specific and/or platform-specific modules down to cross-platform C++ code.
The first one will be ImageManager. Soon.

Reviewed By: fkgozali

Differential Revision: D8475636

fbshipit-source-id: 0afc65063f818d0bab736cd2c55c6fdd21b629ac
This commit is contained in:
Valentin Shergin 2018-06-22 07:28:34 -07:00 committed by Facebook Github Bot
parent 0a20f47021
commit c674303dfd
7 changed files with 79 additions and 6 deletions

View File

@ -7,6 +7,7 @@
#import "RCTScheduler.h"
#import <fabric/uimanager/ContextContainer.h>
#import <fabric/uimanager/Scheduler.h>
#import <fabric/uimanager/SchedulerDelegate.h>
@ -41,7 +42,9 @@ private:
{
if (self = [super init]) {
_delegateProxy = std::make_shared<SchedulerDelegateProxy>((__bridge void *)self);
_scheduler = std::make_shared<Scheduler>();
SharedContextContainer contextContainer = std::make_shared<ContextContainer>();
_scheduler = std::make_shared<Scheduler>(contextContainer);
_scheduler->setDelegate(_delegateProxy.get());
}

View File

@ -7,6 +7,7 @@
#include <fabric/uimanager/ComponentDescriptorFactory.h>
#include <fabric/uimanager/ComponentDescriptorRegistry.h>
#include <fabric/uimanager/ContextContainer.h>
namespace facebook {
namespace react {
@ -14,7 +15,10 @@ namespace react {
/**
* This is a sample implementation. Each app should provide its own.
*/
SharedComponentDescriptorRegistry ComponentDescriptorFactory::buildRegistry(const SharedEventDispatcher &eventDispatcher) {
SharedComponentDescriptorRegistry ComponentDescriptorFactory::buildRegistry(
const SharedEventDispatcher &eventDispatcher,
const SharedContextContainer &contextContainer
) {
auto registry = std::make_shared<ComponentDescriptorRegistry>();
return registry;
}

View File

@ -11,6 +11,7 @@
#include <fabric/core/ComponentDescriptor.h>
#include <fabric/core/EventDispatcher.h>
#include <fabric/uimanager/ContextContainer.h>
#include "ComponentDescriptorRegistry.h"
@ -25,7 +26,7 @@ namespace react {
class ComponentDescriptorFactory {
public:
static SharedComponentDescriptorRegistry buildRegistry(const SharedEventDispatcher &eventDispatcher);
static SharedComponentDescriptorRegistry buildRegistry(const SharedEventDispatcher &eventDispatcher, const SharedContextContainer &contextContainer);
};
} // namespace react

View File

@ -0,0 +1,22 @@
// Copyright (c) 2004-present, Facebook, Inc.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include "ContextContainer.h"
namespace facebook {
namespace react {
void ContextContainer::registerInstance(const ClassHandle &handle, SharedInstance instance) {
std::lock_guard<std::mutex> lock(mutex_);
instances_.insert({handle, instance});
}
const ContextContainer::SharedInstance &ContextContainer::at(const ClassHandle &handle) const {
std::lock_guard<std::mutex> lock(mutex_);
return instances_.at(handle);
}
} // namespace react
} // namespace facebook

View File

@ -0,0 +1,40 @@
// Copyright (c) 2004-present, Facebook, Inc.
// 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 <memory>
#include <mutex>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
namespace facebook {
namespace react {
class ContextContainer;
using SharedContextContainer = std::shared_ptr<ContextContainer>;
/*
* General purpose dependecy injection container.
*/
class ContextContainer final {
public:
using ClassHandle = std::type_index;
using SharedInstance = std::shared_ptr<void>;
void registerInstance(const ClassHandle &handle, SharedInstance instance);
const SharedInstance &at(const ClassHandle &handle) const;
private:
std::unordered_map<ClassHandle, SharedInstance> instances_;
mutable std::mutex mutex_;
};
} // namespace react
} // namespace facebook

View File

@ -22,9 +22,10 @@
namespace facebook {
namespace react {
Scheduler::Scheduler() {
Scheduler::Scheduler(const SharedContextContainer &contextContainer):
contextContainer_(contextContainer) {
auto &&eventDispatcher = std::make_shared<SchedulerEventDispatcher>();
auto &&componentDescriptorRegistry = ComponentDescriptorFactory::buildRegistry(eventDispatcher);
auto &&componentDescriptorRegistry = ComponentDescriptorFactory::buildRegistry(eventDispatcher, contextContainer);
uiManager_ = std::make_shared<FabricUIManager>(componentDescriptorRegistry);
uiManager_->setDelegate(this);

View File

@ -9,6 +9,7 @@
#include <fabric/core/ComponentDescriptor.h>
#include <fabric/core/LayoutConstraints.h>
#include <fabric/uimanager/ContextContainer.h>
#include <fabric/uimanager/SchedulerDelegate.h>
#include <fabric/uimanager/SchedulerEventDispatcher.h>
#include <fabric/uimanager/UIManagerDelegate.h>
@ -31,7 +32,7 @@ class Scheduler final:
public:
Scheduler();
Scheduler(const SharedContextContainer &contextContainer);
~Scheduler();
#pragma mark - Shadow Tree Management
@ -74,6 +75,7 @@ private:
std::shared_ptr<FabricUIManager> uiManager_;
std::unordered_map<Tag, SharedShadowTree> shadowTreeRegistry_;
SharedSchedulerEventDispatcher eventDispatcher_;
SharedContextContainer contextContainer_;
};
} // namespace react