Skip to content

Commit fa1e915

Browse files
committed
Some cleanup and improvements
1 parent 9d51420 commit fa1e915

30 files changed

+181
-323
lines changed

packages/react-native-reanimated/Common/cpp/reanimated/CSS/common/definitions.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ namespace reanimated::css {
99

1010
using namespace facebook;
1111

12-
using PropertyNames = std::vector<std::string>;
1312
using PropertyPath = std::vector<std::string>;
14-
/**
15-
* If nullopt - all style properties can trigger transition
16-
* If empty vector - no style property can trigger transition
17-
* Otherwise - only specified style properties can trigger transition
18-
*/
19-
using TransitionProperties = std::optional<PropertyNames>;
20-
2113
using EasingFunction = std::function<double(double)>;
2214
using ColorChannels = std::array<uint8_t, 4>;
2315

packages/react-native-reanimated/Common/cpp/reanimated/CSS/configs/CSSTransitionConfig.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include <reanimated/CSS/configs/common.h>
44

5-
#include <utility>
6-
75
namespace reanimated::css {
86

97
CSSTransitionPropertyUpdates parsePropertyUpdates(jsi::Runtime &rt, const jsi::Object &diffs) {
@@ -31,16 +29,13 @@ CSSTransitionPropertyUpdates parsePropertyUpdates(jsi::Runtime &rt, const jsi::O
3129

3230
result.emplace(
3331
propertyName,
34-
std::make_optional(std::make_pair(
35-
diffArray.getValueAtIndex(rt, 0), diffArray.getValueAtIndex(rt, 1))));
32+
std::make_optional(std::make_pair(diffArray.getValueAtIndex(rt, 0), diffArray.getValueAtIndex(rt, 1))));
3633
}
3734

3835
return result;
3936
}
4037

41-
PartialCSSTransitionPropertySettings parsePartialPropertySettings(
42-
jsi::Runtime &rt,
43-
const jsi::Object &settings) {
38+
PartialCSSTransitionPropertySettings parsePartialPropertySettings(jsi::Runtime &rt, const jsi::Object &settings) {
4439
PartialCSSTransitionPropertySettings result;
4540

4641
if (settings.hasProperty(rt, "duration")) {

packages/react-native-reanimated/Common/cpp/reanimated/CSS/configs/CSSTransitionConfig.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <optional>
88
#include <string>
99
#include <unordered_map>
10+
#include <utility>
1011

1112
namespace reanimated::css {
1213

@@ -19,8 +20,7 @@ struct CSSTransitionPropertySettings {
1920

2021
using CSSTransitionPropertiesSettings = std::unordered_map<std::string, CSSTransitionPropertySettings>;
2122

22-
using CSSTransitionPropertyUpdates =
23-
std::unordered_map<std::string, std::optional<std::pair<jsi::Value, jsi::Value>>>;
23+
using CSSTransitionPropertyUpdates = std::unordered_map<std::string, std::optional<std::pair<jsi::Value, jsi::Value>>>;
2424

2525
struct CSSTransitionConfig {
2626
CSSTransitionPropertyUpdates properties;
@@ -34,8 +34,7 @@ struct PartialCSSTransitionPropertySettings {
3434
std::optional<bool> allowDiscrete;
3535
};
3636

37-
using CSSTransitionPropertySettingsUpdates =
38-
std::unordered_map<std::string, PartialCSSTransitionPropertySettings>;
37+
using CSSTransitionPropertySettingsUpdates = std::unordered_map<std::string, PartialCSSTransitionPropertySettings>;
3938

4039
struct CSSTransitionUpdates {
4140
CSSTransitionPropertyUpdates properties;

packages/react-native-reanimated/Common/cpp/reanimated/CSS/core/CSSTransition.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <memory>
44
#include <string>
55
#include <unordered_map>
6+
#include <utility>
7+
#include <vector>
68

79
namespace reanimated::css {
810

@@ -14,8 +16,7 @@ CSSTransition::CSSTransition(
1416
viewStylesRepository_(viewStylesRepository),
1517
settings_(config.settings),
1618
styleInterpolator_(TransitionStyleInterpolator(shadowNode_->getComponentName(), viewStylesRepository)),
17-
progressProvider_(TransitionProgressProvider()) {
18-
}
19+
progressProvider_(TransitionProgressProvider()) {}
1920

2021
std::shared_ptr<const ShadowNode> CSSTransition::getShadowNode() const {
2122
return shadowNode_;
@@ -48,16 +49,19 @@ void CSSTransition::updateSettings(const CSSTransitionPropertySettingsUpdates &s
4849
}
4950
}
5051

51-
folly::dynamic CSSTransition::run(
52-
jsi::Runtime &rt,
53-
const CSSTransitionPropertyUpdates &propertyUpdates,
54-
const double timestamp) {
55-
if (!propertyUpdates.empty()) {
56-
styleInterpolator_.updateInterpolatedProperties(rt, propertyUpdates);
52+
folly::dynamic
53+
CSSTransition::run(jsi::Runtime &rt, const CSSTransitionPropertyUpdates &propertyUpdates, const double timestamp) {
54+
const auto reversedPropertyNames = styleInterpolator_.updateInterpolatedProperties(rt, propertyUpdates);
55+
56+
std::vector<std::string> propertyNames;
57+
propertyNames.reserve(propertyUpdates.size());
58+
for (const auto &[key, value] : propertyUpdates) {
59+
propertyNames.emplace_back(key);
5760
}
5861

59-
progressProvider_.runProgressProviders(timestamp, settings_, propertyUpdates);
60-
return update(rt, timestamp);
62+
progressProvider_.runProgressProviders(timestamp, settings_, propertyNames, reversedPropertyNames);
63+
64+
return update(timestamp);
6165
}
6266

6367
folly::dynamic CSSTransition::update(const double timestamp) {

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/PropertyInterpolator.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ class PropertyInterpolator {
2323
virtual folly::dynamic getResetStyle(const std::shared_ptr<const ShadowNode> &shadowNode) const = 0;
2424
virtual folly::dynamic getFirstKeyframeValue() const = 0;
2525
virtual folly::dynamic getLastKeyframeValue() const = 0;
26-
virtual bool equalsReversingAdjustedStartValue(const folly::dynamic &propertyValue) const = 0;
2726

2827
virtual void updateKeyframes(jsi::Runtime &rt, const jsi::Value &keyframes) = 0;
29-
virtual void updateKeyframesFromStyleChange(
28+
virtual bool updateKeyframesFromStyleChange(
3029
jsi::Runtime &rt,
3130
const jsi::Value &oldStyleValue,
3231
const jsi::Value &newStyleValue) = 0;

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/groups/ArrayPropertiesInterpolator.cpp

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@ ArrayPropertiesInterpolator::ArrayPropertiesInterpolator(
1111
const std::shared_ptr<ViewStylesRepository> &viewStylesRepository)
1212
: GroupPropertiesInterpolator(propertyPath, viewStylesRepository), factories_(factories) {}
1313

14-
bool ArrayPropertiesInterpolator::equalsReversingAdjustedStartValue(const folly::dynamic &propertyValue) const {
15-
if (!propertyValue.isArray()) {
16-
return false;
17-
}
18-
19-
const auto valuesCount = propertyValue.size();
20-
if (valuesCount != interpolators_.size()) {
21-
return false;
22-
}
23-
24-
for (size_t i = 0; i < valuesCount; ++i) {
25-
if (!interpolators_[i]->equalsReversingAdjustedStartValue(propertyValue[i])) {
26-
return false;
27-
}
28-
}
29-
30-
return true;
31-
}
32-
3314
void ArrayPropertiesInterpolator::updateKeyframes(jsi::Runtime &rt, const jsi::Value &keyframes) {
3415
const jsi::Array keyframesArray = keyframes.asObject(rt).asArray(rt);
3516
const size_t valuesCount = keyframesArray.size(rt);
@@ -41,37 +22,31 @@ void ArrayPropertiesInterpolator::updateKeyframes(jsi::Runtime &rt, const jsi::V
4122
}
4223
}
4324

44-
void ArrayPropertiesInterpolator::updateKeyframesFromStyleChange(
25+
bool ArrayPropertiesInterpolator::updateKeyframesFromStyleChange(
4526
jsi::Runtime &rt,
4627
const jsi::Value &oldStyleValue,
4728
const jsi::Value &newStyleValue) {
48-
const auto getArray = [&rt](const jsi::Value &value) -> std::optional<jsi::Array> {
49-
if (!value.isObject()) {
50-
return std::nullopt;
51-
}
52-
53-
const auto object = value.asObject(rt);
54-
if (!object.isArray(rt)) {
55-
return std::nullopt;
56-
}
57-
58-
return object.asArray(rt);
59-
};
29+
const auto oldStyleArray = oldStyleValue.isUndefined() ? jsi::Array(rt, 0) : oldStyleValue.asObject(rt).asArray(rt);
30+
const auto newStyleArray = newStyleValue.isUndefined() ? jsi::Array(rt, 0) : newStyleValue.asObject(rt).asArray(rt);
6031

61-
const auto oldArray = getArray(oldStyleValue);
62-
const auto newArray = getArray(newStyleValue);
63-
64-
const size_t oldSize = oldArray.has_value() ? oldArray->size(rt) : 0;
65-
const size_t newSize = newArray.has_value() ? newArray->size(rt) : 0;
32+
const size_t oldSize = oldStyleArray.size(rt);
33+
const size_t newSize = newStyleArray.size(rt);
6634
const size_t valuesCount = std::max(oldSize, newSize);
6735

6836
resizeInterpolators(valuesCount);
6937

38+
bool areAllPropsReversed = true;
39+
7040
for (size_t i = 0; i < valuesCount; ++i) {
71-
const auto oldValue = (oldArray.has_value() && i < oldSize) ? oldArray->getValueAtIndex(rt, i) : jsi::Value::undefined();
72-
const auto newValue = (newArray.has_value() && i < newSize) ? newArray->getValueAtIndex(rt, i) : jsi::Value::undefined();
73-
interpolators_[i]->updateKeyframesFromStyleChange(rt, oldValue, newValue);
41+
// These index checks ensure that interpolation works between 2 arrays
42+
// with different lengths
43+
areAllPropsReversed &= interpolators_[i]->updateKeyframesFromStyleChange(
44+
rt,
45+
i < oldSize ? oldStyleArray.getValueAtIndex(rt, i) : jsi::Value::undefined(),
46+
i < newSize ? newStyleArray.getValueAtIndex(rt, i) : jsi::Value::undefined());
7447
}
48+
49+
return areAllPropsReversed;
7550
}
7651

7752
folly::dynamic ArrayPropertiesInterpolator::mapInterpolators(

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/groups/ArrayPropertiesInterpolator.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ class ArrayPropertiesInterpolator : public GroupPropertiesInterpolator {
1515
const std::shared_ptr<ViewStylesRepository> &viewStylesRepository);
1616
virtual ~ArrayPropertiesInterpolator() = default;
1717

18-
bool equalsReversingAdjustedStartValue(const folly::dynamic &propertyValue) const override;
19-
2018
void updateKeyframes(jsi::Runtime &rt, const jsi::Value &keyframes) override;
21-
void updateKeyframesFromStyleChange(
19+
bool updateKeyframesFromStyleChange(
2220
jsi::Runtime &rt,
2321
const jsi::Value &oldStyleValue,
2422
const jsi::Value &newStyleValue) override;

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/groups/RecordPropertiesInterpolator.cpp

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ RecordPropertiesInterpolator::RecordPropertiesInterpolator(
1212
const std::shared_ptr<ViewStylesRepository> &viewStylesRepository)
1313
: GroupPropertiesInterpolator(propertyPath, viewStylesRepository), factories_(factories) {}
1414

15-
bool RecordPropertiesInterpolator::equalsReversingAdjustedStartValue(const folly::dynamic &propertyValue) const {
16-
return std::ranges::all_of(propertyValue.items(), [this](const auto &item) {
17-
const auto &[propName, propValue] = item;
18-
const auto it = interpolators_.find(propName.getString());
19-
return it != interpolators_.end() && it->second->equalsReversingAdjustedStartValue(propValue);
20-
});
21-
}
22-
2315
void RecordPropertiesInterpolator::updateKeyframes(jsi::Runtime &rt, const jsi::Value &keyframes) {
2416
// TODO - maybe add a possibility to remove interpolators that are no longer
2517
// used (for now, for simplicity, we only add new ones)
@@ -37,50 +29,42 @@ void RecordPropertiesInterpolator::updateKeyframes(jsi::Runtime &rt, const jsi::
3729
}
3830
}
3931

40-
void RecordPropertiesInterpolator::updateKeyframesFromStyleChange(
32+
bool RecordPropertiesInterpolator::updateKeyframesFromStyleChange(
4133
jsi::Runtime &rt,
4234
const jsi::Value &oldStyleValue,
4335
const jsi::Value &newStyleValue) {
44-
const auto getObject = [&rt](const jsi::Value &value) -> std::optional<jsi::Object> {
45-
if (!value.isObject()) {
46-
return std::nullopt;
47-
}
48-
49-
const auto object = value.asObject(rt);
50-
if (!object.isObject()) {
51-
return std::nullopt;
52-
}
53-
54-
return object;
55-
};
36+
// TODO - maybe add a possibility to remove interpolators that are no longer
37+
// used (for now, for simplicity, we only add new ones)
38+
const auto oldStyleObject = oldStyleValue.isUndefined() ? jsi::Object(rt) : oldStyleValue.asObject(rt);
39+
const auto newStyleObject = newStyleValue.isUndefined() ? jsi::Object(rt) : newStyleValue.asObject(rt);
5640

57-
const auto oldObject = getObject(oldStyleValue);
58-
const auto newObject = getObject(newStyleValue);
41+
const auto oldPropertyNames = oldStyleObject.getPropertyNames(rt);
42+
const auto newPropertyNames = newStyleObject.getPropertyNames(rt);
43+
const auto oldSize = oldPropertyNames.size(rt);
44+
const auto newSize = newPropertyNames.size(rt);
5945

6046
std::unordered_set<std::string> propertyNamesSet;
61-
if (oldObject.has_value()) {
62-
jsi::Array names = oldObject->getPropertyNames(rt);
63-
const size_t count = names.size(rt);
64-
for (size_t i = 0; i < count; ++i) {
65-
propertyNamesSet.insert(names.getValueAtIndex(rt, i).asString(rt).utf8(rt));
66-
}
47+
for (size_t i = 0; i < oldSize; ++i) {
48+
propertyNamesSet.insert(oldPropertyNames.getValueAtIndex(rt, i).asString(rt).utf8(rt));
6749
}
68-
if (newObject.has_value()) {
69-
jsi::Array names = newObject->getPropertyNames(rt);
70-
const size_t count = names.size(rt);
71-
for (size_t i = 0; i < count; ++i) {
72-
propertyNamesSet.insert(names.getValueAtIndex(rt, i).asString(rt).utf8(rt));
73-
}
50+
for (size_t i = 0; i < newSize; ++i) {
51+
propertyNamesSet.insert(newPropertyNames.getValueAtIndex(rt, i).asString(rt).utf8(rt));
7452
}
7553

54+
bool areAllPropsReversed = true;
55+
7656
for (const auto &propertyName : propertyNamesSet) {
7757
maybeCreateInterpolator(propertyName);
78-
79-
const auto propId = jsi::PropNameID::forUtf8(rt, propertyName);
80-
const auto oldValue = oldObject.has_value() ? oldObject->getProperty(rt, propId) : jsi::Value::undefined();
81-
const auto newValue = newObject.has_value() ? newObject->getProperty(rt, propId) : jsi::Value::undefined();
82-
interpolators_[propertyName]->updateKeyframesFromStyleChange(rt, oldValue, newValue);
58+
const auto propNameID = jsi::PropNameID::forUtf8(rt, propertyName);
59+
areAllPropsReversed &= interpolators_[propertyName]->updateKeyframesFromStyleChange(
60+
rt,
61+
oldStyleObject.hasProperty(rt, propNameID) ? oldStyleObject.getProperty(rt, propNameID)
62+
: jsi::Value::undefined(),
63+
newStyleObject.hasProperty(rt, propNameID) ? newStyleObject.getProperty(rt, propNameID)
64+
: jsi::Value::undefined());
8365
}
66+
67+
return areAllPropsReversed;
8468
}
8569

8670
folly::dynamic RecordPropertiesInterpolator::mapInterpolators(

packages/react-native-reanimated/Common/cpp/reanimated/CSS/interpolation/groups/RecordPropertiesInterpolator.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ class RecordPropertiesInterpolator : public GroupPropertiesInterpolator {
1616
const std::shared_ptr<ViewStylesRepository> &viewStylesRepository);
1717
virtual ~RecordPropertiesInterpolator() = default;
1818

19-
bool equalsReversingAdjustedStartValue(const folly::dynamic &propertyValue) const override;
20-
2119
void updateKeyframes(jsi::Runtime &rt, const jsi::Value &keyframes) override;
22-
void updateKeyframesFromStyleChange(
20+
bool updateKeyframesFromStyleChange(
2321
jsi::Runtime &rt,
2422
const jsi::Value &oldStyleValue,
2523
const jsi::Value &newStyleValue) override;

0 commit comments

Comments
 (0)