File tree Expand file tree Collapse file tree
ReactCommon/react/renderer/runtimescheduler Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -50,5 +50,6 @@ rn_xplat_cxx_library(
5050 deps = [
5151 react_native_xplat_target ("runtimeexecutor:runtimeexecutor" ),
5252 react_native_xplat_target ("react/renderer/debug:debug" ),
53+ react_native_xplat_target ("better:better" ),
5354 ],
5455)
Original file line number Diff line number Diff line change @@ -22,4 +22,8 @@ void RuntimeScheduler::scheduleTask(std::shared_ptr<Task> const &task) {
2222 });
2323}
2424
25+ void RuntimeScheduler::cancelTask (const std::shared_ptr<Task> &task) {
26+ task->cancel ();
27+ }
28+
2529} // namespace facebook::react
Original file line number Diff line number Diff line change @@ -20,6 +20,8 @@ class RuntimeScheduler final {
2020
2121 void scheduleTask (std::shared_ptr<Task> const &task);
2222
23+ void cancelTask (std::shared_ptr<Task> const &task);
24+
2325 private:
2426 mutable std::priority_queue<
2527 std::shared_ptr<Task>,
Original file line number Diff line number Diff line change @@ -70,6 +70,20 @@ jsi::Value RuntimeSchedulerBinding::get(
7070 });
7171 }
7272
73+ if (propertyName == " unstable_cancelCallback" ) {
74+ return jsi::Function::createFromHostFunction (
75+ runtime,
76+ name,
77+ 1 ,
78+ [this ](
79+ jsi::Runtime &runtime,
80+ jsi::Value const &,
81+ jsi::Value const *arguments,
82+ size_t ) noexcept -> jsi::Value {
83+ runtimeScheduler_.cancelTask (taskFromValue (runtime, arguments[0 ]));
84+ return jsi::Value::undefined ();
85+ });
86+ }
7387 if (propertyName == " unstable_ImmediatePriority" ) {
7488 return jsi::Value (runtime, serialize (SchedulerPriority::ImmediatePriority));
7589 }
@@ -91,6 +105,7 @@ jsi::Value RuntimeSchedulerBinding::get(
91105 return jsi::Value (runtime, serialize (SchedulerPriority::IdlePriority));
92106 }
93107
108+ react_native_assert (false && " undefined property" );
94109 return jsi::Value::undefined ();
95110}
96111
Original file line number Diff line number Diff line change @@ -16,8 +16,18 @@ SchedulerPriority Task::getPriority() const {
1616 return priority_;
1717}
1818
19+ void Task::cancel () {
20+ // Null out the callback to indicate the task has been canceled. (Can't
21+ // remove from the priority_queue because you can't remove arbitrary nodes
22+ // from an array based heap, only the first one.)
23+ callback_.reset ();
24+ }
25+
1926void Task::operator ()(jsi::Runtime &runtime) const {
20- callback_.call (runtime, {});
27+ if (callback_) {
28+ // Cancelled task doesn't have a callback.
29+ callback_.value ().call (runtime, {});
30+ }
2131}
2232
2333} // namespace facebook::react
Original file line number Diff line number Diff line change 77
88#pragma once
99
10+ #include < better/optional.h>
1011#include < jsi/jsi.h>
1112#include < react/renderer/runtimescheduler/SchedulerPriority.h>
1213
@@ -16,12 +17,14 @@ class Task final {
1617 public:
1718 Task (SchedulerPriority priority, jsi::Function callback);
1819
19- SchedulerPriority priority_;
20- jsi::Function callback_;
21-
2220 SchedulerPriority getPriority () const ;
21+ void cancel ();
2322
2423 void operator ()(jsi::Runtime &runtime) const ;
24+
25+ private:
26+ SchedulerPriority priority_;
27+ better::optional<jsi::Function> callback_;
2528};
2629
2730class TaskPriorityComparer {
Original file line number Diff line number Diff line change @@ -26,4 +26,14 @@ inline static jsi::Value valueFromTask(
2626 runtime, std::make_shared<TaskWrapper>(task));
2727}
2828
29+ inline static std::shared_ptr<Task> taskFromValue (
30+ jsi::Runtime &runtime,
31+ jsi::Value const &value) {
32+ if (value.isNull ()) {
33+ return nullptr ;
34+ }
35+
36+ return value.getObject (runtime).getHostObject <TaskWrapper>(runtime)->task ;
37+ }
38+
2939} // namespace facebook::react
You can’t perform that action at this time.
0 commit comments