Skip to content

Commit

Permalink
use shared_ptr for subscribers vector in event system
Browse files Browse the repository at this point in the history
Summary:
using shared_ptr for vector of subscribers
Further changes in commit stack support the mutiple subscribers in event system

Reviewed By: davidaurelio

Differential Revision: D15352512

fbshipit-source-id: fac7f4268abf9ca4277734aca2f21cd711eb7d6e
  • Loading branch information
SidharthGuglani-zz authored and facebook-github-bot committed May 15, 2019
1 parent 44659d2 commit 37c8771
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions ReactCommon/yoga/yoga/event/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "event.h"
#include <memory>
#include <stdexcept>
#include <mutex>

#include <iostream>

Expand All @@ -15,23 +16,29 @@ namespace yoga {

namespace {

Event::Subscribers& eventSubscribers() {
static Event::Subscribers subscribers = {};
std::mutex& eventSubscribersMutex() {
static std::mutex subscribersMutex;
return subscribersMutex;
}

std::shared_ptr<Event::Subscribers>& eventSubscribers() {
static auto subscribers = std::make_shared<Event::Subscribers>();
return subscribers;
}

} // namespace

void Event::reset() {
eventSubscribers() = {};
eventSubscribers() = std::make_shared<Event::Subscribers>();
}

void Event::subscribe(std::function<Subscriber>&& subscriber) {
eventSubscribers().push_back(subscriber);
std::lock_guard<std::mutex> guard(eventSubscribersMutex());
eventSubscribers()->push_back(subscriber);
}

void Event::publish(const YGNode& node, Type eventType, const Data& eventData) {
for (auto& subscriber : eventSubscribers()) {
for (auto& subscriber : *eventSubscribers()) {
if (subscriber) {
subscriber(node, eventType, eventData);
}
Expand Down

0 comments on commit 37c8771

Please sign in to comment.