Skip to content

Commit 2854061

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Introduce EventQueueProcessor
Summary: Changelog: [internal] Pulling out event queue processing to its separate class. We will be adding more logic there so it makes sense to separate it for simplicity and testability purposes. Alternative names that came to mind: `EventsProcessor`, `EventsDispatcher`. Reviewed By: JoshuaGross Differential Revision: D28572283 fbshipit-source-id: 1cb2459f616b9995f66da80d50c401e68002da7f
1 parent 5a9b6fc commit 2854061

File tree

10 files changed

+114
-62
lines changed

10 files changed

+114
-62
lines changed

ReactCommon/react/renderer/core/BatchedEventQueue.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ namespace facebook {
1111
namespace react {
1212

1313
BatchedEventQueue::BatchedEventQueue(
14-
EventPipe eventPipe,
15-
StatePipe statePipe,
14+
EventQueueProcessor eventProcessor,
1615
std::unique_ptr<EventBeat> eventBeat)
17-
: EventQueue(eventPipe, statePipe, std::move(eventBeat)) {}
16+
: EventQueue(std::move(eventProcessor), std::move(eventBeat)) {}
1817

1918
void BatchedEventQueue::onEnqueue() const {
2019
eventBeat_->request();

ReactCommon/react/renderer/core/BatchedEventQueue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#pragma once
99

1010
#include <react/renderer/core/EventQueue.h>
11+
#include <react/renderer/core/EventQueueProcessor.h>
1112

1213
namespace facebook {
1314
namespace react {
@@ -19,8 +20,7 @@ namespace react {
1920
class BatchedEventQueue final : public EventQueue {
2021
public:
2122
BatchedEventQueue(
22-
EventPipe eventPipe,
23-
StatePipe statePipe,
23+
EventQueueProcessor eventProcessor,
2424
std::unique_ptr<EventBeat> eventBeat);
2525

2626
void onEnqueue() const override;

ReactCommon/react/renderer/core/EventDispatcher.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@ namespace facebook {
1717
namespace react {
1818

1919
EventDispatcher::EventDispatcher(
20-
EventPipe const &eventPipe,
21-
StatePipe const &statePipe,
20+
EventQueueProcessor eventProcessor,
2221
EventBeat::Factory const &synchonousEventBeatFactory,
2322
EventBeat::Factory const &asynchonousEventBeatFactory,
2423
EventBeat::SharedOwnerBox const &ownerBox,
2524
bool unbatchedQueuesOnly)
2625
: synchronousUnbatchedQueue_(std::make_unique<UnbatchedEventQueue>(
27-
eventPipe,
28-
statePipe,
26+
eventProcessor,
2927
synchonousEventBeatFactory(ownerBox))),
3028
synchronousBatchedQueue_(std::make_unique<BatchedEventQueue>(
31-
eventPipe,
32-
statePipe,
29+
eventProcessor,
3330
synchonousEventBeatFactory(ownerBox))),
3431
asynchronousUnbatchedQueue_(std::make_unique<UnbatchedEventQueue>(
35-
eventPipe,
36-
statePipe,
32+
eventProcessor,
3733
asynchonousEventBeatFactory(ownerBox))),
3834
asynchronousBatchedQueue_(std::make_unique<BatchedEventQueue>(
39-
eventPipe,
40-
statePipe,
35+
eventProcessor,
4136
asynchonousEventBeatFactory(ownerBox))),
4237
unbatchedQueuesOnly_(unbatchedQueuesOnly) {}
4338

ReactCommon/react/renderer/core/EventDispatcher.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@
77

88
#pragma once
99

10-
#include <array>
11-
#include <memory>
12-
1310
#include <react/renderer/core/BatchedEventQueue.h>
1411
#include <react/renderer/core/EventBeat.h>
15-
#include <react/renderer/core/EventPipe.h>
1612
#include <react/renderer/core/EventPriority.h>
17-
#include <react/renderer/core/StatePipe.h>
13+
#include <react/renderer/core/EventQueueProcessor.h>
1814
#include <react/renderer/core/StateUpdate.h>
1915
#include <react/renderer/core/UnbatchedEventQueue.h>
2016

@@ -33,8 +29,7 @@ class EventDispatcher {
3329
using Weak = std::weak_ptr<EventDispatcher const>;
3430

3531
EventDispatcher(
36-
EventPipe const &eventPipe,
37-
StatePipe const &statePipe,
32+
EventQueueProcessor eventProcessor,
3833
EventBeat::Factory const &synchonousEventBeatFactory,
3934
EventBeat::Factory const &asynchonousEventBeatFactory,
4035
EventBeat::SharedOwnerBox const &ownerBox,

ReactCommon/react/renderer/core/EventQueue.cpp

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ namespace facebook {
1414
namespace react {
1515

1616
EventQueue::EventQueue(
17-
EventPipe eventPipe,
18-
StatePipe statePipe,
17+
EventQueueProcessor eventProcessor,
1918
std::unique_ptr<EventBeat> eventBeat)
20-
: eventPipe_(std::move(eventPipe)),
21-
statePipe_(std::move(statePipe)),
19+
: eventProcessor_(std::move(eventProcessor)),
2220
eventBeat_(std::move(eventBeat)) {
2321
eventBeat_->setBeatCallback(
2422
std::bind(&EventQueue::onBeat, this, std::placeholders::_1));
@@ -97,30 +95,7 @@ void EventQueue::flushEvents(jsi::Runtime &runtime) const {
9795
eventQueue_.clear();
9896
}
9997

100-
{
101-
std::lock_guard<std::mutex> lock(EventEmitter::DispatchMutex());
102-
103-
for (const auto &event : queue) {
104-
if (event.eventTarget) {
105-
event.eventTarget->retain(runtime);
106-
}
107-
}
108-
}
109-
110-
for (const auto &event : queue) {
111-
eventPipe_(
112-
runtime, event.eventTarget.get(), event.type, event.payloadFactory);
113-
}
114-
115-
// No need to lock `EventEmitter::DispatchMutex()` here.
116-
// The mutex protects from a situation when the `instanceHandle` can be
117-
// deallocated during accessing, but that's impossible at this point because
118-
// we have a strong pointer to it.
119-
for (const auto &event : queue) {
120-
if (event.eventTarget) {
121-
event.eventTarget->release(runtime);
122-
}
123-
}
98+
eventProcessor_.flushEvents(runtime, std::move(queue));
12499
}
125100

126101
void EventQueue::flushStateUpdates() const {
@@ -137,9 +112,7 @@ void EventQueue::flushStateUpdates() const {
137112
stateUpdateQueue_.clear();
138113
}
139114

140-
for (const auto &stateUpdate : stateUpdateQueue) {
141-
statePipe_(stateUpdate);
142-
}
115+
eventProcessor_.flushStateUpdates(std::move(stateUpdateQueue));
143116
}
144117

145118
} // namespace react

ReactCommon/react/renderer/core/EventQueue.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313

1414
#include <jsi/jsi.h>
1515
#include <react/renderer/core/EventBeat.h>
16-
#include <react/renderer/core/EventPipe.h>
16+
#include <react/renderer/core/EventQueueProcessor.h>
1717
#include <react/renderer/core/RawEvent.h>
18-
#include <react/renderer/core/StatePipe.h>
1918
#include <react/renderer/core/StateUpdate.h>
2019

2120
namespace facebook {
@@ -28,8 +27,7 @@ namespace react {
2827
class EventQueue {
2928
public:
3029
EventQueue(
31-
EventPipe eventPipe,
32-
StatePipe statePipe,
30+
EventQueueProcessor eventProcessor,
3331
std::unique_ptr<EventBeat> eventBeat);
3432
virtual ~EventQueue() = default;
3533

@@ -64,8 +62,8 @@ class EventQueue {
6462
void flushEvents(jsi::Runtime &runtime) const;
6563
void flushStateUpdates() const;
6664

67-
const EventPipe eventPipe_;
68-
const StatePipe statePipe_;
65+
EventQueueProcessor eventProcessor_;
66+
6967
const std::unique_ptr<EventBeat> eventBeat_;
7068
// Thread-safe, protected by `queueMutex_`.
7169
mutable std::vector<RawEvent> eventQueue_;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "EventQueue.h"
9+
10+
#include "EventEmitter.h"
11+
#include "ShadowNodeFamily.h"
12+
13+
namespace facebook {
14+
namespace react {
15+
16+
EventQueueProcessor::EventQueueProcessor(
17+
EventPipe eventPipe,
18+
StatePipe statePipe)
19+
: eventPipe_(std::move(eventPipe)), statePipe_(std::move(statePipe)) {}
20+
21+
void EventQueueProcessor::flushEvents(
22+
jsi::Runtime &runtime,
23+
std::vector<RawEvent> &&events) const {
24+
{
25+
std::lock_guard<std::mutex> lock(EventEmitter::DispatchMutex());
26+
27+
for (const auto &event : events) {
28+
if (event.eventTarget) {
29+
event.eventTarget->retain(runtime);
30+
}
31+
}
32+
}
33+
34+
for (const auto &event : events) {
35+
eventPipe_(
36+
runtime, event.eventTarget.get(), event.type, event.payloadFactory);
37+
}
38+
39+
// No need to lock `EventEmitter::DispatchMutex()` here.
40+
// The mutex protects from a situation when the `instanceHandle` can be
41+
// deallocated during accessing, but that's impossible at this point because
42+
// we have a strong pointer to it.
43+
for (const auto &event : events) {
44+
if (event.eventTarget) {
45+
event.eventTarget->release(runtime);
46+
}
47+
}
48+
}
49+
50+
void EventQueueProcessor::flushStateUpdates(
51+
std::vector<StateUpdate> &&states) const {
52+
for (const auto &stateUpdate : states) {
53+
statePipe_(stateUpdate);
54+
}
55+
}
56+
57+
} // namespace react
58+
} // namespace facebook
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#pragma once
9+
10+
#include <vector>
11+
12+
#include <jsi/jsi.h>
13+
#include <react/renderer/core/EventPipe.h>
14+
#include <react/renderer/core/RawEvent.h>
15+
#include <react/renderer/core/StatePipe.h>
16+
#include <react/renderer/core/StateUpdate.h>
17+
18+
namespace facebook {
19+
namespace react {
20+
21+
class EventQueueProcessor {
22+
public:
23+
EventQueueProcessor(EventPipe eventPipe, StatePipe statePipe);
24+
25+
void flushEvents(jsi::Runtime &runtime, std::vector<RawEvent> &&events) const;
26+
void flushStateUpdates(std::vector<StateUpdate> &&states) const;
27+
28+
private:
29+
EventPipe const eventPipe_;
30+
StatePipe const statePipe_;
31+
};
32+
33+
} // namespace react
34+
} // namespace facebook

ReactCommon/react/renderer/scheduler/Scheduler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <react/debug/react_native_assert.h>
1414
#include <react/renderer/componentregistry/ComponentDescriptorRegistry.h>
1515
#include <react/renderer/core/Constants.h>
16+
#include <react/renderer/core/EventQueueProcessor.h>
1617
#include <react/renderer/core/LayoutContext.h>
1718
#include <react/renderer/debug/SystraceSection.h>
1819
#include <react/renderer/mounting/MountingOverrideDelegate.h>
@@ -77,8 +78,7 @@ Scheduler::Scheduler(
7778
// Creating an `EventDispatcher` instance inside the already allocated
7879
// container (inside the optional).
7980
eventDispatcher_->emplace(
80-
eventPipe,
81-
statePipe,
81+
EventQueueProcessor(eventPipe, statePipe),
8282
schedulerToolbox.synchronousEventBeatFactory,
8383
schedulerToolbox.asynchronousEventBeatFactory,
8484
eventOwnerBox,

packages/rn-tester/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -862,7 +862,7 @@ SPEC CHECKSUMS:
862862
RCTTypeSafety: f5405e0143bb2addae97ce33e4c87d9284a48fe2
863863
React: f64c9f6db5428717922a3292ba6a448615a2e143
864864
React-callinvoker: c5d61e29df57793f0dc10ec2bc01c846f863e51f
865-
React-Core: 6ac4e2647cebffffe6e24137682fb2a325dc43be
865+
React-Core: f5b65410a2ecdbb4560f23dc4525588390b0b3b1
866866
React-CoreModules: 818e75a3eb8f431be8fc3d8c8eb15206b3f52e81
867867
React-cxxreact: fa0884110d142f7e1600a86ae769d5fd43ec8a69
868868
React-Fabric: ee21f85af985320ca427353e95821a5b4d6f4f46

0 commit comments

Comments
 (0)