12
12
#import < React/RCTAssert.h>
13
13
#import < React/RCTBridge+Private.h>
14
14
#import < React/RCTBridge.h>
15
+ #import < React/RCTConvert.h>
15
16
#import < React/RCTLog.h>
16
17
#import < React/RCTUtils.h>
17
- #import < React/RCTConvert.h>
18
18
19
19
#import " CoreModulesPlugins.h"
20
20
@@ -75,8 +75,7 @@ @interface _RCTTimingProxy : NSObject
75
75
@end
76
76
77
77
// NSTimer retains its target, insert this class to break potential retain cycles
78
- @implementation _RCTTimingProxy
79
- {
78
+ @implementation _RCTTimingProxy {
80
79
__weak id _target;
81
80
}
82
81
@@ -96,16 +95,14 @@ - (void)timerDidFire
96
95
97
96
@end
98
97
99
- @interface RCTTiming () <NativeTimingSpec>
98
+ @interface RCTTiming () <NativeTimingSpec>
100
99
@end
101
100
102
- @implementation RCTTiming
103
- {
101
+ @implementation RCTTiming {
104
102
NSMutableDictionary <NSNumber *, _RCTTimer *> *_timers;
105
103
NSTimer *_sleepTimer;
106
104
BOOL _sendIdleEvents;
107
105
BOOL _inBackground;
108
- UIBackgroundTaskIdentifier _backgroundTaskIdentifier;
109
106
id <RCTTimingDelegate> _timingDelegate;
110
107
}
111
108
@@ -115,7 +112,7 @@ @implementation RCTTiming
115
112
116
113
RCT_EXPORT_MODULE ()
117
114
118
- - (instancetype )initWithDelegate:(id <RCTTimingDelegate>) delegate
115
+ - (instancetype )initWithDelegate:(id <RCTTimingDelegate>)delegate
119
116
{
120
117
if (self = [super init ]) {
121
118
[self setup ];
@@ -136,19 +133,19 @@ - (void)setup
136
133
_paused = YES ;
137
134
_timers = [NSMutableDictionary new ];
138
135
_inBackground = NO ;
139
- _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
140
136
141
- for (NSString *name in @[UIApplicationWillResignActiveNotification,
142
- UIApplicationDidEnterBackgroundNotification,
143
- UIApplicationWillTerminateNotification ]) {
137
+ for (NSString *name in @[
138
+ UIApplicationWillResignActiveNotification,
139
+ UIApplicationDidEnterBackgroundNotification,
140
+ UIApplicationWillTerminateNotification
141
+ ]) {
144
142
[[NSNotificationCenter defaultCenter ] addObserver: self
145
143
selector: @selector (appDidMoveToBackground )
146
144
name: name
147
145
object: nil ];
148
146
}
149
147
150
- for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
151
- UIApplicationWillEnterForegroundNotification ]) {
148
+ for (NSString *name in @[ UIApplicationDidBecomeActiveNotification, UIApplicationWillEnterForegroundNotification ]) {
152
149
[[NSNotificationCenter defaultCenter ] addObserver: self
153
150
selector: @selector (appDidMoveToForeground )
154
151
name: name
@@ -158,34 +155,9 @@ - (void)setup
158
155
159
156
- (void )dealloc
160
157
{
161
- [self markEndOfBackgroundTaskIfNeeded ];
162
158
[_sleepTimer invalidate ];
163
159
}
164
160
165
- - (void )markStartOfBackgroundTaskIfNeeded
166
- {
167
- if (_backgroundTaskIdentifier == UIBackgroundTaskInvalid) {
168
- __weak RCTTiming *weakSelf = self;
169
- // Marks the beginning of a new long-running background task. We can run the timer in the background.
170
- _backgroundTaskIdentifier = [[UIApplication sharedApplication ] beginBackgroundTaskWithName: @" rct.timing.gb.task" expirationHandler: ^{
171
- RCTTiming *strongSelf = weakSelf;
172
- if (!strongSelf) {
173
- return ;
174
- }
175
- // Mark the end of background task
176
- [strongSelf markEndOfBackgroundTaskIfNeeded ];
177
- }];
178
- }
179
- }
180
-
181
- - (void )markEndOfBackgroundTaskIfNeeded
182
- {
183
- if (_backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
184
- [[UIApplication sharedApplication ] endBackgroundTask: _backgroundTaskIdentifier];
185
- _backgroundTaskIdentifier = UIBackgroundTaskInvalid;
186
- }
187
- }
188
-
189
161
- (dispatch_queue_t )methodQueue
190
162
{
191
163
return RCTJSThread;
@@ -211,7 +183,6 @@ - (void)appDidMoveToBackground
211
183
212
184
- (void )appDidMoveToForeground
213
185
{
214
- [self markEndOfBackgroundTaskIfNeeded ];
215
186
_inBackground = NO ;
216
187
[self startTimers ];
217
188
}
@@ -246,7 +217,7 @@ - (void)startTimers
246
217
247
218
- (BOOL )hasPendingTimers
248
219
{
249
- @synchronized (_timers) {
220
+ @synchronized (_timers) {
250
221
return _sendIdleEvents || _timers.count > 0 ;
251
222
}
252
223
}
@@ -256,7 +227,7 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
256
227
NSDate *nextScheduledTarget = [NSDate distantFuture ];
257
228
NSMutableArray <_RCTTimer *> *timersToCall = [NSMutableArray new ];
258
229
NSDate *now = [NSDate date ]; // compare all the timers to the same base time
259
- @synchronized (_timers) {
230
+ @synchronized (_timers) {
260
231
for (_RCTTimer *timer in _timers.allValues ) {
261
232
if ([timer shouldFire: now]) {
262
233
[timersToCall addObject: timer];
@@ -272,7 +243,7 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
272
243
return [a.target compare: b.target];
273
244
}] valueForKey: @" callbackID" ];
274
245
if (_bridge) {
275
- [_bridge enqueueJSCall: @" JSTimers" method: @" callTimers" args: @[sortedTimers] completion: NULL ];
246
+ [_bridge enqueueJSCall: @" JSTimers" method: @" callTimers" args: @[ sortedTimers ] completion: NULL ];
276
247
} else {
277
248
[_timingDelegate callTimers: sortedTimers];
278
249
}
@@ -283,7 +254,7 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
283
254
[timer reschedule ];
284
255
nextScheduledTarget = [nextScheduledTarget earlierDate: timer.target];
285
256
} else {
286
- @synchronized (_timers) {
257
+ @synchronized (_timers) {
287
258
[_timers removeObjectForKey: timer.callbackID];
288
259
}
289
260
}
@@ -294,25 +265,23 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
294
265
NSTimeInterval frameElapsed = currentTimestamp - update.timestamp ;
295
266
if (kFrameDuration - frameElapsed >= kIdleCallbackFrameDeadline ) {
296
267
NSNumber *absoluteFrameStartMS = @((currentTimestamp - frameElapsed) * 1000 );
297
- if (_bridge){
298
- [_bridge enqueueJSCall: @" JSTimers" method: @" callIdleCallbacks" args: @[absoluteFrameStartMS] completion: NULL ];
268
+ if (_bridge) {
269
+ [_bridge enqueueJSCall: @" JSTimers" method: @" callIdleCallbacks" args: @[ absoluteFrameStartMS ] completion: NULL ];
299
270
} else {
300
271
[_timingDelegate callIdleCallbacks: absoluteFrameStartMS];
301
272
}
302
-
303
273
}
304
274
}
305
275
306
276
// Switch to a paused state only if we didn't call any timer this frame, so if
307
277
// in response to this timer another timer is scheduled, we don't pause and unpause
308
278
// the displaylink frivolously.
309
279
NSUInteger timerCount;
310
- @synchronized (_timers) {
280
+ @synchronized (_timers) {
311
281
timerCount = _timers.count ;
312
282
}
313
283
if (_inBackground) {
314
284
if (timerCount) {
315
- [self markStartOfBackgroundTaskIfNeeded ];
316
285
[self scheduleSleepTimer: nextScheduledTarget];
317
286
}
318
287
} else if (!_sendIdleEvents && timersToCall.count == 0 ) {
@@ -331,13 +300,13 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
331
300
332
301
- (void )scheduleSleepTimer : (NSDate *)sleepTarget
333
302
{
334
- @synchronized (self) {
303
+ @synchronized (self) {
335
304
if (!_sleepTimer || !_sleepTimer.valid ) {
336
305
_sleepTimer = [[NSTimer alloc ] initWithFireDate: sleepTarget
337
- interval: 0
338
- target: [_RCTTimingProxy proxyWithTarget: self ]
339
- selector: @selector (timerDidFire )
340
- userInfo: nil
306
+ interval: 0
307
+ target: [_RCTTimingProxy proxyWithTarget: self ]
308
+ selector: @selector (timerDidFire )
309
+ userInfo: nil
341
310
repeats: NO ];
342
311
[[NSRunLoop currentRunLoop ] addTimer: _sleepTimer forMode: NSDefaultRunLoopMode ];
343
312
} else {
@@ -367,13 +336,14 @@ - (void)timerDidFire
367
336
* calculating the timer's target time. We calculate this by passing in
368
337
* Date.now() from JS and then subtracting that from the current time here.
369
338
*/
370
- RCT_EXPORT_METHOD (createTimer:(double )callbackID
371
- duration:(NSTimeInterval )jsDuration
372
- jsSchedulingTime:(double )jsSchedulingTime
373
- repeats:(BOOL )repeats)
339
+ RCT_EXPORT_METHOD (createTimer
340
+ : (double )callbackID duration
341
+ : (NSTimeInterval )jsDuration jsSchedulingTime
342
+ : (double )jsSchedulingTime repeats
343
+ : (BOOL )repeats)
374
344
{
375
345
NSNumber *callbackIdObjc = [NSNumber numberWithDouble: callbackID];
376
- NSDate *schedulingTime = [RCTConvert NSDate: [NSNumber numberWithDouble: jsSchedulingTime]];
346
+ NSDate *schedulingTime = [RCTConvert NSDate: [NSNumber numberWithDouble: jsSchedulingTime]];
377
347
if (jsDuration == 0 && repeats == NO ) {
378
348
// For super fast, one-off timers, just enqueue them immediately rather than waiting a frame.
379
349
if (_bridge) {
@@ -384,10 +354,7 @@ - (void)timerDidFire
384
354
return ;
385
355
}
386
356
387
- [self createTimerForNextFrame: callbackIdObjc
388
- duration: jsDuration
389
- jsSchedulingTime: schedulingTime
390
- repeats: repeats];
357
+ [self createTimerForNextFrame: callbackIdObjc duration: jsDuration jsSchedulingTime: schedulingTime repeats: repeats];
391
358
}
392
359
393
360
/* *
@@ -410,12 +377,11 @@ - (void)createTimerForNextFrame:(nonnull NSNumber *)callbackID
410
377
interval: jsDuration
411
378
targetTime: targetTime
412
379
repeats: repeats];
413
- @synchronized (_timers) {
380
+ @synchronized (_timers) {
414
381
_timers[callbackID] = timer;
415
382
}
416
383
417
384
if (_inBackground) {
418
- [self markStartOfBackgroundTaskIfNeeded ];
419
385
[self scheduleSleepTimer: timer.target];
420
386
} else if (_paused) {
421
387
if ([timer.target timeIntervalSinceNow ] > kMinimumSleepInterval ) {
@@ -426,17 +392,17 @@ - (void)createTimerForNextFrame:(nonnull NSNumber *)callbackID
426
392
}
427
393
}
428
394
429
- RCT_EXPORT_METHOD (deleteTimer: (double )timerID)
395
+ RCT_EXPORT_METHOD (deleteTimer : (double )timerID)
430
396
{
431
- @synchronized (_timers) {
397
+ @synchronized (_timers) {
432
398
[_timers removeObjectForKey: [NSNumber numberWithDouble: timerID]];
433
399
}
434
400
if (![self hasPendingTimers ]) {
435
401
[self stopTimers ];
436
402
}
437
403
}
438
404
439
- RCT_EXPORT_METHOD (setSendIdleEvents: (BOOL )sendIdleEvents)
405
+ RCT_EXPORT_METHOD (setSendIdleEvents : (BOOL )sendIdleEvents)
440
406
{
441
407
_sendIdleEvents = sendIdleEvents;
442
408
if (sendIdleEvents) {
@@ -446,13 +412,15 @@ - (void)createTimerForNextFrame:(nonnull NSNumber *)callbackID
446
412
}
447
413
}
448
414
449
- - (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker : (std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
415
+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker :
416
+ (std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
450
417
{
451
418
return std::make_shared<facebook::react::NativeTimingSpecJSI>(self, jsInvoker);
452
419
}
453
420
454
421
@end
455
422
456
- Class RCTTimingCls (void ) {
423
+ Class RCTTimingCls (void )
424
+ {
457
425
return RCTTiming.class ;
458
426
}
0 commit comments