diff --git a/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp b/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp index d7978b95f6f..e257d1f6e32 100644 --- a/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp +++ b/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.cpp @@ -30,4 +30,8 @@ bool RuntimeScheduler::getShouldYield() const { return shouldYield_; } +RuntimeSchedulerClock::time_point RuntimeScheduler::now() const { + return RuntimeSchedulerClock::now(); +} + } // namespace facebook::react diff --git a/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.h b/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.h index be0059853a7..a1af49551fd 100644 --- a/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.h +++ b/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler.h @@ -10,11 +10,17 @@ #include #include #include +#include #include #include 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, diff --git a/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.cpp b/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.cpp index 71da7c66cc8..c2a72ef7572 100644 --- a/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.cpp +++ b/ReactCommon/react/renderer/runtimescheduler/RuntimeSchedulerBinding.cpp @@ -10,6 +10,7 @@ #include "primitives.h" #include +#include #include 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(now.time_since_epoch()) + .count(); + return jsi::Value(asDouble); + }); + } + if (propertyName == "unstable_ImmediatePriority") { return jsi::Value(runtime, serialize(SchedulerPriority::ImmediatePriority)); }