mirror of
https://github.com/status-im/react-native.git
synced 2025-02-23 22:58:19 +00:00
Summary: Trivial. The name `visit` expresss intent more clearly and fits C++17 `std::visit` pattern. Reviewed By: mdvacca Differential Revision: D13269744 fbshipit-source-id: 6aa6569c58aa66673e3f3a1662390a74290b8e01
59 lines
1.7 KiB
C++
59 lines
1.7 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.
|
|
|
|
#pragma once
|
|
|
|
#include <folly/SharedMutex.h>
|
|
#include <shared_mutex>
|
|
|
|
#include <react/core/ReactPrimitives.h>
|
|
#include <react/uimanager/ShadowTree.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
/*
|
|
* Owning registry of `ShadowTree`s.
|
|
*/
|
|
class ShadowTreeRegistry final {
|
|
public:
|
|
ShadowTreeRegistry() = default;
|
|
|
|
/*
|
|
* Adds a `ShadowTree` instance to the registry.
|
|
* The ownership of the instance is also transferred to the registry.
|
|
* Can be called from any thread.
|
|
*/
|
|
void add(std::unique_ptr<ShadowTree> &&shadowTree) const;
|
|
|
|
/*
|
|
* Removes a `ShadowTree` instance with given `surfaceId` from the registry
|
|
* and returns it as a result.
|
|
* The ownership of the instance is also transferred to the caller.
|
|
* Can be called from any thread.
|
|
*/
|
|
std::unique_ptr<ShadowTree> remove(SurfaceId surfaceId) const;
|
|
|
|
/*
|
|
* Finds a `ShadowTree` instance with a given `surfaceId` in the registry and
|
|
* synchronously calls the `callback` with a reference to the instance while
|
|
* the mutex is being acquired.
|
|
* Returns `true` if the registry has `ShadowTree` instance with corresponding
|
|
* `surfaceId`, otherwise returns `false` without calling the `callback`.
|
|
* Can be called from any thread.
|
|
*/
|
|
bool visit(
|
|
SurfaceId surfaceId,
|
|
std::function<void(const ShadowTree &shadowTree)> callback) const;
|
|
|
|
private:
|
|
mutable folly::SharedMutex mutex_;
|
|
mutable std::unordered_map<SurfaceId, std::unique_ptr<ShadowTree>>
|
|
registry_; // Protected by `mutex_`.
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|