Make time in C++ Animated injectable (#51749)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51749

changelog: [internal]

Make it possible to inject time via `now` argument to C++ Animated. This will be used in testing.

Reviewed By: javache

Differential Revision: D75710463

fbshipit-source-id: 2d6da875c7379c4b229f8b7af0fa665cebc2ca8b
This commit is contained in:
Samuel Susla
2025-06-03 04:43:35 -07:00
committed by Facebook GitHub Bot
parent 80e8a6a161
commit 116fb6dab0
2 changed files with 15 additions and 2 deletions
@@ -35,6 +35,13 @@
namespace facebook::react {
// Global function pointer for getting current time. Current time
// can be injected for testing purposes.
static TimePointFunction g_now = &std::chrono::steady_clock::now;
void g_setNativeAnimatedNowTimestampFunction(TimePointFunction nowFunction) {
g_now = nowFunction;
}
namespace {
struct NodesQueueItem {
@@ -719,7 +726,7 @@ void NativeAnimatedNodesManager::onRender() {
// Step through the animation loop
if (isAnimationUpdateNeeded()) {
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now().time_since_epoch())
g_now().time_since_epoch())
.count();
auto containsChange =
@@ -14,6 +14,7 @@
#include <react/renderer/animated/EventEmitterListener.h>
#include <react/renderer/animated/event_drivers/EventAnimationDriver.h>
#include <react/renderer/core/ReactPrimitives.h>
#include <chrono>
#include <memory>
#include <mutex>
#include <optional>
@@ -23,6 +24,11 @@
namespace facebook::react {
using TimePointFunction = std::chrono::steady_clock::time_point (*)();
// A way to inject a custom time function for testing purposes.
// Default is `std::chrono::steady_clock::now`.
void g_setNativeAnimatedNowTimestampFunction(TimePointFunction nowFunction);
class AnimatedNode;
class AnimationDriver;
class Scheduler;
@@ -52,7 +58,7 @@ class NativeAnimatedNodesManager {
explicit NativeAnimatedNodesManager(
DirectManipulationCallback&& directManipulationCallback,
FabricCommitCallback&& fabricCommitCallback = nullptr,
FabricCommitCallback&& fabricCommitCallback,
StartOnRenderCallback&& startOnRenderCallback = nullptr,
StopOnRenderCallback&& stopOnRenderCallback = nullptr) noexcept;