Valentin Shergin 0792fba63f Fabric: Using ShadowView instead of ShadowNode in Mutations
Summary:
@public
This is quite a big diff but the actual meaningful change is simple: now we use ShadowView class instead of ShadowNode in mutation instructions.
Note:
 * In some places (especially during diffing) we have to operate with ShadowNodeViewPair objects (which represents a pair of ShadowNode and ShadowView). The reason for that is that we cannot construct child ShadowViews from parent ShadowViews because they don't have any information about children.
 * `ShadowTree::emitLayoutEvents` is now much simpler because ShadowView better represents the specifics of this kind of object.
 * The code in RCTMountingManager also became simpler.

This change will allow us to implement more cool tricks soon.

Reviewed By: mdvacca

Differential Revision: D9403564

fbshipit-source-id: dbc7c61af250144d6c7335a01dc30df0005559a2
2018-09-03 23:04:20 -07:00

114 lines
3.3 KiB
C++

// 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 "Scheduler.h"
#include <fabric/core/LayoutContext.h>
#include <fabric/uimanager/ComponentDescriptorRegistry.h>
#include <fabric/uimanager/FabricUIManager.h>
#include "ComponentDescriptorFactory.h"
#include "Differentiator.h"
namespace facebook {
namespace react {
Scheduler::Scheduler(const SharedContextContainer &contextContainer):
contextContainer_(contextContainer) {
uiManager_ = std::make_shared<FabricUIManager>();
auto eventDispatcher =
std::make_shared<EventDispatcher>(
std::bind(
&FabricUIManager::dispatchEventToTarget,
uiManager_.get(),
std::placeholders::_1,
std::placeholders::_2,
std::placeholders::_3
),
contextContainer->getInstance<EventBeatFactory>("synchronous"),
contextContainer->getInstance<EventBeatFactory>("asynchronous")
);
uiManager_->setComponentDescriptorRegistry(
ComponentDescriptorFactory::buildRegistry(eventDispatcher, contextContainer)
);
uiManager_->setDelegate(this);
}
Scheduler::~Scheduler() {
uiManager_->setDelegate(nullptr);
}
void Scheduler::registerRootTag(Tag rootTag) {
const auto &shadowTree = std::make_shared<ShadowTree>(rootTag);
shadowTree->setDelegate(this);
shadowTreeRegistry_.insert({rootTag, shadowTree});
}
void Scheduler::unregisterRootTag(Tag rootTag) {
const auto &iterator = shadowTreeRegistry_.find(rootTag);
const auto &shadowTree = iterator->second;
assert(shadowTree);
shadowTree->setDelegate(nullptr);
shadowTreeRegistry_.erase(iterator);
}
Size Scheduler::measure(const Tag &rootTag, const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const {
const auto &shadowTree = shadowTreeRegistry_.at(rootTag);
assert(shadowTree);
return shadowTree->measure(layoutConstraints, layoutContext);
}
void Scheduler::constraintLayout(const Tag &rootTag, const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) {
const auto &shadowTree = shadowTreeRegistry_.at(rootTag);
assert(shadowTree);
return shadowTree->constraintLayout(layoutConstraints, layoutContext);
}
#pragma mark - Delegate
void Scheduler::setDelegate(SchedulerDelegate *delegate) {
delegate_ = delegate;
}
SchedulerDelegate *Scheduler::getDelegate() const {
return delegate_;
}
#pragma mark - ShadowTreeDelegate
void Scheduler::shadowTreeDidCommit(const SharedShadowTree &shadowTree, const ShadowViewMutationList &mutations) {
if (delegate_) {
delegate_->schedulerDidFinishTransaction(shadowTree->getRootTag(), mutations);
}
}
#pragma mark - UIManagerDelegate
void Scheduler::uiManagerDidFinishTransaction(Tag rootTag, const SharedShadowNodeUnsharedList &rootChildNodes) {
const auto &iterator = shadowTreeRegistry_.find(rootTag);
const auto &shadowTree = iterator->second;
assert(shadowTree);
return shadowTree->complete(rootChildNodes);
}
void Scheduler::uiManagerDidCreateShadowNode(const SharedShadowNode &shadowNode) {
if (delegate_) {
delegate_->schedulerDidRequestPreliminaryViewAllocation(shadowNode->getComponentName());
}
}
#pragma mark - Deprecated
std::shared_ptr<FabricUIManager> Scheduler::getUIManager_DO_NOT_USE() {
return uiManager_;
}
} // namespace react
} // namespace facebook