Skip to content

Commit

Permalink
Re-introduce AsyncEventBeatV2 experiment
Browse files Browse the repository at this point in the history
Summary: changelog: internal

Reviewed By: javache

Differential Revision: D31056006

fbshipit-source-id: 423c81a0fde77b0e2e4b3ddcd6e23c0552b16b3b
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Sep 24, 2021
1 parent 382e78e commit 6d367d7
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 8 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
Expand Up @@ -7,6 +7,7 @@

#include "Binding.h"
#include "AsyncEventBeat.h"
#include "AsyncEventBeatV2.h"
#include "EventEmitterWrapper.h"
#include "ReactNativeConfigHolder.h"
#include "StateWrapperImpl.h"
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6d367d7

Please sign in to comment.