/* * 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 #include #include #include "IntersectionObserver.h" namespace facebook::react { class IntersectionObserverManager final : public UIManagerMountHook { public: IntersectionObserverManager(); void observe( IntersectionObserverObserverId intersectionObserverId, const ShadowNode::Shared& shadowNode, std::vector thresholds, const UIManager& uiManager); void unobserve( IntersectionObserverObserverId intersectionObserverId, const ShadowNode& shadowNode); void connect( UIManager& uiManager, std::function notifyIntersectionObserversCallback); void disconnect(UIManager& uiManager); std::vector takeRecords(); #pragma mark - UIManagerMountHook void shadowTreeDidMount( const RootShadowNode::Shared& rootShadowNode, double mountTime) noexcept override; private: mutable std::unordered_map> observersBySurfaceId_; mutable std::shared_mutex observersMutex_; mutable std::function notifyIntersectionObserversCallback_; mutable std::vector pendingEntries_; mutable std::mutex pendingEntriesMutex_; mutable bool notifiedIntersectionObservers_{}; mutable bool mountHookRegistered_{}; void notifyObserversIfNecessary(); void notifyObservers(); // Equivalent to // https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo void updateIntersectionObservations( const RootShadowNode& rootShadowNode, double mountTime); const IntersectionObserver& getRegisteredIntersectionObserver( SurfaceId surfaceId, IntersectionObserverObserverId observerId) const; }; } // namespace facebook::react