Re-introduce AsyncEventBeatV2 experiment

Summary: changelog: internal

Reviewed By: javache

Differential Revision: D31056006

fbshipit-source-id: 423c81a0fde77b0e2e4b3ddcd6e23c0552b16b3b
This commit is contained in:
Samuel Susla 2021-09-24 07:14:05 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 382e78e0f0
Коммит 6d367d70f2
3 изменённых файлов: 135 добавлений и 8 удалений

Просмотреть файл

@ -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"
@ -541,22 +542,39 @@ void Binding::installFabricUIManager(
}
}
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)
[eventBeatManager,
runtimeExecutor,
localJavaUIManager,
enableV2AsynchronousEventBeat](EventBeat::SharedOwnerBox const &ownerBox)
-> std::unique_ptr<EventBeat> {
return std::make_unique<AsyncEventBeat>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
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)
[eventBeatManager,
runtimeExecutor,
localJavaUIManager,
enableV2AsynchronousEventBeat](EventBeat::SharedOwnerBox const &ownerBox)
-> std::unique_ptr<EventBeat> {
return std::make_unique<AsyncEventBeat>(
ownerBox, eventBeatManager, runtimeExecutor, localJavaUIManager);
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);