Skip to content

Commit

Permalink
Fabric: Introducing ContextContainer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
shergin authored and facebook-github-bot committed Jun 22, 2018
1 parent 0a20f47 commit c674303
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 6 deletions.
5 changes: 4 additions & 1 deletion React/Fabric/RCTScheduler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import "RCTScheduler.h"

#import <fabric/uimanager/ContextContainer.h>
#import <fabric/uimanager/Scheduler.h>
#import <fabric/uimanager/SchedulerDelegate.h>

Expand Down Expand Up @@ -41,7 +42,9 @@ - (instancetype)init
{
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());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@

#include <fabric/uimanager/ComponentDescriptorFactory.h>
#include <fabric/uimanager/ComponentDescriptorRegistry.h>
#include <fabric/uimanager/ContextContainer.h>

namespace facebook {
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;
}
Expand Down
3 changes: 2 additions & 1 deletion ReactCommon/fabric/uimanager/ComponentDescriptorFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <fabric/core/ComponentDescriptor.h>
#include <fabric/core/EventDispatcher.h>
#include <fabric/uimanager/ContextContainer.h>

#include "ComponentDescriptorRegistry.h"

Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions ReactCommon/fabric/uimanager/ContextContainer.cpp
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions ReactCommon/fabric/uimanager/ContextContainer.h
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions ReactCommon/fabric/uimanager/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion ReactCommon/fabric/uimanager/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -31,7 +32,7 @@ class Scheduler final:

public:

Scheduler();
Scheduler(const SharedContextContainer &contextContainer);
~Scheduler();

#pragma mark - Shadow Tree Management
Expand Down Expand Up @@ -74,6 +75,7 @@ class Scheduler final:
std::shared_ptr<FabricUIManager> uiManager_;
std::unordered_map<Tag, SharedShadowTree> shadowTreeRegistry_;
SharedSchedulerEventDispatcher eventDispatcher_;
SharedContextContainer contextContainer_;
};

} // namespace react
Expand Down

0 comments on commit c674303

Please sign in to comment.