Skip to content

Commit 08ef1df

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Implement RuntimeScheduler::cancelTask
Summary: Changelog: [internal] Implement RuntimeScheduler::cancelTask and expose it through JSI. JavaScript implementation: https://github.com/facebook/react/blob/master/packages/scheduler/src/forks/SchedulerNoDOM.js#L384-L397 Reviewed By: mdvacca Differential Revision: D27648071 fbshipit-source-id: 91ae92b336017596fb658831824e971c7e047cd2
1 parent 3c14c19 commit 08ef1df

7 files changed

Lines changed: 49 additions & 4 deletions

File tree

ReactCommon/react/renderer/runtimescheduler/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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
)

ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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

ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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>,

ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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

ReactCommon/react/renderer/runtimescheduler/Task.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
1926
void 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

ReactCommon/react/renderer/runtimescheduler/Task.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
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

2730
class TaskPriorityComparer {

ReactCommon/react/renderer/runtimescheduler/primitives.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)