mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-06-15 12:47:46 +08:00
Summary: I read my code and one thing stroke me: What if the same event emitter dispatches several events during the same event beat? In the previous implementation, only the first one will be delivered because the first `release` call consumes stored strong reference. So, I changed the API a bit to make this more explicit: we have `retain` & `release` methods and we have a getter that works (multiple times) only if the object was successfully retained. Reviewed By: sahrens Differential Revision: D13668147 fbshipit-source-id: c00af5f15bc7e30aa704d46bd23584b918b6f75a
67 lines
1.5 KiB
C++
67 lines
1.5 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 "EventQueue.h"
|
|
|
|
#include "EventEmitter.h"
|
|
|
|
namespace facebook {
|
|
namespace react {
|
|
|
|
EventQueue::EventQueue(
|
|
EventPipe eventPipe,
|
|
std::unique_ptr<EventBeat> eventBeat)
|
|
: eventPipe_(std::move(eventPipe)), eventBeat_(std::move(eventBeat)) {
|
|
eventBeat_->setBeatCallback(
|
|
std::bind(&EventQueue::onBeat, this, std::placeholders::_1));
|
|
}
|
|
|
|
void EventQueue::enqueueEvent(const RawEvent &rawEvent) const {
|
|
std::lock_guard<std::mutex> lock(queueMutex_);
|
|
queue_.push_back(rawEvent);
|
|
}
|
|
|
|
void EventQueue::onBeat(jsi::Runtime &runtime) const {
|
|
std::vector<RawEvent> queue;
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(queueMutex_);
|
|
|
|
if (queue_.size() == 0) {
|
|
return;
|
|
}
|
|
|
|
queue = std::move(queue_);
|
|
queue_.clear();
|
|
}
|
|
|
|
{
|
|
std::lock_guard<std::mutex> lock(EventEmitter::DispatchMutex());
|
|
|
|
for (const auto &event : queue) {
|
|
if (event.eventTarget) {
|
|
event.eventTarget->retain(runtime);
|
|
}
|
|
}
|
|
}
|
|
|
|
for (const auto &event : queue) {
|
|
eventPipe_(
|
|
runtime, event.eventTarget.get(), event.type, event.payloadFactory);
|
|
}
|
|
|
|
// No need to lock `EventEmitter::DispatchMutex()` here.
|
|
for (const auto &event : queue) {
|
|
if (event.eventTarget) {
|
|
event.eventTarget->release(runtime);
|
|
}
|
|
}
|
|
}
|
|
|
|
} // namespace react
|
|
} // namespace facebook
|