/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include namespace facebook::react { using MutationObserverId = int32_t; struct MutationRecord { MutationObserverId mutationObserverId; std::shared_ptr targetShadowNode; std::vector> addedShadowNodes; std::vector> removedShadowNodes; }; class MutationObserver { public: explicit MutationObserver(MutationObserverId mutationObserverId); // delete copy constructor MutationObserver(const MutationObserver&) = delete; // delete copy assignment MutationObserver& operator=(const MutationObserver&) = delete; // allow move constructor MutationObserver(MutationObserver&&) = default; // allow move assignment MutationObserver& operator=(MutationObserver&&) = default; void observe( std::shared_ptr targetShadowNodeFamily, bool observeSubtree); void recordMutations( const RootShadowNode& oldRootShadowNode, const RootShadowNode& newRootShadowNode, std::vector& recordedMutations) const; private: MutationObserverId mutationObserverId_; std::vector> deeplyObservedShadowNodeFamilies_; std::vector> shallowlyObservedShadowNodeFamilies_; using SetOfShadowNodePointers = std::unordered_set; void recordMutationsInTarget( const ShadowNodeFamily& targetShadowNodeFamily, const RootShadowNode& oldRootShadowNode, const RootShadowNode& newRootShadowNode, bool observeSubtree, std::vector& recordedMutations, SetOfShadowNodePointers& processedNodes) const; void recordMutationsInSubtrees( const std::shared_ptr& oldNode, const std::shared_ptr& newNode, bool observeSubtree, std::vector& recordedMutations, SetOfShadowNodePointers& processedNodes) const; }; } // namespace facebook::react