mirror of
https://github.com/status-im/react-native.git
synced 2025-02-24 15:18:10 +00:00
Summary: Now it's parts of RootShadowNode and Scheduler. Reviewed By: sahrens Differential Revision: D13615364 fbshipit-source-id: 13dbea1e69ef51b2679101915c01c6be7e15d859
222 lines
6.1 KiB
C++
222 lines
6.1 KiB
C++
// Copyright (c) Facebook, Inc. and its affiliates.
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
// LICENSE file in the root directory of this source tree.
|
|
|
|
#include "ShadowTree.h"
|
|
|
|
#include <react/core/LayoutContext.h>
|
|
#include <react/core/LayoutPrimitives.h>
|
|
#include <react/debug/SystraceSection.h>
|
|
#include <react/mounting/Differentiator.h>
|
|
#include <react/mounting/ShadowViewMutation.h>
|
|
|
|
#include "ShadowTreeDelegate.h"
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
ShadowTree::ShadowTree(
|
|
SurfaceId surfaceId,
|
|
const LayoutConstraints &layoutConstraints,
|
|
const LayoutContext &layoutContext)
|
|
: surfaceId_(surfaceId) {
|
|
const auto noopEventEmitter = std::make_shared<const ViewEventEmitter>(
|
|
nullptr, -1, std::shared_ptr<const EventDispatcher>());
|
|
|
|
const auto props = std::make_shared<const RootProps>(
|
|
*RootShadowNode::defaultSharedProps(), layoutConstraints, layoutContext);
|
|
|
|
rootShadowNode_ = std::make_shared<RootShadowNode>(
|
|
ShadowNodeFragment{
|
|
.tag = surfaceId,
|
|
.rootTag = surfaceId,
|
|
.props = props,
|
|
.eventEmitter = noopEventEmitter,
|
|
},
|
|
nullptr);
|
|
}
|
|
|
|
ShadowTree::~ShadowTree() {
|
|
commit([](const SharedRootShadowNode &oldRootShadowNode) {
|
|
return std::make_shared<RootShadowNode>(
|
|
*oldRootShadowNode,
|
|
ShadowNodeFragment{.children =
|
|
ShadowNode::emptySharedShadowNodeSharedList()});
|
|
});
|
|
}
|
|
|
|
Tag ShadowTree::getSurfaceId() const {
|
|
return surfaceId_;
|
|
}
|
|
|
|
SharedRootShadowNode ShadowTree::getRootShadowNode() const {
|
|
std::shared_lock<folly::SharedMutex> lock(commitMutex_);
|
|
return rootShadowNode_;
|
|
}
|
|
|
|
bool ShadowTree::completeByReplacingShadowNode(
|
|
const SharedShadowNode &oldShadowNode,
|
|
const SharedShadowNode &newShadowNode) const {
|
|
return commit([&](const SharedRootShadowNode &oldRootShadowNode) {
|
|
std::vector<std::reference_wrapper<const ShadowNode>> ancestors;
|
|
oldShadowNode->constructAncestorPath(*oldRootShadowNode, ancestors);
|
|
|
|
if (ancestors.size() == 0) {
|
|
return UnsharedRootShadowNode{nullptr};
|
|
}
|
|
|
|
auto oldChild = oldShadowNode;
|
|
auto newChild = newShadowNode;
|
|
|
|
SharedShadowNodeUnsharedList sharedChildren;
|
|
|
|
for (const auto &ancestor : ancestors) {
|
|
auto children = ancestor.get().getChildren();
|
|
std::replace(children.begin(), children.end(), oldChild, newChild);
|
|
|
|
sharedChildren = std::make_shared<SharedShadowNodeList>(children);
|
|
|
|
oldChild = ancestor.get().shared_from_this();
|
|
newChild =
|
|
oldChild->clone(ShadowNodeFragment{.children = sharedChildren});
|
|
}
|
|
|
|
return std::make_shared<RootShadowNode>(
|
|
*oldRootShadowNode, ShadowNodeFragment{.children = sharedChildren});
|
|
});
|
|
}
|
|
|
|
bool ShadowTree::commit(
|
|
std::function<UnsharedRootShadowNode(
|
|
const SharedRootShadowNode &oldRootShadowNode)> transaction,
|
|
int attempts,
|
|
int *revision) const {
|
|
SystraceSection s("ShadowTree::commit");
|
|
|
|
while (attempts) {
|
|
attempts--;
|
|
|
|
SharedRootShadowNode oldRootShadowNode;
|
|
|
|
{
|
|
// Reading `rootShadowNode_` in shared manner.
|
|
std::shared_lock<folly::SharedMutex> lock(commitMutex_);
|
|
oldRootShadowNode = rootShadowNode_;
|
|
}
|
|
|
|
UnsharedRootShadowNode newRootShadowNode = transaction(oldRootShadowNode);
|
|
|
|
if (!newRootShadowNode) {
|
|
break;
|
|
}
|
|
|
|
newRootShadowNode->layout();
|
|
newRootShadowNode->sealRecursive();
|
|
|
|
auto mutations =
|
|
calculateShadowViewMutations(*oldRootShadowNode, *newRootShadowNode);
|
|
|
|
{
|
|
// Updating `rootShadowNode_` in unique manner if it hasn't changed.
|
|
std::unique_lock<folly::SharedMutex> lock(commitMutex_);
|
|
|
|
if (rootShadowNode_ != oldRootShadowNode) {
|
|
continue;
|
|
}
|
|
|
|
rootShadowNode_ = newRootShadowNode;
|
|
|
|
toggleEventEmitters(mutations);
|
|
|
|
revision_++;
|
|
|
|
// Returning last revision if requested.
|
|
if (revision) {
|
|
*revision = revision_;
|
|
}
|
|
}
|
|
|
|
emitLayoutEvents(mutations);
|
|
|
|
if (delegate_) {
|
|
delegate_->shadowTreeDidCommit(*this, mutations);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void ShadowTree::emitLayoutEvents(
|
|
const ShadowViewMutationList &mutations) const {
|
|
SystraceSection s("ShadowTree::emitLayoutEvents");
|
|
|
|
for (const auto &mutation : mutations) {
|
|
// Only `Insert` and `Update` mutations can affect layout metrics.
|
|
if (mutation.type != ShadowViewMutation::Insert &&
|
|
mutation.type != ShadowViewMutation::Update) {
|
|
continue;
|
|
}
|
|
|
|
const auto viewEventEmitter =
|
|
std::dynamic_pointer_cast<const ViewEventEmitter>(
|
|
mutation.newChildShadowView.eventEmitter);
|
|
|
|
// Checking if particular shadow node supports `onLayout` event (part of
|
|
// `ViewEventEmitter`).
|
|
if (!viewEventEmitter) {
|
|
continue;
|
|
}
|
|
|
|
// Checking if the `onLayout` event was requested for the particular Shadow
|
|
// Node.
|
|
const auto viewProps = std::dynamic_pointer_cast<const ViewProps>(
|
|
mutation.newChildShadowView.props);
|
|
if (viewProps && !viewProps->onLayout) {
|
|
continue;
|
|
}
|
|
|
|
// In case if we have `oldChildShadowView`, checking that layout metrics
|
|
// have changed.
|
|
if (mutation.type != ShadowViewMutation::Update &&
|
|
mutation.oldChildShadowView.layoutMetrics ==
|
|
mutation.newChildShadowView.layoutMetrics) {
|
|
continue;
|
|
}
|
|
|
|
viewEventEmitter->onLayout(mutation.newChildShadowView.layoutMetrics);
|
|
}
|
|
}
|
|
|
|
void ShadowTree::toggleEventEmitters(
|
|
const ShadowViewMutationList &mutations) const {
|
|
std::lock_guard<std::recursive_mutex> lock(EventEmitter::DispatchMutex());
|
|
|
|
for (const auto &mutation : mutations) {
|
|
if (mutation.type == ShadowViewMutation::Create) {
|
|
mutation.newChildShadowView.eventEmitter->enable();
|
|
}
|
|
}
|
|
|
|
for (const auto &mutation : mutations) {
|
|
if (mutation.type == ShadowViewMutation::Delete) {
|
|
mutation.oldChildShadowView.eventEmitter->disable();
|
|
}
|
|
}
|
|
}
|
|
|
|
#pragma mark - Delegate
|
|
|
|
void ShadowTree::setDelegate(ShadowTreeDelegate const *delegate) {
|
|
delegate_ = delegate;
|
|
}
|
|
|
|
ShadowTreeDelegate const *ShadowTree::getDelegate() const {
|
|
return delegate_;
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|