77
88#import " RCTTiming.h"
99
10- #import < FBReactNativeSpec/FBReactNativeSpec.h>
11-
1210#import < React/RCTAssert.h>
13- #import < React/RCTBridge+Private.h>
14- #import < React/RCTBridge.h>
1511#import < React/RCTConvert.h>
1612#import < React/RCTLog.h>
1713#import < React/RCTUtils.h>
1814
19- #import " CoreModulesPlugins.h"
20-
2115static const NSTimeInterval kMinimumSleepInterval = 1 ;
2216
2317// These timing contants should be kept in sync with the ones in `JSTimers.js`.
@@ -82,7 +76,7 @@ @implementation _RCTTimingProxy {
8276+ (instancetype )proxyWithTarget : (id )target
8377{
8478 _RCTTimingProxy *proxy = [self new ];
85- if (proxy) {
79+ if (proxy != nullptr ) {
8680 proxy->_target = target;
8781 }
8882 return proxy;
@@ -103,12 +97,9 @@ @implementation RCTTiming {
10397 id <RCTTimingDelegate> _timingDelegate;
10498}
10599
106- @synthesize bridge = _bridge;
107100@synthesize paused = _paused;
108101@synthesize pauseCallback = _pauseCallback;
109102
110- RCT_EXPORT_MODULE ()
111-
112103- (instancetype )initWithDelegate : (id <RCTTimingDelegate>)delegate
113104{
114105 if (self = [super init ]) {
@@ -165,15 +156,9 @@ - (void)dealloc
165156 [_sleepTimer invalidate ];
166157}
167158
168- - (dispatch_queue_t )methodQueue
169- {
170- return RCTJSThread;
171- }
172-
173159- (void )invalidate
174160{
175161 [self stopTimers ];
176- _bridge = nil ;
177162 _timingDelegate = nil ;
178163}
179164
@@ -220,7 +205,7 @@ - (void)stopTimers
220205
221206- (void )startTimers
222207{
223- if ((!_bridge && ! _timingDelegate) || _inBackground || ![self hasPendingTimers ]) {
208+ if ((!_timingDelegate) || _inBackground || ![self hasPendingTimers ]) {
224209 return ;
225210 }
226211
@@ -259,11 +244,7 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
259244 NSArray <NSNumber *> *sortedTimers = [[timersToCall sortedArrayUsingComparator: ^(_RCTTimer *a, _RCTTimer *b) {
260245 return [a.target compare: b.target];
261246 }] valueForKey: @" callbackID" ];
262- if (_bridge) {
263- [_bridge enqueueJSCall: @" JSTimers" method: @" callTimers" args: @[ sortedTimers ] completion: NULL ];
264- } else {
265- [_timingDelegate callTimers: sortedTimers];
266- }
247+ [_timingDelegate callTimers: sortedTimers];
267248 }
268249
269250 for (_RCTTimer *timer in timersToCall) {
@@ -282,23 +263,19 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
282263 if (kFrameDuration - frameElapsed >= kIdleCallbackFrameDeadline ) {
283264 NSTimeInterval currentTimestamp = [[NSDate date ] timeIntervalSince1970 ];
284265 NSNumber *absoluteFrameStartMS = @((currentTimestamp - frameElapsed) * 1000 );
285- if (_bridge) {
286- [_bridge enqueueJSCall: @" JSTimers" method: @" callIdleCallbacks" args: @[ absoluteFrameStartMS ] completion: NULL ];
287- } else {
288- [_timingDelegate callIdleCallbacks: absoluteFrameStartMS];
289- }
266+ [_timingDelegate callIdleCallbacks: absoluteFrameStartMS];
290267 }
291268 }
292269
293270 // Switch to a paused state only if we didn't call any timer this frame, so if
294271 // in response to this timer another timer is scheduled, we don't pause and unpause
295272 // the displaylink frivolously.
296- NSUInteger timerCount;
273+ NSUInteger timerCount = 0 ;
297274 @synchronized (_timers) {
298275 timerCount = _timers.count ;
299276 }
300277 if (_inBackground) {
301- if (timerCount) {
278+ if (timerCount != 0u ) {
302279 [self scheduleSleepTimer: nextScheduledTarget];
303280 }
304281 } else if (!_sendIdleEvents && timersToCall.count == 0 ) {
@@ -318,7 +295,7 @@ - (void)didUpdateFrame:(RCTFrameUpdate *)update
318295- (void )scheduleSleepTimer : (NSDate *)sleepTarget
319296{
320297 @synchronized (self) {
321- if (! _sleepTimer || !_sleepTimer.valid ) {
298+ if (( _sleepTimer == nullptr ) || !_sleepTimer.valid ) {
322299 _sleepTimer = [[NSTimer alloc ] initWithFireDate: sleepTarget
323300 interval: 0
324301 target: [_RCTTimingProxy proxyWithTarget: self ]
@@ -343,35 +320,6 @@ - (void)timerDidFire
343320 }
344321}
345322
346- /* *
347- * A method used for asynchronously creating a timer. If the timer has already expired,
348- * (based on the provided jsSchedulingTime) then it will be immediately invoked.
349- *
350- * There's a small difference between the time when we call
351- * setTimeout/setInterval/requestAnimation frame and the time it actually makes
352- * it here. This is important and needs to be taken into account when
353- * calculating the timer's target time. We calculate this by passing in
354- * Date.now() from JS and then subtracting that from the current time here.
355- */
356- RCT_EXPORT_METHOD (
357- createTimer : (double )callbackID duration : (NSTimeInterval )jsDuration jsSchedulingTime : (double )
358- jsSchedulingTime repeats : (BOOL )repeats)
359- {
360- NSNumber *callbackIdObjc = [NSNumber numberWithDouble: callbackID];
361- NSDate *schedulingTime = [RCTConvert NSDate: [NSNumber numberWithDouble: jsSchedulingTime]];
362- if (jsDuration == 0 && repeats == NO ) {
363- // For super fast, one-off timers, just enqueue them immediately rather than waiting a frame.
364- if (_bridge) {
365- [_bridge _immediatelyCallTimer: callbackIdObjc];
366- } else {
367- [_timingDelegate immediatelyCallTimer: callbackIdObjc];
368- }
369- return ;
370- }
371-
372- [self createTimerForNextFrame: callbackIdObjc duration: jsDuration jsSchedulingTime: schedulingTime repeats: repeats];
373- }
374-
375323/* *
376324 * A method used for synchronously creating a timer. The timer will not be invoked until the
377325 * next frame, regardless of whether it has already expired (i.e. jsSchedulingTime is 0).
@@ -407,29 +355,14 @@ - (void)createTimerForNextFrame:(nonnull NSNumber *)callbackID
407355 }
408356}
409357
410- RCT_EXPORT_METHOD ( deleteTimer : (double )timerID)
358+ - ( void ) deleteTimer : (double )timerID
411359{
412360 @synchronized (_timers) {
413- [_timers removeObjectForKey: [ NSNumber numberWithDouble: timerID] ];
361+ [_timers removeObjectForKey: @( timerID) ];
414362 }
415363 if (![self hasPendingTimers ]) {
416364 [self stopTimers ];
417365 }
418366}
419367
420- RCT_EXPORT_METHOD (setSendIdleEvents : (BOOL )sendIdleEvents)
421- {
422- _sendIdleEvents = sendIdleEvents;
423- if (sendIdleEvents) {
424- [self startTimers ];
425- } else if (![self hasPendingTimers ]) {
426- [self stopTimers ];
427- }
428- }
429-
430368@end
431-
432- Class RCTTimingCls (void )
433- {
434- return RCTTiming.class ;
435- }
0 commit comments