Refactor: RuntimeScheduler: Make error handlers configurable (#45616)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45616

We want to eventually route all js error handling through JsErrorHandler in bridgeless.

This will help with that.

Changelog: [Internal]

Reviewed By: rubennorte

Differential Revision: D60138415

fbshipit-source-id: de62edfe75066ba135225e24543628306aa5f4a0
This commit is contained in:
Ramanpreet Nara
2024-07-24 08:57:31 -07:00
committed by Facebook GitHub Bot
parent dc8c8ebc5b
commit 842c382773
6 changed files with 60 additions and 18 deletions
@@ -19,23 +19,35 @@ namespace facebook::react {
namespace {
std::unique_ptr<RuntimeSchedulerBase> getRuntimeSchedulerImplementation(
RuntimeExecutor runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now) {
std::function<RuntimeSchedulerTimePoint()> now,
RuntimeSchedulerErrorHandler onTaskError,
RuntimeSchedulerErrorHandler onMicrotaskError) {
if (ReactNativeFeatureFlags::useModernRuntimeScheduler()) {
return std::make_unique<RuntimeScheduler_Modern>(
std::move(runtimeExecutor), std::move(now));
std::move(runtimeExecutor),
std::move(now),
std::move(onTaskError),
std::move(onMicrotaskError));
} else {
return std::make_unique<RuntimeScheduler_Legacy>(
std::move(runtimeExecutor), std::move(now));
std::move(runtimeExecutor),
std::move(now),
std::move(onTaskError),
std::move(onMicrotaskError));
}
}
} // namespace
RuntimeScheduler::RuntimeScheduler(
RuntimeExecutor runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now)
std::function<RuntimeSchedulerTimePoint()> now,
RuntimeSchedulerErrorHandler onTaskError,
RuntimeSchedulerErrorHandler onMicrotaskError)
: runtimeSchedulerImpl_(getRuntimeSchedulerImplementation(
std::move(runtimeExecutor),
std::move(now))) {}
std::move(now),
std::move(onTaskError),
std::move(onMicrotaskError))) {}
void RuntimeScheduler::scheduleWork(RawCallback&& callback) noexcept {
return runtimeSchedulerImpl_->scheduleWork(std::move(callback));
@@ -8,6 +8,7 @@
#pragma once
#include <ReactCommon/RuntimeExecutor.h>
#include <cxxreact/ErrorUtils.h>
#include <react/performance/timeline/PerformanceEntryReporter.h>
#include <react/renderer/consistency/ShadowTreeRevisionConsistencyManager.h>
#include <react/renderer/runtimescheduler/RuntimeSchedulerClock.h>
@@ -19,6 +20,9 @@ namespace facebook::react {
using RuntimeSchedulerRenderingUpdate = std::function<void()>;
using RuntimeSchedulerTimeout = std::chrono::milliseconds;
using RuntimeSchedulerErrorHandler =
std::function<void(jsi::Runtime& runtime, jsi::JSError& error)>;
// This is a temporary abstract class for RuntimeScheduler forks to implement
// (and use them interchangeably).
class RuntimeSchedulerBase {
@@ -60,7 +64,15 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
explicit RuntimeScheduler(
RuntimeExecutor runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now =
RuntimeSchedulerClock::now);
RuntimeSchedulerClock::now,
RuntimeSchedulerErrorHandler onTaskError =
[](jsi::Runtime& runtime, jsi::JSError& error) {
handleJSError(runtime, error, true);
},
RuntimeSchedulerErrorHandler onMicrotaskError =
[](jsi::Runtime& runtime, jsi::JSError& error) {
handleJSError(runtime, error, true);
});
/*
* Not copyable.
@@ -8,7 +8,6 @@
#include "RuntimeScheduler_Legacy.h"
#include "SchedulerPriorityUtils.h"
#include <cxxreact/ErrorUtils.h>
#include <cxxreact/SystraceSection.h>
#include <react/renderer/consistency/ScopedShadowTreeRevisionLock.h>
#include <utility>
@@ -19,8 +18,13 @@ namespace facebook::react {
RuntimeScheduler_Legacy::RuntimeScheduler_Legacy(
RuntimeExecutor runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now)
: runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {}
std::function<RuntimeSchedulerTimePoint()> now,
RuntimeSchedulerErrorHandler onTaskError,
RuntimeSchedulerErrorHandler onMicrotaskError)
: runtimeExecutor_(std::move(runtimeExecutor)),
now_(std::move(now)),
onTaskError_(std::move(onTaskError)),
onMicrotaskError_(std::move(onMicrotaskError)) {}
void RuntimeScheduler_Legacy::scheduleWork(RawCallback&& callback) noexcept {
SystraceSection s("RuntimeScheduler::scheduleWork");
@@ -167,7 +171,7 @@ void RuntimeScheduler_Legacy::callExpiredTasks(jsi::Runtime& runtime) {
executeTask(runtime, topPriorityTask, didUserCallbackTimeout);
}
} catch (jsi::JSError& error) {
handleJSError(runtime, error, true);
onTaskError_(runtime, error);
}
currentPriority_ = previousPriority;
@@ -224,7 +228,7 @@ void RuntimeScheduler_Legacy::startWorkLoop(jsi::Runtime& runtime) {
executeTask(runtime, topPriorityTask, didUserCallbackTimeout);
}
} catch (jsi::JSError& error) {
handleJSError(runtime, error, true);
onTaskError_(runtime, error);
}
currentPriority_ = previousPriority;
@@ -22,7 +22,9 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
public:
explicit RuntimeScheduler_Legacy(
RuntimeExecutor runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now);
std::function<RuntimeSchedulerTimePoint()> now,
RuntimeSchedulerErrorHandler onTaskError,
RuntimeSchedulerErrorHandler onMicrotaskError);
/*
* Not copyable.
@@ -179,6 +181,9 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
ShadowTreeRevisionConsistencyManager* shadowTreeRevisionConsistencyManager_{
nullptr};
RuntimeSchedulerErrorHandler onTaskError_;
RuntimeSchedulerErrorHandler onMicrotaskError_;
};
} // namespace facebook::react
@@ -8,7 +8,6 @@
#include "RuntimeScheduler_Modern.h"
#include "SchedulerPriorityUtils.h"
#include <cxxreact/ErrorUtils.h>
#include <cxxreact/SystraceSection.h>
#include <react/featureflags/ReactNativeFeatureFlags.h>
#include <react/renderer/consistency/ScopedShadowTreeRevisionLock.h>
@@ -33,8 +32,13 @@ std::chrono::milliseconds getResolvedTimeoutForIdleTask(
RuntimeScheduler_Modern::RuntimeScheduler_Modern(
RuntimeExecutor runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now)
: runtimeExecutor_(std::move(runtimeExecutor)), now_(std::move(now)) {}
std::function<RuntimeSchedulerTimePoint()> now,
RuntimeSchedulerErrorHandler onTaskError,
RuntimeSchedulerErrorHandler onMicrotaskError)
: runtimeExecutor_(std::move(runtimeExecutor)),
now_(std::move(now)),
onTaskError_(std::move(onTaskError)),
onMicrotaskError_(std::move(onMicrotaskError)) {}
void RuntimeScheduler_Modern::scheduleWork(RawCallback&& callback) noexcept {
SystraceSection s("RuntimeScheduler::scheduleWork");
@@ -384,7 +388,7 @@ void RuntimeScheduler_Modern::executeTask(
task.callback = result.getObject(runtime).getFunction(runtime);
}
} catch (jsi::JSError& error) {
handleJSError(runtime, error, true);
onTaskError_(runtime, error);
}
}
@@ -418,7 +422,7 @@ void RuntimeScheduler_Modern::performMicrotaskCheckpoint(
break;
}
} catch (jsi::JSError& error) {
handleJSError(runtime, error, true);
onMicrotaskError_(runtime, error);
}
retries++;
}
@@ -23,7 +23,9 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
public:
explicit RuntimeScheduler_Modern(
RuntimeExecutor runtimeExecutor,
std::function<RuntimeSchedulerTimePoint()> now);
std::function<RuntimeSchedulerTimePoint()> now,
RuntimeSchedulerErrorHandler onTaskError,
RuntimeSchedulerErrorHandler onMicrotaskError);
/*
* Not copyable.
@@ -222,6 +224,9 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
nullptr};
PerformanceEntryReporter* performanceEntryReporter_{nullptr};
RuntimeSchedulerErrorHandler onTaskError_;
RuntimeSchedulerErrorHandler onMicrotaskError_;
};
} // namespace facebook::react