Prevent redundant dispatches onto RuntimeExecutor queue in AsynchronousEventBeat::induce

Summary:
Changelog: [internal]

Prevent redundant calls to RuntimeExecutor by making sure no two calls to the executor are scheduled at the same time.

Reviewed By: mdvacca

Differential Revision: D27989412

fbshipit-source-id: 8f9b1591f7c9c2265fd4b05bf3dc5505ffc2568b
This commit is contained in:
Samuel Susla
2021-04-25 16:15:25 -07:00
committed by Facebook GitHub Bot
parent f598dd0ee3
commit 9cd98fa3ee
3 changed files with 139 additions and 10 deletions
@@ -0,0 +1,69 @@
/*
* 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 <jsi/jsi.h>
#include <react/renderer/core/EventBeat.h>
#include <react/renderer/uimanager/primitives.h>
#include "AsyncEventBeatV2.h"
namespace facebook::react {
AsyncEventBeatV2::AsyncEventBeatV2(
EventBeat::SharedOwnerBox const &ownerBox,
EventBeatManager *eventBeatManager,
RuntimeExecutor runtimeExecutor,
jni::global_ref<jobject> javaUIManager)
: EventBeat(ownerBox),
eventBeatManager_(eventBeatManager),
runtimeExecutor_(runtimeExecutor),
javaUIManager_(javaUIManager) {
eventBeatManager->addObserver(*this);
}
AsyncEventBeatV2::~AsyncEventBeatV2() {
eventBeatManager_->removeObserver(*this);
}
void AsyncEventBeatV2::tick() const {
if (!isRequested_ || isBeatCallbackScheduled_) {
return;
}
isRequested_ = false;
isBeatCallbackScheduled_ = true;
runtimeExecutor_([this, ownerBox = ownerBox_](jsi::Runtime &runtime) {
isBeatCallbackScheduled_ = false;
auto owner = ownerBox->owner.lock();
if (!owner) {
return;
}
if (beatCallback_) {
beatCallback_(runtime);
}
});
}
void AsyncEventBeatV2::induce() const {
tick();
}
void AsyncEventBeatV2::request() const {
bool alreadyRequested = isRequested_;
EventBeat::request();
if (!alreadyRequested) {
// Notifies java side that an event will be dispatched (e.g. LayoutEvent)
static auto onRequestEventBeat =
jni::findClassStatic("com/facebook/react/fabric/FabricUIManager")
->getMethod<void()>("onRequestEventBeat");
onRequestEventBeat(javaUIManager_);
}
}
} // namespace facebook::react
@@ -0,0 +1,40 @@
/*
* 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 <react/renderer/core/EventBeat.h>
#include "EventBeatManager.h"
namespace facebook::react {
class AsyncEventBeatV2 final : public EventBeat,
public EventBeatManagerObserver {
public:
AsyncEventBeatV2(
EventBeat::SharedOwnerBox const &ownerBox,
EventBeatManager *eventBeatManager,
RuntimeExecutor runtimeExecutor,
jni::global_ref<jobject> javaUIManager);
~AsyncEventBeatV2() override;
void tick() const override;
void induce() const override;
void request() const override;
private:
EventBeatManager *eventBeatManager_;
RuntimeExecutor runtimeExecutor_;
jni::global_ref<jobject> javaUIManager_;
mutable std::atomic<bool> isBeatCallbackScheduled_{false};
};
} // namespace facebook::react
@@ -7,6 +7,7 @@
#include "Binding.h"
#include "AsyncEventBeat.h"
#include "AsyncEventBeatV2.h"
#include "EventEmitterWrapper.h"
#include "ReactNativeConfigHolder.h"
#include "StateWrapperImpl.h"
@@ -488,21 +489,40 @@ void Binding::installFabricUIManager(
std::make_shared<JMessageQueueThread>(jsMessageQueueThread);
auto runtimeExecutor = runtimeExecutorHolder->cthis()->get();
auto enableV2AsynchronousEventBeat =
config->getBool("react_fabric:enable_asynchronous_event_beat_v2_android");
// TODO: T31905686 Create synchronous Event Beat
jni::global_ref<jobject> localJavaUIManager = javaUIManager_;
EventBeat::Factory synchronousBeatFactory =
[eventBeatManager, runtimeExecutor, localJavaUIManager](
EventBeat::SharedOwnerBox const &ownerBox) {
return std::make_unique<AsyncEventBeat>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
};
[eventBeatManager,
runtimeExecutor,
localJavaUIManager,
enableV2AsynchronousEventBeat](EventBeat::SharedOwnerBox const &ownerBox)
-> std::unique_ptr<EventBeat> {
if (enableV2AsynchronousEventBeat) {
return std::make_unique<AsyncEventBeatV2>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
} else {
return std::make_unique<AsyncEventBeat>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
}
};
EventBeat::Factory asynchronousBeatFactory =
[eventBeatManager, runtimeExecutor, localJavaUIManager](
EventBeat::SharedOwnerBox const &ownerBox) {
return std::make_unique<AsyncEventBeat>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
};
[eventBeatManager,
runtimeExecutor,
localJavaUIManager,
enableV2AsynchronousEventBeat](EventBeat::SharedOwnerBox const &ownerBox)
-> std::unique_ptr<EventBeat> {
if (enableV2AsynchronousEventBeat) {
return std::make_unique<AsyncEventBeatV2>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
} else {
return std::make_unique<AsyncEventBeat>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
}
};
contextContainer->insert("ReactNativeConfig", config);
contextContainer->insert("FabricUIManager", javaUIManager_);