Skip to content

Commit

Permalink
Use designated initialisers for ShadowViewMutation
Browse files Browse the repository at this point in the history
Summary:
changelog: [internal]

In this diff, we delete default initialised for ShadowViewMutation to prevent accidentally creating empty ShadowViewMutation.
The other initialiser is made private and all of its uses are migrated to designated initialisers. This makes for safer API.

Reviewed By: RSNara

Differential Revision: D30774900

fbshipit-source-id: d2064bf08409850e75e13ad06558b7980a7f5d8d
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Sep 12, 2021
1 parent d2cc91b commit cc3064d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <react/debug/flags.h>
#include <react/debug/react_native_assert.h>

#include <logger/react_native_log.h>
#include <react/renderer/animations/conversions.h>
#include <react/renderer/animations/utils.h>
#include <react/renderer/componentregistry/ComponentDescriptorFactory.h>
Expand Down Expand Up @@ -432,12 +433,11 @@ LayoutAnimationKeyFrameManager::pullTransaction(
// that is being referenced by the REMOVE mutation.
if (keyframe.type == AnimationConfigurationType::Update &&
mutation.oldChildShadowView.tag > 0) {
executeMutationImmediately = ShadowViewMutation{
/* .type = */ mutation.type,
/* .parentShadowView = */ mutation.parentShadowView,
/* .oldChildShadowView = */ keyframe.viewPrev,
/* .newChildShadowView = */ {},
/* .index = */ mutation.index};
executeMutationImmediately =
ShadowViewMutation::RemoveMutation(
mutation.parentShadowView,
keyframe.viewPrev,
mutation.index);
}
}
}
Expand Down Expand Up @@ -1274,18 +1274,31 @@ void LayoutAnimationKeyFrameManager::queueFinalMutationsForCompletedKeyFrame(
logPrefix + ": Queuing up Final Mutation:", finalMutation);
// Copy so that if something else mutates the inflight animations,
// it won't change this mutation after this point.
auto mutation = ShadowViewMutation{
/* .type = */ finalMutation.type,
/* .parentShadowView = */ finalMutation.parentShadowView,
/* .oldChildShadowView = */ prev,
/* .newChildShadowView = */ finalMutation.newChildShadowView,
/* .index = */ finalMutation.index};
react_native_assert(mutation.oldChildShadowView.tag > 0);
react_native_assert(
mutation.newChildShadowView.tag > 0 ||
finalMutation.type == ShadowViewMutation::Remove ||
finalMutation.type == ShadowViewMutation::Delete);
mutationsList.push_back(mutation);
switch (finalMutation.type) {
// For CREATE/INSERT this will contain CREATE, INSERT in that order.
// For REMOVE/DELETE, same.
case ShadowViewMutation::Type::Create:
mutationsList.push_back(ShadowViewMutation::CreateMutation(
finalMutation.newChildShadowView));
break;
case ShadowViewMutation::Type::Delete:
mutationsList.push_back(ShadowViewMutation::DeleteMutation(prev));
break;
case ShadowViewMutation::Type::Insert:
mutationsList.push_back(ShadowViewMutation::InsertMutation(
finalMutation.parentShadowView,
finalMutation.newChildShadowView,
finalMutation.index));
break;
case ShadowViewMutation::Type::Remove:
mutationsList.push_back(ShadowViewMutation::RemoveMutation(
finalMutation.parentShadowView, prev, finalMutation.index));
break;
case ShadowViewMutation::Type::Update:
mutationsList.push_back(ShadowViewMutation::UpdateMutation(
prev, finalMutation.newChildShadowView));
break;
}
if (finalMutation.newChildShadowView.tag > 0) {
prev = finalMutation.newChildShadowView;
}
Expand Down Expand Up @@ -1327,19 +1340,15 @@ void LayoutAnimationKeyFrameManager::queueFinalMutationsForCompletedKeyFrame(
generatedMutation);
mutationsList.push_back(generatedMutation);
} else {
auto mutation = ShadowViewMutation{
/* .type = */ ShadowViewMutation::Type::Update,
/* .parentShadowView = */ keyframe.parentView,
/* .oldChildShadowView = */ keyframe.viewPrev,
/* .newChildShadowView = */ keyframe.viewEnd,
/* .index = */ -1};
auto mutation = ShadowViewMutation::UpdateMutation(
keyframe.viewPrev, keyframe.viewEnd);
PrintMutationInstruction(
logPrefix +
"Animation Complete: Queuing up Final Synthetic Mutation:",
mutation);
react_native_assert(mutation.oldChildShadowView.tag > 0);
react_native_assert(mutation.newChildShadowView.tag > 0);
mutationsList.push_back(mutation);
mutationsList.push_back(std::move(mutation));
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions ReactCommon/react/renderer/mounting/ShadowViewMutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ bool ShadowViewMutation::mutatedViewIsVirtual() const {
return viewIsVirtual;
}

ShadowViewMutation::ShadowViewMutation(
Type type,
ShadowView parentShadowView,
ShadowView oldChildShadowView,
ShadowView newChildShadowView,
int index)
: type(type),
parentShadowView(parentShadowView),
oldChildShadowView(oldChildShadowView),
newChildShadowView(newChildShadowView),
index(index) {}

#if RN_DEBUG_STRING_CONVERTIBLE

std::string getDebugName(ShadowViewMutation const &mutation) {
Expand Down
11 changes: 10 additions & 1 deletion ReactCommon/react/renderer/mounting/ShadowViewMutation.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace react {
struct ShadowViewMutation final {
using List = std::vector<ShadowViewMutation>;

ShadowViewMutation() = delete;

#pragma mark - Designated Initializers

/*
Expand Down Expand Up @@ -70,13 +72,20 @@ struct ShadowViewMutation final {
ShadowView newChildShadowView = {};
int index = -1;

public:
// Some platforms can have the notion of virtual views - views that are in the
// ShadowTree hierarchy but never are on the platform. Generally this is used
// so notify the platform that a view exists so that we can keep EventEmitters
// around, to notify JS of something. This mechanism is DEPRECATED and it is
// highly recommended that you NOT make use of this in your platform!
bool mutatedViewIsVirtual() const;

private:
ShadowViewMutation(
Type type,
ShadowView parentShadowView,
ShadowView oldChildShadowView,
ShadowView newChildShadowView,
int index);
};

using ShadowViewMutationList = std::vector<ShadowViewMutation>;
Expand Down

0 comments on commit cc3064d

Please sign in to comment.