Valentin Shergin c25d5948a5 Fabric: Exposing EventEmitter's ownership model as a shared_ptr
Summary:
As we did in the previous diff, here we implemented `EventEmitter`'s ownership model as a `shared_ptr`. This change fixes problem with leaking `WeakObject`s which happens on hot-reload.

So, in short:
 * `EventTargetWrapper` object owns `jsi::WeakObject` that can be converted to actual `jsi::Object` that represent event target in JavaScript realm;
 * `EventTargetWrapper` and `jsi::WeakObject` objects must be deallocated as soon as native part does not need them anymore;
 * `EventEmitter` objects retain `EventTarget` objects;
 * `EventEmitter` can loose event target object in case if assosiated `ShadowNode` got unmounted (not deallocated); in this case `EventEmitter` is loosing possibility to dispatch event even if some mounting-layer code is still retaining it.

Reviewed By: mdvacca

Differential Revision: D9762755

fbshipit-source-id: 96e989767a32914db9f4627fce51b044c71f257a
2018-09-13 23:02:37 -07:00

50 lines
1.2 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 <fabric/events/EventBeat.h>
#include <fabric/events/EventQueue.h>
#include <fabric/events/primitives.h>
#include <fabric/events/RawEvent.h>
namespace facebook {
namespace react {
class EventDispatcher;
using SharedEventDispatcher = std::shared_ptr<const EventDispatcher>;
using WeakEventDispatcher = std::weak_ptr<const EventDispatcher>;
/*
* Represents event-delivery infrastructure.
* Particular `EventEmitter` clases use this for sending events.
*/
class EventDispatcher {
public:
EventDispatcher(
const EventPipe &eventPipe,
const EventBeatFactory &synchonousEventBeatFactory,
const EventBeatFactory &asynchonousEventBeatFactory
);
/*
* Dispatches a raw event with given priority using event-delivery pipe.
*/
void dispatchEvent(
const RawEvent &rawEvent,
EventPriority priority
) const;
private:
std::array<std::unique_ptr<EventQueue>, 4> eventQueues_;
};
} // namespace react
} // namespace facebook