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:
committed by
Facebook Github Bot
parent
5824dbda66
commit
74fc37efc8
47
yoga/event/event.cpp
Normal file
47
yoga/event/event.cpp
Normal 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
|
67
yoga/event/event.h
Normal file
67
yoga/event/event.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
|
||||
struct YGConfig;
|
||||
struct YGNode;
|
||||
|
||||
namespace facebook {
|
||||
namespace yoga {
|
||||
|
||||
struct Event {
|
||||
enum Type { NodeAllocation, NodeDeallocation, NodeLayout };
|
||||
class Data;
|
||||
using Subscriber = void(const YGNode&, Type, Data);
|
||||
|
||||
template <Type E>
|
||||
struct TypedData {};
|
||||
|
||||
class Data {
|
||||
const void* data_;
|
||||
|
||||
public:
|
||||
template <Type E>
|
||||
Data(const TypedData<E>& data) : data_{&data} {}
|
||||
|
||||
template <Type E>
|
||||
const TypedData<E>& get() const {
|
||||
return *static_cast<const TypedData<E>*>(data_);
|
||||
};
|
||||
};
|
||||
|
||||
static void reset();
|
||||
|
||||
static void subscribe(std::function<Subscriber>&& subscriber);
|
||||
|
||||
template <Type E>
|
||||
static void publish(const YGNode& node, const TypedData<E>& eventData = {}) {
|
||||
publish(node, E, Data{eventData});
|
||||
}
|
||||
|
||||
template <Type E>
|
||||
static void publish(const YGNode* node, const TypedData<E>& eventData = {}) {
|
||||
publish<E>(*node, eventData);
|
||||
}
|
||||
|
||||
private:
|
||||
static void publish(const YGNode&, Type, const Data&);
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Event::TypedData<Event::NodeAllocation> {
|
||||
YGConfig* config;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Event::TypedData<Event::NodeDeallocation> {
|
||||
YGConfig* config;
|
||||
};
|
||||
|
||||
} // namespace yoga
|
||||
} // namespace facebook
|
Reference in New Issue
Block a user