mirror of
https://github.com/status-im/react-native.git
synced 2025-02-24 07:08:27 +00:00
Summary: The first implementation of EventEmitter's enable/disable feature didn't not provide a way to enable an object after it was disabled. Apparently, we need this functionality due that fact that all nodes of the same family share same event emitter. Reviewed By: mdvacca Differential Revision: D10395849 fbshipit-source-id: 0eba54f0bb7ded35d64afb6559e6e27208c2b577
89 lines
2.6 KiB
C++
89 lines
2.6 KiB
C++
/**
|
|
* 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 <memory>
|
|
#include <mutex>
|
|
|
|
#include <fabric/events/EventDispatcher.h>
|
|
#include <fabric/events/primitives.h>
|
|
#include <folly/dynamic.h>
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
class EventEmitter;
|
|
|
|
using SharedEventEmitter = std::shared_ptr<const EventEmitter>;
|
|
|
|
/*
|
|
* Base class for all particular typed event handlers.
|
|
* 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.
|
|
*/
|
|
class EventEmitter {
|
|
/*
|
|
* We have to repeat `Tag` type definition here because `events` module does
|
|
* not depend on `core` module (and should not).
|
|
*/
|
|
using Tag = int32_t;
|
|
|
|
public:
|
|
static std::recursive_mutex &DispatchMutex();
|
|
|
|
EventEmitter(
|
|
SharedEventTarget eventTarget,
|
|
Tag tag,
|
|
WeakEventDispatcher eventDispatcher);
|
|
|
|
virtual ~EventEmitter() = default;
|
|
|
|
/*
|
|
* Indicates that an event can be delivered to `eventTarget`.
|
|
* Callsite must acquire `DispatchMutex` to access those methods.
|
|
* The `setEnabled` operation is not guaranteed: sometimes `EventEmitter`
|
|
* can be re-enabled after disabling, sometimes not.
|
|
*/
|
|
void setEnabled(bool enabled) const;
|
|
bool getEnabled() const;
|
|
|
|
protected:
|
|
#ifdef ANDROID
|
|
// We need this temporarily due to lack of Java-counterparts for particular
|
|
// subclasses.
|
|
public:
|
|
#endif
|
|
|
|
/*
|
|
* Initates an event delivery process.
|
|
* Is used by particular subclasses only.
|
|
*/
|
|
void dispatchEvent(
|
|
const std::string &type,
|
|
const folly::dynamic &payload = folly::dynamic::object(),
|
|
const EventPriority &priority = EventPriority::AsynchronousBatched) const;
|
|
|
|
private:
|
|
mutable SharedEventTarget eventTarget_;
|
|
mutable WeakEventTarget weakEventTarget_;
|
|
Tag tag_;
|
|
WeakEventDispatcher eventDispatcher_;
|
|
};
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|