Files
react-native/ReactCommon/fabric/mounting/stubs/stubs.cpp
T
Valentin Shergin b0273bbe70 Fabric: Reimplementation of stubViewTreeFromShadowNode
Summary:
`stubViewTreeFromShadowNode` was implemented without using `calculateShadowViewMutations` (aka Diffing algorithm).
The problem is that we use `stubViewTreeFromShadowNode` to test the diffing, so it was not fully correct to use the algorithm to test itself.

Reviewed By: JoshuaGross

Differential Revision: D16342526

fbshipit-source-id: 2674245ed5ec71309fe5d362779a33d8dd31dc7b
2019-07-17 14:22:02 -07:00

64 lines
2.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 "stubs.h"
#include <react/core/LayoutableShadowNode.h>
#include <react/core/ShadowNodeFragment.h>
#include <react/mounting/Differentiator.h>
namespace facebook {
namespace react {
/*
* Generates `create` and `insert` instructions recursively traversing a shadow
* tree.
* This is a trivial implementation of diffing algorithm that can only "diff"
* an empty tree with some other one.
*/
static void calculateShadowViewMutationsForNewTree(
ShadowViewMutation::List &mutations,
ShadowView const &parentShadowView,
ShadowViewNodePair::List const &newChildPairs) {
for (auto index = 0; index < newChildPairs.size(); index++) {
auto const &newChildPair = newChildPairs[index];
mutations.push_back(
ShadowViewMutation::CreateMutation(newChildPair.shadowView));
mutations.push_back(ShadowViewMutation::InsertMutation(
parentShadowView, newChildPair.shadowView, index));
auto const newGrandChildPairs =
sliceChildShadowNodeViewPairs(*newChildPair.shadowNode);
calculateShadowViewMutationsForNewTree(
mutations, newChildPair.shadowView, newGrandChildPairs);
}
}
StubViewTree stubViewTreeFromShadowNode(ShadowNode const &rootShadowNode) {
auto mutations = ShadowViewMutation::List{};
mutations.reserve(256);
calculateShadowViewMutationsForNewTree(
mutations,
ShadowView(rootShadowNode),
sliceChildShadowNodeViewPairs(rootShadowNode));
auto emptyRootShadowNode = rootShadowNode.clone(
ShadowNodeFragment{ShadowNodeFragment::tagPlaceholder(),
ShadowNodeFragment::surfaceIdPlaceholder(),
ShadowNodeFragment::propsPlaceholder(),
ShadowNodeFragment::eventEmitterPlaceholder(),
ShadowNode::emptySharedShadowNodeSharedList()});
auto stubViewTree = StubViewTree(ShadowView(*emptyRootShadowNode));
stubViewTree.mutate(mutations);
return stubViewTree;
}
} // namespace react
} // namespace facebook