mirror of
https://github.com/status-im/react-native.git
synced 2025-02-24 07:08:27 +00:00
Summary: Why do we need a dedicated registry class? * We need to simplify registry-related logic in Scheduler. * We need to couple threading aspect of the registry with the registry itself, otherwise it's not clear why exactly we acquire the mutex. We also should not acquire the mutex in a per-method way (as we did before), because it's incorrect and misleading (only lines that access the registry should by protected). * We need to have a way to share the registry with other classes (e.g. UIManager) without passing a reference to the whole Scheduler. Reviewed By: mdvacca Differential Revision: D13036550 fbshipit-source-id: 644da910e823666c586834a3da2b4cdcb90eebb2
56 lines
1.6 KiB
C++
56 lines
1.6 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 <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 get(
|
|
SurfaceId surfaceId,
|
|
std::function<void(const ShadowTree &shadowTree)> callback) const;
|
|
|
|
private:
|
|
mutable std::mutex mutex_;
|
|
mutable std::unordered_map<SurfaceId, std::unique_ptr<ShadowTree>>
|
|
registry_; // Protected by `mutex_`.
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|