2018-05-22 22:48:19 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-22 22:48:19 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2018-09-14 05:56:00 +00:00
|
|
|
* Stores a pointer to `EventTarget` identifying a particular component and
|
|
|
|
* a weak pointer to `EventDispatcher` which is responsible for delivering the event.
|
|
|
|
*
|
|
|
|
* Note: Retaining an `EventTarget` does *not* guarantee that actual event target
|
|
|
|
* exists and/or valid in JavaScript realm. The `EventTarget` retains an `EventTargetWrapper`
|
|
|
|
* which wraps JavaScript object in `unsafe-unretained` manner. Retaining
|
|
|
|
* the `EventTarget` *does* indicate that we can use that to get an actual
|
|
|
|
* JavaScript object from that in the future *ensuring safety beforehand somehow*;
|
|
|
|
* JSI maintains `WeakObject` object as long as we retain the `EventTarget`.
|
|
|
|
* All `EventTarget` instances must be deallocated before stopping JavaScript machine.
|
2018-05-22 22:48:19 +00:00
|
|
|
*/
|
2018-09-14 05:56:06 +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-08-27 14:21:24 +00:00
|
|
|
static std::recursive_mutex &DispatchMutex();
|
2018-05-22 22:48:19 +00:00
|
|
|
|
2018-09-14 05:56:00 +00:00
|
|
|
EventEmitter(
|
|
|
|
SharedEventTarget eventTarget,
|
|
|
|
Tag tag,
|
|
|
|
WeakEventDispatcher eventDispatcher
|
|
|
|
);
|
|
|
|
|
2018-08-27 14:21:24 +00:00
|
|
|
virtual ~EventEmitter() = default;
|
2018-05-22 22:48:19 +00:00
|
|
|
|
2018-08-27 14:21:24 +00:00
|
|
|
/*
|
|
|
|
* Indicates that an event can be delivered to `eventTarget`.
|
|
|
|
* Callsite must acquire `DispatchMutex` to access those methods.
|
2018-09-14 05:56:02 +00:00
|
|
|
* The `setEnabled` operation is not guaranteed: the `EventEmitter` cannot
|
|
|
|
* be re-enabled after disabling; in this case, the method does nothing.
|
2018-08-27 14:21:24 +00:00
|
|
|
*/
|
|
|
|
void setEnabled(bool enabled) const;
|
|
|
|
bool getEnabled() const;
|
|
|
|
|
|
|
|
protected:
|
2018-05-22 22:48:19 +00:00
|
|
|
/*
|
|
|
|
* 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-09-14 05:56:00 +00:00
|
|
|
mutable SharedEventTarget eventTarget_;
|
2018-06-01 16:36:19 +00:00
|
|
|
Tag tag_;
|
2018-09-14 05:56:00 +00:00
|
|
|
WeakEventDispatcher eventDispatcher_;
|
2018-05-22 22:48:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|