Add option to use RuntimeScheduler in TurboModules

Summary: changelog: [internal]

Reviewed By: RSNara

Differential Revision: D31108093

fbshipit-source-id: 941abf334cc89391641131475725a3eeb790b822
This commit is contained in:
Samuel Susla 2021-09-25 15:24:39 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 5bc9d91b10
Коммит 18697adec4
5 изменённых файлов: 69 добавлений и 3 удалений

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

@ -1,4 +1,4 @@
load("//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "APPLE", "rn_xplat_cxx_library", "subdir_glob")
load("//tools/build_defs/oss:rn_defs.bzl", "ANDROID", "APPLE", "CXX", "rn_xplat_cxx_library", "subdir_glob")
rn_xplat_cxx_library(
name = "callinvoker",
@ -17,7 +17,7 @@ rn_xplat_cxx_library(
"-Wall",
],
labels = ["supermodule:xplat/default/public.react_native.infra"],
platforms = (ANDROID, APPLE),
platforms = (ANDROID, APPLE, CXX),
preferred_linkage = "static",
preprocessor_flags = [
"-DLOG_TAG=\"ReactNative\"",

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

@ -15,7 +15,7 @@ LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../../
LOCAL_SHARED_LIBRARIES := libruntimeexecutor libreact_render_core libreact_debug libjsi
LOCAL_SHARED_LIBRARIES := libruntimeexecutor libreact_render_core libreact_debug libjsi callinvoker
LOCAL_CFLAGS := \
-DLOG_TAG=\"Fabric\"
@ -25,3 +25,4 @@ LOCAL_CFLAGS += -fexceptions -frtti -std=c++17 -Wall
include $(BUILD_SHARED_LIBRARY)
$(call import-module,runtimeexecutor)
$(call import-module,callinvoker)

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

@ -53,6 +53,7 @@ rn_xplat_cxx_library(
react_native_xplat_target("runtimeexecutor:runtimeexecutor"),
react_native_xplat_target("react/renderer/debug:debug"),
react_native_xplat_target("better:better"),
react_native_xplat_target("callinvoker:callinvoker"),
],
)

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

@ -0,0 +1,30 @@
/*
* 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 "RuntimeSchedulerCallInvoker.h"
namespace facebook::react {
RuntimeSchedulerCallInvoker::RuntimeSchedulerCallInvoker(
std::weak_ptr<RuntimeScheduler> runtimeScheduler)
: runtimeScheduler_(runtimeScheduler) {}
void RuntimeSchedulerCallInvoker::invokeAsync(std::function<void()> &&func) {
if (auto runtimeScheduler = runtimeScheduler_.lock()) {
runtimeScheduler->scheduleWork(
[func = std::move(func)](jsi::Runtime &) { func(); });
}
}
void RuntimeSchedulerCallInvoker::invokeSync(std::function<void()> &&func) {
if (auto runtimeScheduler = runtimeScheduler_.lock()) {
runtimeScheduler->executeNowOnTheSameThread(
[func = std::move(func)](jsi::Runtime &) { func(); });
}
}
} // namespace facebook::react

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

@ -0,0 +1,34 @@
/*
* 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 <ReactCommon/CallInvoker.h>
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
namespace facebook::react {
/*
* Exposes RuntimeScheduler to native modules. All calls invonked on JavaScript
* queue from native modules will be funneled through RuntimeScheduler.
*/
class RuntimeSchedulerCallInvoker : public CallInvoker {
public:
RuntimeSchedulerCallInvoker(std::weak_ptr<RuntimeScheduler> runtimeScheduler);
void invokeAsync(std::function<void()> &&func) override;
void invokeSync(std::function<void()> &&func) override;
private:
/*
* RuntimeScheduler is retained by the runtime. It must not be
* retained by anything beyond the runtime.
*/
std::weak_ptr<RuntimeScheduler> runtimeScheduler_;
};
} // namespace facebook::react