move event files to yoga/events folder

Summary: Moved events files to yoga/events for better code structure

Reviewed By: davidaurelio

Differential Revision: D15198566

fbshipit-source-id: 74d451011841d59fae5a1c637f9c33a7d2d1f87e
This commit is contained in:
Sidharth Guglani
2019-05-09 07:42:34 -07:00
committed by Facebook Github Bot
parent 5824dbda66
commit 74fc37efc8
7 changed files with 9 additions and 9 deletions

47
yoga/event/event.cpp Normal file
View File

@@ -0,0 +1,47 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the LICENSE
* file in the root directory of this source tree.
*/
#include "event.h"
#include <memory>
#include <stdexcept>
#include <iostream>
namespace facebook {
namespace yoga {
namespace {
// For now, a single subscriber is enough.
// This can be changed as soon as the need for more than one subscriber arises.
std::function<Event::Subscriber>& globalEventSubscriber() {
static std::function<Event::Subscriber> subscriber = nullptr;
return subscriber;
}
} // namespace
void Event::reset() {
globalEventSubscriber() = nullptr;
}
void Event::subscribe(std::function<Subscriber>&& subscriber) {
if (globalEventSubscriber() != nullptr) {
throw std::logic_error(
"Yoga currently supports only one global event subscriber");
}
globalEventSubscriber() = std::move(subscriber);
}
void Event::publish(const YGNode& node, Type eventType, const Data& eventData) {
auto& subscriber = globalEventSubscriber();
if (subscriber) {
subscriber(node, eventType, eventData);
}
}
} // namespace yoga
} // namespace facebook