Skip to content

Commit be92588

Browse files
rozelefacebook-github-bot
authored andcommitted
Add EventPayload::extractValue for NativeAnimated (facebook#49988)
Summary: Pull Request resolved: facebook#49988 NativeAnimated currently depends on folly::dynamic event payloads for event-driven animations. While some events (e.g., ScrollEvent.h) have an `asDynamic` implementation, not all events do. In practice, NativeAnimated just needs to be able to extract an numeric value from a path to drive an animation. Rather than converting events to dynamic, or otherwise special casing event handling, this change allows arbitrary payloads to implement `EventPayload::extractValue` to retrieve JS property path values directly from events, without intermediate conversions to dynamic. ## Changelog [Internal] Reviewed By: javache Differential Revision: D71046682 fbshipit-source-id: 3544335ff9d50da87ced015de587b97204173b57
1 parent c99e3a8 commit be92588

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ folly::dynamic ScrollEvent::asDynamic() const {
7272
return metrics;
7373
};
7474

75+
std::optional<double> ScrollEvent::extractValue(
76+
const std::vector<std::string>& path) const {
77+
if (path.size() == 1 && path[0] == "zoomScale") {
78+
return zoomScale;
79+
} else if (path.size() == 2 && path[0] == "contentOffset") {
80+
if (path[1] == "x") {
81+
return contentOffset.x;
82+
} else if (path[1] == "y") {
83+
return contentOffset.y;
84+
}
85+
}
86+
87+
return EventPayload::extractValue(path);
88+
}
89+
7590
EventPayloadType ScrollEvent::getType() const {
7691
return EventPayloadType::ScrollEvent;
7792
}

packages/react-native/ReactCommon/react/renderer/components/scrollview/ScrollEvent.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ struct ScrollEvent : public EventPayload {
3636
*/
3737
jsi::Value asJSIValue(jsi::Runtime& runtime) const override;
3838
EventPayloadType getType() const override;
39+
40+
std::optional<double> extractValue(
41+
const std::vector<std::string>& path) const override;
3942
};
4043

4144
struct ScrollEndDragEvent : public ScrollEvent {

packages/react-native/ReactCommon/react/renderer/core/EventPayload.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <jsi/jsi.h>
1111

1212
#include <react/renderer/core/EventPayloadType.h>
13+
#include <optional>
14+
#include <vector>
1315

1416
namespace facebook::react {
1517

@@ -33,6 +35,17 @@ struct EventPayload {
3335
* in `EventPayloadType` and return it from its overriden `getType()` method.
3436
*/
3537
virtual EventPayloadType getType() const = 0;
38+
39+
/**
40+
* Used to extract numeric values from the event payload based on
41+
* property path names as they will exist in JavaScript. This can
42+
* be used in conjunction with listeners on EventEmitters to do
43+
* things like drive native animations.
44+
*/
45+
virtual std::optional<double> extractValue(
46+
const std::vector<std::string>& /* path */) const {
47+
return std::nullopt;
48+
}
3649
};
3750

3851
using SharedEventPayload = std::shared_ptr<const EventPayload>;

0 commit comments

Comments
 (0)