Skip to content

Commit 116fb6d

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Make time in C++ Animated injectable (#51749)
Summary: Pull Request resolved: #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
1 parent 80e8a6a commit 116fb6d

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535

3636
namespace facebook::react {
3737

38+
// Global function pointer for getting current time. Current time
39+
// can be injected for testing purposes.
40+
static TimePointFunction g_now = &std::chrono::steady_clock::now;
41+
void g_setNativeAnimatedNowTimestampFunction(TimePointFunction nowFunction) {
42+
g_now = nowFunction;
43+
}
44+
3845
namespace {
3946

4047
struct NodesQueueItem {
@@ -719,7 +726,7 @@ void NativeAnimatedNodesManager::onRender() {
719726
// Step through the animation loop
720727
if (isAnimationUpdateNeeded()) {
721728
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
722-
std::chrono::steady_clock::now().time_since_epoch())
729+
g_now().time_since_epoch())
723730
.count();
724731

725732
auto containsChange =

packages/react-native/ReactCxxPlatform/react/renderer/animated/NativeAnimatedNodesManager.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <react/renderer/animated/EventEmitterListener.h>
1515
#include <react/renderer/animated/event_drivers/EventAnimationDriver.h>
1616
#include <react/renderer/core/ReactPrimitives.h>
17+
#include <chrono>
1718
#include <memory>
1819
#include <mutex>
1920
#include <optional>
@@ -23,6 +24,11 @@
2324

2425
namespace facebook::react {
2526

27+
using TimePointFunction = std::chrono::steady_clock::time_point (*)();
28+
// A way to inject a custom time function for testing purposes.
29+
// Default is `std::chrono::steady_clock::now`.
30+
void g_setNativeAnimatedNowTimestampFunction(TimePointFunction nowFunction);
31+
2632
class AnimatedNode;
2733
class AnimationDriver;
2834
class Scheduler;
@@ -52,7 +58,7 @@ class NativeAnimatedNodesManager {
5258

5359
explicit NativeAnimatedNodesManager(
5460
DirectManipulationCallback&& directManipulationCallback,
55-
FabricCommitCallback&& fabricCommitCallback = nullptr,
61+
FabricCommitCallback&& fabricCommitCallback,
5662
StartOnRenderCallback&& startOnRenderCallback = nullptr,
5763
StopOnRenderCallback&& stopOnRenderCallback = nullptr) noexcept;
5864

0 commit comments

Comments
 (0)