Skip to content

Commit 44659d2

Browse files
use vector for subscribers in event system
Summary: Replaced global event subscriber with a vector of subscriber functions Further changes in commit stack support the mutiple subscribers in event system Reviewed By: davidaurelio Differential Revision: D15352451 fbshipit-source-id: 7ca6f0943735bf1f76a906c23e15e14ae3c5f42c
1 parent a9e8a71 commit 44659d2

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

ReactCommon/yoga/yoga/event/event.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,26 @@ namespace yoga {
1515

1616
namespace {
1717

18-
// For now, a single subscriber is enough.
19-
// This can be changed as soon as the need for more than one subscriber arises.
20-
std::function<Event::Subscriber>& globalEventSubscriber() {
21-
static std::function<Event::Subscriber> subscriber = nullptr;
22-
return subscriber;
18+
Event::Subscribers& eventSubscribers() {
19+
static Event::Subscribers subscribers = {};
20+
return subscribers;
2321
}
2422

2523
} // namespace
2624

2725
void Event::reset() {
28-
globalEventSubscriber() = nullptr;
26+
eventSubscribers() = {};
2927
}
3028

3129
void Event::subscribe(std::function<Subscriber>&& subscriber) {
32-
if (globalEventSubscriber() != nullptr) {
33-
throw std::logic_error(
34-
"Yoga currently supports only one global event subscriber");
35-
}
36-
globalEventSubscriber() = std::move(subscriber);
30+
eventSubscribers().push_back(subscriber);
3731
}
3832

3933
void Event::publish(const YGNode& node, Type eventType, const Data& eventData) {
40-
auto& subscriber = globalEventSubscriber();
41-
if (subscriber) {
42-
subscriber(node, eventType, eventData);
34+
for (auto& subscriber : eventSubscribers()) {
35+
if (subscriber) {
36+
subscriber(node, eventType, eventData);
37+
}
4338
}
4439
}
4540

ReactCommon/yoga/yoga/event/event.h

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

99
#include <functional>
10+
#include <vector>
1011

1112
struct YGConfig;
1213
struct YGNode;
@@ -24,6 +25,7 @@ struct Event {
2425
};
2526
class Data;
2627
using Subscriber = void(const YGNode&, Type, Data);
28+
using Subscribers = std::vector<std::function<Subscriber>>;
2729

2830
template <Type E>
2931
struct TypedData {};

0 commit comments

Comments
 (0)