Fabric: Introducing ShadowTree::CommitMode

Summary:
CommitMode allows customizing the side-effects of commit operations. In `Suspended` mode, the results of commit operations will not be passed down to a MountingCoordinator which will result in skipping the mounting phase completely.
This is one of the core elements of the Pre-rendering infrastructure.

Changelog: [Internal] Fabric-specific internal change.

Differential Revision: D24290773

fbshipit-source-id: c10ec20d13f3131fc632352ef22f4465c9dfb3c2
This commit is contained in:
Valentin Shergin
2021-01-15 16:23:14 -08:00
committed by Facebook GitHub Bot
parent daefa526ad
commit 16a1fc7b7a
2 changed files with 62 additions and 3 deletions
@@ -22,6 +22,7 @@ namespace facebook {
namespace react {
using CommitStatus = ShadowTree::CommitStatus;
using CommitMode = ShadowTree::CommitMode;
/*
* Generates (possibly) a new tree where all nodes with non-obsolete `State`
@@ -257,6 +258,29 @@ Tag ShadowTree::getSurfaceId() const {
return surfaceId_;
}
void ShadowTree::setCommitMode(CommitMode commitMode) const {
auto revision = ShadowTreeRevision{};
{
std::unique_lock<better::shared_mutex> lock(commitMutex_);
if (commitMode_ == commitMode) {
return;
}
commitMode_ = commitMode;
revision = currentRevision_;
}
if (commitMode == CommitMode::Normal) {
mount(revision);
}
}
CommitMode ShadowTree::getCommitMode() const {
std::shared_lock<better::shared_mutex> lock(commitMutex_);
return commitMode_;
}
MountingCoordinator::Shared ShadowTree::getMountingCoordinator() const {
return mountingCoordinator_;
}
@@ -290,12 +314,14 @@ CommitStatus ShadowTree::tryCommit(
auto telemetry = TransactionTelemetry{};
telemetry.willCommit();
CommitMode commitMode;
auto oldRevision = ShadowTreeRevision{};
auto newRevision = ShadowTreeRevision{};
{
// Reading `currentRevision_` in shared manner.
std::shared_lock<better::shared_mutex> lock(commitMutex_);
commitMode = commitMode_;
oldRevision = currentRevision_;
}
@@ -369,9 +395,9 @@ CommitStatus ShadowTree::tryCommit(
emitLayoutEvents(affectedLayoutableNodes);
mountingCoordinator_->push(newRevision);
notifyDelegatesOfUpdates();
if (commitMode == CommitMode::Normal) {
mount(newRevision);
}
return CommitStatus::Succeeded;
}
@@ -381,6 +407,11 @@ ShadowTreeRevision ShadowTree::getCurrentRevision() const {
return currentRevision_;
}
void ShadowTree::mount(ShadowTreeRevision const &revision) const {
mountingCoordinator_->push(revision);
delegate_.shadowTreeDidFinishTransaction(*this, mountingCoordinator_);
}
void ShadowTree::commitEmptyTree() const {
commit(
[](RootShadowNode const &oldRootShadowNode) -> RootShadowNode::Unshared {
@@ -31,12 +31,28 @@ using ShadowTreeCommitTransaction = std::function<RootShadowNode::Unshared(
*/
class ShadowTree final {
public:
/*
* Represents a result of a `commit` operation.
*/
enum class CommitStatus {
Succeeded,
Failed,
Cancelled,
};
/*
* Represents commits' side-effects propagation mode.
*/
enum class CommitMode {
// Commits' side-effects are observable via `MountingCoordinator`.
// The rendering pipeline fully works end-to-end.
Normal,
// Commits' side-effects are *not* observable via `MountingCoordinator`.
// The mounting phase is skipped in the rendering pipeline.
Suspended,
};
struct CommitOptions {
bool enableStateReconciliation{false};
// Lambda called inside `tryCommit`. If false is returned, commit is
@@ -62,6 +78,14 @@ class ShadowTree final {
*/
SurfaceId getSurfaceId() const;
/*
* Sets and gets the commit mode.
* Changing commit mode from `Suspended` to `Normal` will flush all suspended
* changes to `MountingCoordinator`.
*/
void setCommitMode(CommitMode commitMode) const;
CommitMode getCommitMode() const;
/*
* Performs commit calling `transaction` function with a `oldRootShadowNode`
* and expecting a `newRootShadowNode` as a return value.
@@ -99,12 +123,16 @@ class ShadowTree final {
MountingCoordinator::Shared getMountingCoordinator() const;
private:
void mount(ShadowTreeRevision const &revision) const;
void emitLayoutEvents(
std::vector<LayoutableShadowNode const *> &affectedLayoutableNodes) const;
SurfaceId const surfaceId_;
ShadowTreeDelegate const &delegate_;
mutable better::shared_mutex commitMutex_;
mutable CommitMode commitMode_{
CommitMode::Normal}; // Protected by `commitMutex_`.
mutable ShadowTreeRevision currentRevision_; // Protected by `commitMutex_`.
MountingCoordinator::Shared mountingCoordinator_;
};