Implement RuntimeScheduler::now

Summary:
Changelog: [internal]

Implement RuntimeScheduler::now and exposite it to JavaScript. JavaScripts expects miliseconds and is using `performance.now()` call to get this value.

More about `performance.now()`: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
JavaScript implementation in Scheduler: https://github.com/facebook/react/blob/master/packages/scheduler/src/forks/SchedulerNoDOM.js#L71-L82

Reviewed By: JoshuaGross

Differential Revision: D27675512

fbshipit-source-id: bce5ac700e71b1722cd280dfbdd15e141783a3a5
This commit is contained in:
Samuel Susla
2021-04-14 02:53:24 -07:00
committed by Facebook GitHub Bot
parent 52f1d54509
commit 240588b6d4
3 changed files with 31 additions and 0 deletions
@@ -30,4 +30,8 @@ bool RuntimeScheduler::getShouldYield() const {
return shouldYield_;
}
RuntimeSchedulerClock::time_point RuntimeScheduler::now() const {
return RuntimeSchedulerClock::now();
}
} // namespace facebook::react
@@ -10,11 +10,17 @@
#include <ReactCommon/RuntimeExecutor.h>
#include <react/renderer/runtimescheduler/Task.h>
#include <atomic>
#include <chrono>
#include <memory>
#include <queue>
namespace facebook::react {
/*
* Represents a monotonic clock suitable for measuring intervals.
*/
using RuntimeSchedulerClock = std::chrono::steady_clock;
class RuntimeScheduler final {
public:
RuntimeScheduler(RuntimeExecutor const &runtimeExecutor);
@@ -25,6 +31,8 @@ class RuntimeScheduler final {
bool getShouldYield() const;
RuntimeSchedulerClock::time_point now() const;
private:
mutable std::priority_queue<
std::shared_ptr<Task>,
@@ -10,6 +10,7 @@
#include "primitives.h"
#include <react/debug/react_native_assert.h>
#include <chrono>
#include <memory>
namespace facebook::react {
@@ -115,6 +116,24 @@ jsi::Value RuntimeSchedulerBinding::get(
});
}
if (propertyName == "unstable_now") {
return jsi::Function::createFromHostFunction(
runtime,
name,
0,
[this](
jsi::Runtime &,
jsi::Value const &,
jsi::Value const *,
size_t) noexcept -> jsi::Value {
auto now = runtimeScheduler_->now();
auto asDouble =
std::chrono::duration<double, std::milli>(now.time_since_epoch())
.count();
return jsi::Value(asDouble);
});
}
if (propertyName == "unstable_ImmediatePriority") {
return jsi::Value(runtime, serialize(SchedulerPriority::ImmediatePriority));
}