13
13
14
14
namespace facebook ::react {
15
15
16
+ namespace {
17
+ inline const char * getTimerSourceName (TimerSource source) {
18
+ switch (source) {
19
+ case TimerSource::Unknown:
20
+ return " unknown" ;
21
+ case TimerSource::SetTimeout:
22
+ return " setTimeout" ;
23
+ case TimerSource::SetInterval:
24
+ return " setInterval" ;
25
+ case TimerSource::RequestAnimationFrame:
26
+ return " requestAnimationFrame" ;
27
+ }
28
+ }
29
+ } // namespace
30
+
16
31
TimerManager::TimerManager (
17
32
std::unique_ptr<PlatformTimerRegistry> platformTimerRegistry) noexcept
18
33
: platformTimerRegistry_(std::move(platformTimerRegistry)) {}
@@ -60,14 +75,28 @@ void TimerManager::callReactNativeMicrotasks(jsi::Runtime& runtime) {
60
75
TimerHandle TimerManager::createTimer (
61
76
jsi::Function&& callback,
62
77
std::vector<jsi::Value>&& args,
63
- double delay) {
78
+ double delay,
79
+ TimerSource source) {
64
80
// Get the id for the callback.
65
81
TimerHandle timerID = timerIndex_++;
82
+
83
+ SystraceSection s (
84
+ " TimerManager::createTimer" ,
85
+ " id" ,
86
+ timerID,
87
+ " type" ,
88
+ getTimerSourceName (source),
89
+ " delay" ,
90
+ delay);
91
+
66
92
timers_.emplace (
67
93
std::piecewise_construct,
68
94
std::forward_as_tuple (timerID),
69
95
std::forward_as_tuple (
70
- std::move (callback), std::move (args), /* repeat */ false ));
96
+ std::move (callback),
97
+ std::move (args),
98
+ /* repeat */ false ,
99
+ source));
71
100
72
101
platformTimerRegistry_->createTimer (timerID, delay);
73
102
@@ -77,14 +106,25 @@ TimerHandle TimerManager::createTimer(
77
106
TimerHandle TimerManager::createRecurringTimer (
78
107
jsi::Function&& callback,
79
108
std::vector<jsi::Value>&& args,
80
- double delay) {
109
+ double delay,
110
+ TimerSource source) {
81
111
// Get the id for the callback.
82
112
TimerHandle timerID = timerIndex_++;
113
+
114
+ SystraceSection s (
115
+ " TimerManager::createRecurringTimer" ,
116
+ " id" ,
117
+ timerID,
118
+ " type" ,
119
+ getTimerSourceName (source),
120
+ " delay" ,
121
+ delay);
122
+
83
123
timers_.emplace (
84
124
std::piecewise_construct,
85
125
std::forward_as_tuple (timerID),
86
126
std::forward_as_tuple (
87
- std::move (callback), std::move (args), /* repeat */ true ));
127
+ std::move (callback), std::move (args), /* repeat */ true , source ));
88
128
89
129
platformTimerRegistry_->createRecurringTimer (timerID, delay);
90
130
@@ -131,11 +171,20 @@ void TimerManager::deleteRecurringTimer(
131
171
132
172
void TimerManager::callTimer (TimerHandle timerHandle) {
133
173
runtimeExecutor_ ([this , timerHandle](jsi::Runtime& runtime) {
134
- SystraceSection s (" TimerManager::callTimer" );
135
174
auto it = timers_.find (timerHandle);
136
175
if (it != timers_.end ()) {
137
- bool repeats = it->second .repeat ;
138
- it->second .invoke (runtime);
176
+ auto & timerCallback = it->second ;
177
+ bool repeats = timerCallback.repeat ;
178
+
179
+ {
180
+ SystraceSection s (
181
+ " TimerManager::callTimer" ,
182
+ " id" ,
183
+ timerHandle,
184
+ " type" ,
185
+ getTimerSourceName (timerCallback.source ));
186
+ timerCallback.invoke (runtime);
187
+ }
139
188
140
189
if (!repeats) {
141
190
// Invoking a timer has the potential to delete it. Do not re-use the
@@ -246,7 +295,11 @@ void TimerManager::attachGlobals(jsi::Runtime& runtime) {
246
295
moreArgs.emplace_back (rt, args[extraArgNum]);
247
296
}
248
297
249
- return createTimer (std::move (callback), std::move (moreArgs), delay);
298
+ return createTimer (
299
+ std::move (callback),
300
+ std::move (moreArgs),
301
+ delay,
302
+ TimerSource::SetTimeout);
250
303
}));
251
304
252
305
runtime.global ().setProperty (
@@ -301,7 +354,10 @@ void TimerManager::attachGlobals(jsi::Runtime& runtime) {
301
354
}
302
355
303
356
return createRecurringTimer (
304
- std::move (callback), std::move (moreArgs), delay);
357
+ std::move (callback),
358
+ std::move (moreArgs),
359
+ delay,
360
+ TimerSource::SetInterval);
305
361
}));
306
362
307
363
runtime.global ().setProperty (
@@ -370,7 +426,8 @@ void TimerManager::attachGlobals(jsi::Runtime& runtime) {
370
426
return createTimer (
371
427
std::move (callback),
372
428
std::vector<jsi::Value>(),
373
- /* delay */ 0 );
429
+ /* delay */ 0 ,
430
+ TimerSource::RequestAnimationFrame);
374
431
}));
375
432
376
433
runtime.global ().setProperty (
0 commit comments