Skip to content

Commit 2016460

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Pass event priority to React
Summary: Changelog: [internal] This is a mechanism that will guess event's React priority based on other events ongoing on the platform. If an event happens within span of ContinuousStart -> ContinuousEnd and its category is unspecified, we deduce it's React priority to be default. All other events are discrete. Special case: `onScroll`, which is always treated as "Default". Reviewed By: JoshuaGross Differential Revision: D28485060 fbshipit-source-id: d2eae63dbcf03271dfed97128a1590dd165a3ce2
1 parent 1866566 commit 2016460

File tree

12 files changed

+240
-25
lines changed

12 files changed

+240
-25
lines changed

ReactCommon/react/renderer/components/view/TouchEventEmitter.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,23 @@ static jsi::Value touchEventPayload(
6262
void TouchEventEmitter::dispatchTouchEvent(
6363
std::string const &type,
6464
TouchEvent const &event,
65-
EventPriority const &priority) const {
65+
EventPriority priority,
66+
RawEvent::Category category) const {
6667
dispatchEvent(
6768
type,
6869
[event](jsi::Runtime &runtime) {
6970
return touchEventPayload(runtime, event);
7071
},
71-
priority);
72+
priority,
73+
category);
7274
}
7375

7476
void TouchEventEmitter::onTouchStart(TouchEvent const &event) const {
75-
dispatchTouchEvent("touchStart", event, EventPriority::AsynchronousBatched);
77+
dispatchTouchEvent(
78+
"touchStart",
79+
event,
80+
EventPriority::AsynchronousBatched,
81+
RawEvent::Category::ContinuousStart);
7682
}
7783

7884
void TouchEventEmitter::onTouchMove(TouchEvent const &event) const {
@@ -82,11 +88,19 @@ void TouchEventEmitter::onTouchMove(TouchEvent const &event) const {
8288
}
8389

8490
void TouchEventEmitter::onTouchEnd(TouchEvent const &event) const {
85-
dispatchTouchEvent("touchEnd", event, EventPriority::AsynchronousBatched);
91+
dispatchTouchEvent(
92+
"touchEnd",
93+
event,
94+
EventPriority::AsynchronousBatched,
95+
RawEvent::Category::ContinuousEnd);
8696
}
8797

8898
void TouchEventEmitter::onTouchCancel(TouchEvent const &event) const {
89-
dispatchTouchEvent("touchCancel", event, EventPriority::AsynchronousBatched);
99+
dispatchTouchEvent(
100+
"touchCancel",
101+
event,
102+
EventPriority::AsynchronousBatched,
103+
RawEvent::Category::ContinuousEnd);
90104
}
91105

92106
} // namespace react

ReactCommon/react/renderer/components/view/TouchEventEmitter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class TouchEventEmitter : public EventEmitter {
3333
void dispatchTouchEvent(
3434
std::string const &type,
3535
TouchEvent const &event,
36-
EventPriority const &priority) const;
36+
EventPriority priority,
37+
RawEvent::Category category) const;
3738
};
3839

3940
} // namespace react

ReactCommon/react/renderer/core/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ fb_xplat_cxx_test(
8787
"//xplat/third-party/gmock:gtest",
8888
react_native_xplat_target("react/renderer/components/view:view"),
8989
":core",
90+
"//xplat/hermes/API:HermesAPI",
9091
],
9192
)
9293

