2018-05-31 22:31:03 +00:00
|
|
|
// 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.
|
2018-04-10 19:46:20 +00:00
|
|
|
|
|
|
|
#include "Scheduler.h"
|
|
|
|
|
|
|
|
#include <fabric/core/LayoutContext.h>
|
2018-05-18 03:03:41 +00:00
|
|
|
#include <fabric/uimanager/ComponentDescriptorRegistry.h>
|
|
|
|
#include <fabric/uimanager/FabricUIManager.h>
|
2018-04-10 19:46:20 +00:00
|
|
|
|
2018-05-19 02:44:34 +00:00
|
|
|
#include "ComponentDescriptorFactory.h"
|
2018-04-10 19:46:20 +00:00
|
|
|
#include "Differentiator.h"
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2018-06-22 14:28:34 +00:00
|
|
|
Scheduler::Scheduler(const SharedContextContainer &contextContainer):
|
|
|
|
contextContainer_(contextContainer) {
|
2018-06-01 16:36:25 +00:00
|
|
|
|
2018-08-27 14:21:26 +00:00
|
|
|
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)
|
|
|
|
);
|
2018-06-01 16:36:25 +00:00
|
|
|
|
2018-08-27 14:21:26 +00:00
|
|
|
uiManager_->setDelegate(this);
|
2018-04-10 19:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Scheduler::~Scheduler() {
|
|
|
|
uiManager_->setDelegate(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::registerRootTag(Tag rootTag) {
|
2018-09-04 05:53:28 +00:00
|
|
|
auto shadowTree = std::make_unique<ShadowTree>(rootTag);
|
2018-05-09 05:56:16 +00:00
|
|
|
shadowTree->setDelegate(this);
|
2018-09-04 05:53:28 +00:00
|
|
|
shadowTreeRegistry_.emplace(rootTag, std::move(shadowTree));
|
2018-04-10 19:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::unregisterRootTag(Tag rootTag) {
|
2018-06-22 14:28:36 +00:00
|
|
|
const auto &iterator = shadowTreeRegistry_.find(rootTag);
|
|
|
|
const auto &shadowTree = iterator->second;
|
2018-05-09 05:56:16 +00:00
|
|
|
assert(shadowTree);
|
|
|
|
shadowTree->setDelegate(nullptr);
|
|
|
|
shadowTreeRegistry_.erase(iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
Size Scheduler::measure(const Tag &rootTag, const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const {
|
2018-06-22 14:28:36 +00:00
|
|
|
const auto &shadowTree = shadowTreeRegistry_.at(rootTag);
|
2018-05-09 05:56:16 +00:00
|
|
|
assert(shadowTree);
|
|
|
|
return shadowTree->measure(layoutConstraints, layoutContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Scheduler::constraintLayout(const Tag &rootTag, const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) {
|
2018-06-22 14:28:36 +00:00
|
|
|
const auto &shadowTree = shadowTreeRegistry_.at(rootTag);
|
2018-05-09 05:56:16 +00:00
|
|
|
assert(shadowTree);
|
|
|
|
return shadowTree->constraintLayout(layoutConstraints, layoutContext);
|
2018-04-10 19:46:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Delegate
|
|
|
|
|
|
|
|
void Scheduler::setDelegate(SchedulerDelegate *delegate) {
|
|
|
|
delegate_ = delegate;
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:56:16 +00:00
|
|
|
SchedulerDelegate *Scheduler::getDelegate() const {
|
2018-04-10 19:46:20 +00:00
|
|
|
return delegate_;
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:56:16 +00:00
|
|
|
#pragma mark - ShadowTreeDelegate
|
2018-04-10 19:46:20 +00:00
|
|
|
|
2018-09-04 05:53:28 +00:00
|
|
|
void Scheduler::shadowTreeDidCommit(const ShadowTree &shadowTree, const ShadowViewMutationList &mutations) {
|
2018-04-10 19:46:20 +00:00
|
|
|
if (delegate_) {
|
2018-09-04 05:53:28 +00:00
|
|
|
delegate_->schedulerDidFinishTransaction(shadowTree.getRootTag(), mutations);
|
2018-04-10 19:46:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 05:56:16 +00:00
|
|
|
#pragma mark - UIManagerDelegate
|
2018-05-31 22:31:03 +00:00
|
|
|
|
2018-05-09 05:56:16 +00:00
|
|
|
void Scheduler::uiManagerDidFinishTransaction(Tag rootTag, const SharedShadowNodeUnsharedList &rootChildNodes) {
|
2018-09-08 06:38:49 +00:00
|
|
|
const auto iterator = shadowTreeRegistry_.find(rootTag);
|
|
|
|
if (iterator == shadowTreeRegistry_.end()) {
|
|
|
|
// This might happen during surface unmounting/deallocation process
|
|
|
|
// due to the asynchronous nature of JS calls.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return iterator->second->complete(rootChildNodes);
|
2018-05-09 05:56:16 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 19:46:20 +00:00
|
|
|
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
|