forked from facebook/react-native
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-introduce AsyncEventBeatV2 experiment
Summary: changelog: internal Reviewed By: javache Differential Revision: D31056006 fbshipit-source-id: 423c81a0fde77b0e2e4b3ddcd6e23c0552b16b3b
- Loading branch information
1 parent
382e78e
commit 6d367d7
Showing
3 changed files
with
135 additions
and
8 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
ReactAndroid/src/main/java/com/facebook/react/fabric/jni/AsyncEventBeatV2.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
40 changes: 40 additions & 0 deletions
40
ReactAndroid/src/main/java/com/facebook/react/fabric/jni/AsyncEventBeatV2.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters