Valentin Shergin 7d630b92dc Fabric: Lock-free events 5/n: New implementation of toggleEventEmitters (which does not rely on mutations)
Summary: This diff implements a new algorithm that effectively marks `EventEmitter`s enabled or disabled. The previous implementation relied on a list of mutation instructions whereas the new one analyzes the shadow trees. The mutations-based approach didn't work well because mutations describe `ShadowView`s whereas some `ShadowNode`s are simply not views (like VirtualText), but we have to enable/disable them anyway.

Reviewed By: sahrens

Differential Revision: D13642594

fbshipit-source-id: 12169e11d5685e50bcd0d8c410498c594df744b4
2019-01-16 20:22:39 -08:00

84 lines
2.5 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 <memory>
#include <shared_mutex>
#include <react/components/root/RootShadowNode.h>
#include <react/core/LayoutConstraints.h>
#include <react/core/ReactPrimitives.h>
#include <react/core/ShadowNode.h>
#include <react/mounting/ShadowViewMutation.h>
#include <react/uimanager/ShadowTreeDelegate.h>
namespace facebook {
namespace react {
/*
* Represents the shadow tree and its lifecycle.
*/
class ShadowTree final {
public:
/*
* Creates a new shadow tree instance.
*/
ShadowTree(
SurfaceId surfaceId,
const LayoutConstraints &layoutConstraints,
const LayoutContext &layoutContext);
~ShadowTree();
/*
* Returns the `SurfaceId` associated with the shadow tree.
*/
SurfaceId getSurfaceId() const;
/*
* Performs commit calling `transaction` function with a `oldRootShadowNode`
* and expecting a `newRootShadowNode` as a return value.
* The `transaction` function can abort commit returning `nullptr`.
* If a `revision` pointer is not null, the method will store there a
* contiguous revision number of the successfully performed transaction.
* Specify `attempts` to allow performing multiple tries.
* Returns `true` if the operation finished successfully.
*/
bool commit(
std::function<UnsharedRootShadowNode(
const SharedRootShadowNode &oldRootShadowNode)> transaction,
int attempts = 1,
int *revision = nullptr) const;
#pragma mark - Delegate
/*
* Sets and gets the delegate.
* The delegate is stored as a raw pointer, so the owner must null
* the pointer before being destroyed.
*/
void setDelegate(ShadowTreeDelegate const *delegate);
ShadowTreeDelegate const *getDelegate() const;
private:
UnsharedRootShadowNode cloneRootShadowNode(
const SharedRootShadowNode &oldRootShadowNode,
const LayoutConstraints &layoutConstraints,
const LayoutContext &layoutContext) const;
void emitLayoutEvents(const ShadowViewMutationList &mutations) const;
const SurfaceId surfaceId_;
mutable folly::SharedMutex commitMutex_;
mutable SharedRootShadowNode rootShadowNode_; // Protected by `commitMutex_`.
mutable int revision_{1}; // Protected by `commitMutex_`.
ShadowTreeDelegate const *delegate_;
};
} // namespace react
} // namespace facebook