diff --git a/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp b/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp index 2bc1036375d1..f850b14885ef 100644 --- a/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp +++ b/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp @@ -8,6 +8,7 @@ #include "MutationObserver.h" #include #include +#include namespace facebook::react { @@ -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 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{}; + } + it->second.seenInOld = true; + return newChildren[it->second.index]; + }; + std::vector> addedNodes; std::vector> 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) { @@ -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); } } diff --git a/packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-itest.js b/packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-itest.js index 4d07791caf25..28b5262ff299 100644 --- a/packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-itest.js +++ b/packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-itest.js @@ -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(); + const firstChangedIndex = 4; + const secondChangedIndex = 8; + const childRefs = Array.from({length: width}, () => + createRef(), + ); + const firstDescendantRef = createRef(); + const secondDescendantRef = createRef(); + const firstAddedRef = createRef(); + const secondAddedRef = createRef(); + + Fantom.runTask(() => { + root.render( + + {childRefs.map((ref, i) => ( + + ))} + , + ); + }); + + const parent = nullthrows(parentRef.current); + const originalChildren = childRefs.map(ref => nullthrows(ref.current)); + let deepRecords: ReadonlyArray = []; + let shallowRecords: ReadonlyArray = []; + 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 ( + + {i === firstChangedIndex ? ( + + ) : null} + {i === secondChangedIndex ? ( + + ) : null} + + ); + }) + .reverse(); + root.render( + + {[ + , + ...updatedChildren, + , + ]} + , + ); + }); + + const nodes: ReadonlyArray = [ + 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();