2018-05-22 22:48:19 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
|
|
|
* 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 <memory>
|
2018-06-01 16:36:25 +00:00
|
|
|
#include <mutex>
|
2018-05-22 22:48:19 +00:00
|
|
|
|
|
|
|
#include <folly/dynamic.h>
|
2018-07-18 05:41:39 +00:00
|
|
|
#include <fabric/events/EventDispatcher.h>
|
|
|
|
#include <fabric/events/primitives.h>
|
2018-05-22 22:48:19 +00:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2018-06-09 20:02:55 +00:00
|
|
|
class EventEmitter;
|
2018-05-22 22:48:19 +00:00
|
|
|
|
2018-06-09 20:02:55 +00:00
|
|
|
using SharedEventEmitter = std::shared_ptr<const EventEmitter>;
|
2018-05-22 22:48:19 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Base class for all particular typed event handlers.
|
|
|
|
* Stores `InstanceHandle` identifying a particular component and the pointer
|
|
|
|
* to `EventDispatcher` which is responsible for delivering the event.
|
|
|
|
*
|
|
|
|
* TODO: Reconsider naming of all event-related things.
|
|
|
|
*/
|
2018-06-09 20:02:55 +00:00
|
|
|
class EventEmitter {
|
2018-05-22 22:48:19 +00:00
|
|
|
|
2018-07-18 05:41:39 +00:00
|
|
|
/*
|
|
|
|
* We have to repeat `Tag` type definition here because `events` module does
|
|
|
|
* not depend on `core` module (and should not).
|
|
|
|
*/
|
|
|
|
using Tag = int32_t;
|
|
|
|
|
2018-05-22 22:48:19 +00:00
|
|
|
public:
|
2018-06-29 22:30:30 +00:00
|
|
|
EventEmitter(const EventTarget &eventTarget, const Tag &tag, const SharedEventDispatcher &eventDispatcher);
|
2018-06-09 20:02:55 +00:00
|
|
|
virtual ~EventEmitter();
|
2018-05-22 22:48:19 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initates an event delivery process.
|
|
|
|
* Is used by particular subclasses only.
|
|
|
|
*/
|
|
|
|
void dispatchEvent(
|
2018-06-01 16:36:19 +00:00
|
|
|
const std::string &type,
|
|
|
|
const folly::dynamic &payload = folly::dynamic::object(),
|
2018-05-22 22:48:19 +00:00
|
|
|
const EventPriority &priority = EventPriority::AsynchronousBatched
|
|
|
|
) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2018-06-29 22:30:30 +00:00
|
|
|
mutable EventTarget eventTarget_ {nullptr};
|
2018-06-01 16:36:19 +00:00
|
|
|
Tag tag_;
|
2018-05-22 22:48:19 +00:00
|
|
|
std::weak_ptr<const EventDispatcher> eventDispatcher_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|