Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "MutationObserver.h"
#include <react/renderer/core/ShadowNodeTraits.h>
#include <react/renderer/uimanager/primitives.h>
#include <unordered_map>

namespace facebook::react {

Expand Down Expand Up @@ -150,12 +151,38 @@ void MutationObserver::recordMutationsInSubtrees(
return;
}

struct ChildMatch {
size_t index;
bool seenInOld{false};
};
const bool useIndex = oldChildren.size() >= 16 && newChildren.size() >= 16;
std::unordered_map<const ShadowNodeFamily*, ChildMatch> newChildMatches;
if (useIndex) {
newChildMatches.reserve(newChildren.size());
for (size_t i = 0; i < newChildren.size(); ++i) {
newChildMatches.try_emplace(
&newChildren[i]->getFamily(), ChildMatch{.index = i});
}
}

const auto findNewChild = [&](const ShadowNode& oldChild) {
if (!useIndex) {
return findNodeOfSameFamily(newChildren, oldChild);
}
const auto it = newChildMatches.find(&oldChild.getFamily());
if (it == newChildMatches.end()) {
return std::shared_ptr<const ShadowNode>{};
}
it->second.seenInOld = true;
return newChildren[it->second.index];
};

std::vector<std::shared_ptr<const ShadowNode>> addedNodes;
std::vector<std::shared_ptr<const ShadowNode>> removedNodes;

// Check for removed nodes (and equal nodes for further inspection)
for (auto& oldChild : oldChildren) {
auto newChild = findNodeOfSameFamily(newChildren, *oldChild);
for (const auto& oldChild : oldChildren) {
const auto newChild = findNewChild(*oldChild);
if (!newChild) {
removedNodes.push_back(oldChild);
} else if (observeSubtree) {
Expand All @@ -171,9 +198,11 @@ void MutationObserver::recordMutationsInSubtrees(
}

// Check for added nodes
for (auto& newChild : newChildren) {
auto oldChild = findNodeOfSameFamily(oldChildren, *newChild);
if (!oldChild) {
for (const auto& newChild : newChildren) {
const bool isAdded = useIndex
? !newChildMatches.at(&newChild->getFamily()).seenInOld
: !findNodeOfSameFamily(oldChildren, *newChild);
if (isAdded) {
addedNodes.push_back(newChild);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,105 @@ describe('MutationObserver', () => {
expect([...secondRecords[0].removedNodes]).toEqual([node111]);
});

for (const width of [15, 16, 32]) {
it(`should preserve record order when ${width} children are reordered and mutated`, () => {
const root = Fantom.createRoot();
const parentRef = createRef<HostInstance>();
const firstChangedIndex = 4;
const secondChangedIndex = 8;
const childRefs = Array.from({length: width}, () =>
createRef<HostInstance>(),
);
const firstDescendantRef = createRef<HostInstance>();
const secondDescendantRef = createRef<HostInstance>();
const firstAddedRef = createRef<HostInstance>();
const secondAddedRef = createRef<HostInstance>();

Fantom.runTask(() => {
root.render(
<View ref={parentRef}>
{childRefs.map((ref, i) => (
<View key={i} ref={ref} />
))}
</View>,
);
});

const parent = nullthrows(parentRef.current);
const originalChildren = childRefs.map(ref => nullthrows(ref.current));
let deepRecords: ReadonlyArray<MutationRecordType> = [];
let shallowRecords: ReadonlyArray<MutationRecordType> = [];
const deepObserver = new MutationObserver(records => {
deepRecords = records;
});
const shallowObserver = new MutationObserver(records => {
shallowRecords = records;
});
deepObserver.observe(parent, {childList: true, subtree: true});
deepObserver.observe(originalChildren[firstChangedIndex], {
childList: true,
});
shallowObserver.observe(parent, {childList: true});

Fantom.runTask(() => {
const updatedChildren = childRefs
.slice(2)
.map((ref, offset) => {
const i = offset + 2;
return (
<View key={i} ref={ref}>
{i === firstChangedIndex ? (
<View ref={firstDescendantRef} />
) : null}
{i === secondChangedIndex ? (
<View ref={secondDescendantRef} />
) : null}
</View>
);
})
.reverse();
root.render(
<View ref={parentRef}>
{[
<View key="added-first" ref={firstAddedRef} />,
...updatedChildren,
<View key="added-second" ref={secondAddedRef} />,
]}
</View>,
);
});

const nodes: ReadonlyArray<unknown> = [
parent,
...originalChildren,
nullthrows(firstDescendantRef.current),
nullthrows(secondDescendantRef.current),
nullthrows(firstAddedRef.current),
nullthrows(secondAddedRef.current),
];
const summarizeRecord = (record: MutationRecordType) => ({
target: nodes.indexOf(record.target),
added: [...record.addedNodes].map(node => nodes.indexOf(node)),
removed: [...record.removedNodes].map(node => nodes.indexOf(node)),
});
const parentRecord = {
target: 0,
added: [width + 3, width + 4],
removed: [1, 2],
};
expect(deepRecords.map(summarizeRecord)).toEqual([
{target: firstChangedIndex + 1, added: [width + 1], removed: []},
{target: secondChangedIndex + 1, added: [width + 2], removed: []},
parentRecord,
]);
expect(shallowRecords.map(summarizeRecord)).toEqual([parentRecord]);

deepObserver.disconnect();
shallowObserver.disconnect();
root.destroy();
});
}

it('should report changes in different parts of the subtree as separate entries (subtree = true)', () => {
const nodeRef = createRef<HostInstance>();

Expand Down
Loading