Files
react-native/ReactCommon/fabric/core/events/EventDispatcher.cpp
T
Valentin SherginandFacebook Github Bot c80192c2ab Fabric: EventBeat::Owner to help with crashes
Summary:
The purpose of `EventBeat` is handling an asynchronous callback to itself which is being delivered on some different thread. That brings a challenge of ensuring that the `EventBeat` object stays valid during the timeframe of callback execution. The concept of Owner helps with that.
The owner is a shared pointer that retains (probably indirectly) the `EventBeat` object. To ensure the correctness of the call, `EventBeat` retains the owner (practically creating a retain cycle) during executing the callback. In case if the pointer to the owner already null, `EventBeat` skips executing the callback.
It's impossible to retain itself directly or refer to the shared pointer to itself from a constructor. `OwnerBox` is designed to work around this issue; it allows to store the pointer later, right after the creation of some other object that owns an `EventBeat`.

Reviewed By: JoshuaGross

Differential Revision: D17128549

fbshipit-source-id: 7ed34fd865430975157fd362f51c4a3d64214430
2019-08-30 18:24:27 -07:00

70 lines
2.1 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.
*/
#include "EventDispatcher.h"
#include <react/core/StateUpdate.h>
#include "BatchedEventQueue.h"
#include "RawEvent.h"
#include "UnbatchedEventQueue.h"
#define REACT_FABRIC_SYNC_EVENT_DISPATCHING_DISABLED
namespace facebook {
namespace react {
EventDispatcher::EventDispatcher(
EventPipe const &eventPipe,
StatePipe const &statePipe,
EventBeat::Factory const &synchonousEventBeatFactory,
EventBeat::Factory const &asynchonousEventBeatFactory,
EventBeat::SharedOwnerBox const &ownerBox) {
// Synchronous/Unbatched
eventQueues_[(int)EventPriority::SynchronousUnbatched] =
std::make_unique<UnbatchedEventQueue>(
eventPipe, statePipe, synchonousEventBeatFactory(ownerBox));
// Synchronous/Batched
eventQueues_[(int)EventPriority::SynchronousBatched] =
std::make_unique<BatchedEventQueue>(
eventPipe, statePipe, synchonousEventBeatFactory(ownerBox));
// Asynchronous/Unbatched
eventQueues_[(int)EventPriority::AsynchronousUnbatched] =
std::make_unique<UnbatchedEventQueue>(
eventPipe, statePipe, asynchonousEventBeatFactory(ownerBox));
// Asynchronous/Batched
eventQueues_[(int)EventPriority::AsynchronousBatched] =
std::make_unique<BatchedEventQueue>(
eventPipe, statePipe, asynchonousEventBeatFactory(ownerBox));
}
void EventDispatcher::dispatchEvent(
RawEvent const &rawEvent,
EventPriority priority) const {
getEventQueue(priority).enqueueEvent(std::move(rawEvent));
}
void EventDispatcher::dispatchStateUpdate(
StateUpdate &&stateUpdate,
EventPriority priority) const {
getEventQueue(priority).enqueueStateUpdate(std::move(stateUpdate));
}
const EventQueue &EventDispatcher::getEventQueue(EventPriority priority) const {
#ifdef REACT_FABRIC_SYNC_EVENT_DISPATCHING_DISABLED
priority = EventPriority::AsynchronousBatched;
#endif
return *eventQueues_[(int)priority];
}
} // namespace react
} // namespace facebook