From 116fb6dab037e28687da613e7ea119150fa21097 Mon Sep 17 00:00:00 2001 From: Samuel Susla Date: Tue, 3 Jun 2025 04:43:35 -0700 Subject: [PATCH] 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 --- .../renderer/animated/NativeAnimatedNodesManager.cpp | 9 ++++++++- .../react/renderer/animated/NativeAnimatedNodesManager.h | 8 +++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp index e08dadfaa96..9b4e658fdd8 100644 --- a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp @@ -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::steady_clock::now().time_since_epoch()) + g_now().time_since_epoch()) .count(); auto containsChange = diff --git a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h index d6be78b996c..1fe11ddf4ab 100644 --- a/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h +++ b/packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -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;