ReactCommon/react/renderer/core/EventDispatcher.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
namespace facebook {
1818
namespace react {
1919

20-
class RawEvent;
20+
struct RawEvent;
2121

2222
/*
2323
* Represents event-delivery infrastructure.

ReactCommon/react/renderer/core/EventEmitter.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ void EventEmitter::dispatchEvent(
5353
const std::string &type,
5454
const folly::dynamic &payload,
5555
EventPriority priority) const {
56-
dispatchEvent(
57-
type,
58-
[payload](jsi::Runtime &runtime) {
59-
return valueFromDynamic(runtime, payload);
60-
},
61-
priority);
56+
dispatchEvent(type, [payload](jsi::Runtime &runtime) {
57+
return valueFromDynamic(runtime, payload);
58+
});
6259
}
6360

6461
void EventEmitter::dispatchUniqueEvent(
@@ -72,7 +69,8 @@ void EventEmitter::dispatchUniqueEvent(
7269
void EventEmitter::dispatchEvent(
7370
const std::string &type,
7471
const ValueFactory &payloadFactory,
75-
EventPriority priority) const {
72+
EventPriority priority,
73+
RawEvent::Category category) const {
7674
SystraceSection s("EventEmitter::dispatchEvent");
7775

7876
auto eventDispatcher = eventDispatcher_.lock();
@@ -81,7 +79,8 @@ void EventEmitter::dispatchEvent(
8179
}
8280

8381
eventDispatcher->dispatchEvent(
84-
RawEvent(normalizeEventType(type), payloadFactory, eventTarget_),
82+
RawEvent(
83+
normalizeEventType(type), payloadFactory, eventTarget_, category),
8584
priority);
8685
}
8786

@@ -95,8 +94,11 @@ void EventEmitter::dispatchUniqueEvent(
9594
return;
9695
}
9796

98-
eventDispatcher->dispatchUniqueEvent(
99-
RawEvent(normalizeEventType(type), payloadFactory, eventTarget_));
97+
eventDispatcher->dispatchUniqueEvent(RawEvent(
98+
normalizeEventType(type),
99+
payloadFactory,
100+
eventTarget_,
101+
RawEvent::Category::Continuous));
100102
}
101103

102104
void EventEmitter::setEnabled(bool enabled) const {

ReactCommon/react/renderer/core/EventEmitter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ class EventEmitter {
7171
const std::string &type,
7272
const ValueFactory &payloadFactory =
7373
EventEmitter::defaultPayloadFactory(),
74-
EventPriority priority = EventPriority::AsynchronousBatched) const;
74+
EventPriority priority = EventPriority::AsynchronousBatched,
75+
RawEvent::Category category = RawEvent::Category::Unspecified) const;
7576

7677
void dispatchEvent(
7778
const std::string &type,

ReactCommon/react/renderer/core/EventQueue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class EventQueue {
6969
mutable std::vector<RawEvent> eventQueue_;
7070
mutable std::vector<StateUpdate> stateUpdateQueue_;
7171
mutable std::mutex queueMutex_;
72+
mutable bool hasContinuousEventStarted_{false};
7273
};
7374

7475
} // namespace react

ReactCommon/react/renderer/core/EventQueueProcessor.cpp

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,33 @@ void EventQueueProcessor::flushEvents(
3131
}
3232
}
3333

34-
for (const auto &event : events) {
34+
for (auto const &event : events) {
35+
if (event.category == RawEvent::Category::ContinuousEnd) {
36+
hasContinuousEventStarted_ = false;
37+
}
38+
39+
auto reactPriority = hasContinuousEventStarted_
40+
? ReactEventPriority::Default
41+
: ReactEventPriority::Discrete;
42+
43+
if (event.category == RawEvent::Category::Continuous) {
44+
reactPriority = ReactEventPriority::Default;
45+
}
46+
47+
if (event.category == RawEvent::Category::Discrete) {
48+
reactPriority = ReactEventPriority::Discrete;
49+
}
50+
3551
eventPipe_(
3652
runtime,
3753
event.eventTarget.get(),
3854
event.type,
39-
ReactEventPriority::Default,
55+
reactPriority,
4056
event.payloadFactory);
57+
58+
if (event.category == RawEvent::Category::ContinuousStart) {
59+
hasContinuousEventStarted_ = true;
60+
}
4161
}
4262

4363
// No need to lock `EventEmitter::DispatchMutex()` here.

ReactCommon/react/renderer/core/EventQueueProcessor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class EventQueueProcessor {
2828
private:
2929
EventPipe const eventPipe_;
3030
StatePipe const statePipe_;
31+
32+
mutable bool hasContinuousEventStarted_{false};
3133
};
3234

3335
} // namespace react

ReactCommon/react/renderer/core/RawEvent.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ namespace react {
1313
RawEvent::RawEvent(
1414
std::string type,
1515
ValueFactory payloadFactory,
16-
SharedEventTarget eventTarget)
16+
SharedEventTarget eventTarget,
17+
Category category)
1718
: type(std::move(type)),
1819
payloadFactory(std::move(payloadFactory)),
19-
eventTarget(std::move(eventTarget)) {}
20+
eventTarget(std::move(eventTarget)),
21+
category(category) {}
2022

2123
} // namespace react
2224
} // namespace facebook

0 commit comments

Comments
 (0)