Skip to content

Commit

Permalink
Implement RuntimeScheduler::getShouldYield
Browse files Browse the repository at this point in the history
Summary:
Changelog: [internal]

Implement RuntimeScheduler::getShouldYield and expose it through JSI.
For now we are only returning `false`. The value is backed by atomic_bool and in the future we will be able to indicate that React should yield to native.

JavaScript implementation:
https://github.com/facebook/react/blob/master/packages/scheduler/src/forks/SchedulerNoDOM.js#L439-L441

Reviewed By: JoshuaGross

Differential Revision: D27648579

fbshipit-source-id: b9313e2efbd9daae8975357df9de803f24a35e89
  • Loading branch information
sammy-SC authored and facebook-github-bot committed Apr 14, 2021
1 parent aee07e1 commit cc3e87c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ void RuntimeScheduler::cancelTask(const std::shared_ptr<Task> &task) {
task->cancel();
}

bool RuntimeScheduler::getShouldYield() const {
return shouldYield_;
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <ReactCommon/RuntimeExecutor.h>
#include <react/renderer/runtimescheduler/Task.h>
#include <atomic>
#include <memory>
#include <queue>

Expand All @@ -22,13 +23,16 @@ class RuntimeScheduler final {

void cancelTask(std::shared_ptr<Task> const &task);

bool getShouldYield() const;

private:
mutable std::priority_queue<
std::shared_ptr<Task>,
std::vector<std::shared_ptr<Task>>,
TaskPriorityComparer>
taskQueue_;
RuntimeExecutor const runtimeExecutor_;
std::atomic_bool shouldYield_{false};
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ RuntimeSchedulerBinding::createAndInstallIfNeeded(
if (runtimeSchedulerValue.isUndefined()) {
// The global namespace does not have an instance of the binding;
// we need to create, install and return it.
auto runtimeSchedulerBinding = std::make_shared<RuntimeSchedulerBinding>(
RuntimeScheduler(runtimeExecutor));
auto runtimeScheduler = std::make_unique<RuntimeScheduler>(runtimeExecutor);
auto runtimeSchedulerBinding =
std::make_shared<RuntimeSchedulerBinding>(std::move(runtimeScheduler));
auto object =
jsi::Object::createFromHostObject(runtime, runtimeSchedulerBinding);
runtime.global().setProperty(
Expand All @@ -41,7 +42,7 @@ RuntimeSchedulerBinding::createAndInstallIfNeeded(
}

RuntimeSchedulerBinding::RuntimeSchedulerBinding(
RuntimeScheduler runtimeScheduler)
std::unique_ptr<RuntimeScheduler> runtimeScheduler)
: runtimeScheduler_(std::move(runtimeScheduler)) {}

jsi::Value RuntimeSchedulerBinding::get(
Expand All @@ -64,7 +65,7 @@ jsi::Value RuntimeSchedulerBinding::get(
react_native_assert(arguments[2].isUndefined());

auto task = std::make_shared<Task>(priority, std::move(callback));
runtimeScheduler_.scheduleTask(task);
runtimeScheduler_->scheduleTask(task);

return valueFromTask(runtime, task);
});
Expand All @@ -80,10 +81,25 @@ jsi::Value RuntimeSchedulerBinding::get(
jsi::Value const &,
jsi::Value const *arguments,
size_t) noexcept -> jsi::Value {
runtimeScheduler_.cancelTask(taskFromValue(runtime, arguments[0]));
runtimeScheduler_->cancelTask(taskFromValue(runtime, arguments[0]));
return jsi::Value::undefined();
});
}

if (propertyName == "unstable_shouldYield") {
return jsi::Function::createFromHostFunction(
runtime,
name,
0,
[this](
jsi::Runtime &,
jsi::Value const &,
jsi::Value const *,
size_t) noexcept -> jsi::Value {
auto shouldYield = runtimeScheduler_->getShouldYield();
return jsi::Value(shouldYield);
});
}
if (propertyName == "unstable_ImmediatePriority") {
return jsi::Value(runtime, serialize(SchedulerPriority::ImmediatePriority));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace facebook::react {
*/
class RuntimeSchedulerBinding : public jsi::HostObject {
public:
RuntimeSchedulerBinding(RuntimeScheduler runtimeScheduler);
RuntimeSchedulerBinding(std::unique_ptr<RuntimeScheduler> runtimeScheduler);

/*
* Installs RuntimeSchedulerBinding into JavaScript runtime if needed.
Expand All @@ -35,7 +35,7 @@ class RuntimeSchedulerBinding : public jsi::HostObject {
jsi::Value get(jsi::Runtime &runtime, jsi::PropNameID const &name) override;

private:
RuntimeScheduler runtimeScheduler_;
std::unique_ptr<RuntimeScheduler> runtimeScheduler_;
};

} // namespace facebook::react

0 comments on commit cc3e87c

Please sign in to comment.