2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTTiming.h"
|
|
|
|
|
2015-02-24 17:06:57 +00:00
|
|
|
#import "RCTAssert.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTSparseArray.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2015-04-17 11:02:37 +00:00
|
|
|
@interface RCTBridge (Private)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow super fast, one time, timers to skip the queue and be directly executed
|
|
|
|
*/
|
|
|
|
- (void)_immediatelyCallTimer:(NSNumber *)timer;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@interface RCTTimer : NSObject
|
|
|
|
|
|
|
|
@property (nonatomic, strong, readonly) NSDate *target;
|
|
|
|
@property (nonatomic, assign, readonly) BOOL repeats;
|
2015-02-24 17:06:57 +00:00
|
|
|
@property (nonatomic, copy, readonly) NSNumber *callbackID;
|
2015-02-20 04:10:52 +00:00
|
|
|
@property (nonatomic, assign, readonly) NSTimeInterval interval;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTTimer
|
|
|
|
|
|
|
|
- (instancetype)initWithCallbackID:(NSNumber *)callbackID
|
|
|
|
interval:(NSTimeInterval)interval
|
|
|
|
targetTime:(NSTimeInterval)targetTime
|
|
|
|
repeats:(BOOL)repeats
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
_interval = interval;
|
|
|
|
_repeats = repeats;
|
|
|
|
_callbackID = callbackID;
|
|
|
|
_target = [NSDate dateWithTimeIntervalSinceNow:targetTime];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns `YES` if we should invoke the JS callback.
|
|
|
|
*/
|
|
|
|
- (BOOL)updateFoundNeedsJSUpdate
|
|
|
|
{
|
|
|
|
if (_target && _target.timeIntervalSinceNow <= 0) {
|
|
|
|
// The JS Timers will do fine grained calculating of expired timeouts.
|
|
|
|
_target = _repeats ? [NSDate dateWithTimeIntervalSinceNow:_interval] : nil;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTTiming
|
|
|
|
{
|
|
|
|
RCTSparseArray *_timers;
|
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
@synthesize bridge = _bridge;
|
2015-05-04 17:35:49 +00:00
|
|
|
@synthesize paused = _paused;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
- (instancetype)init
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-05-04 17:35:49 +00:00
|
|
|
_paused = YES;
|
2015-08-17 14:35:34 +00:00
|
|
|
_timers = [RCTSparseArray new];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
for (NSString *name in @[UIApplicationWillResignActiveNotification,
|
|
|
|
UIApplicationDidEnterBackgroundNotification,
|
|
|
|
UIApplicationWillTerminateNotification]) {
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(stopTimers)
|
|
|
|
name:name
|
|
|
|
object:nil];
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
|
|
|
|
UIApplicationWillEnterForegroundNotification]) {
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(startTimers)
|
|
|
|
name:name
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2015-04-20 19:06:02 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
2015-04-26 02:18:39 +00:00
|
|
|
return RCTJSThread;
|
2015-04-20 19:06:02 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
[self stopTimers];
|
|
|
|
_bridge = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopTimers
|
|
|
|
{
|
2015-05-04 17:35:49 +00:00
|
|
|
_paused = YES;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startTimers
|
|
|
|
{
|
2015-08-14 08:59:42 +00:00
|
|
|
if (!_bridge || _timers.count == 0) {
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-04 17:35:49 +00:00
|
|
|
_paused = NO;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
- (void)didUpdateFrame:(__unused RCTFrameUpdate *)update
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-08-17 14:35:34 +00:00
|
|
|
NSMutableArray *timersToCall = [NSMutableArray new];
|
2015-02-20 04:10:52 +00:00
|
|
|
for (RCTTimer *timer in _timers.allObjects) {
|
|
|
|
if ([timer updateFoundNeedsJSUpdate]) {
|
|
|
|
[timersToCall addObject:timer.callbackID];
|
|
|
|
}
|
|
|
|
if (!timer.target) {
|
|
|
|
_timers[timer.callbackID] = nil;
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// call timers that need to be called
|
2015-08-24 10:14:33 +00:00
|
|
|
if (timersToCall.count > 0) {
|
2015-06-15 17:48:24 +00:00
|
|
|
[_bridge enqueueJSCall:@"JSTimersExecution.callTimers" args:@[timersToCall]];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-06-07 15:42:31 +00:00
|
|
|
|
|
|
|
if (_timers.count == 0) {
|
|
|
|
[self stopTimers];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* There's a small difference between the time when we call
|
|
|
|
* setTimeout/setInterval/requestAnimation frame and the time it actually makes
|
|
|
|
* it here. This is important and needs to be taken into account when
|
|
|
|
* calculating the timer's target time. We calculate this by passing in
|
|
|
|
* Date.now() from JS and then subtracting that from the current time here.
|
|
|
|
*/
|
2015-07-31 13:55:47 +00:00
|
|
|
RCT_EXPORT_METHOD(createTimer:(nonnull NSNumber *)callbackID
|
2015-04-08 15:52:48 +00:00
|
|
|
duration:(NSTimeInterval)jsDuration
|
|
|
|
jsSchedulingTime:(NSDate *)jsSchedulingTime
|
|
|
|
repeats:(BOOL)repeats)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-03-18 05:22:51 +00:00
|
|
|
if (jsDuration == 0 && repeats == NO) {
|
|
|
|
// For super fast, one-off timers, just enqueue them immediately rather than waiting a frame.
|
2015-04-17 11:02:37 +00:00
|
|
|
[_bridge _immediatelyCallTimer:callbackID];
|
2015-03-18 05:22:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
NSTimeInterval jsSchedulingOverhead = -jsSchedulingTime.timeIntervalSinceNow;
|
2015-02-20 04:10:52 +00:00
|
|
|
if (jsSchedulingOverhead < 0) {
|
|
|
|
RCTLogWarn(@"jsSchedulingOverhead (%ims) should be positive", (int)(jsSchedulingOverhead * 1000));
|
2015-06-13 00:01:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Probably debugging on device, set to 0 so we don't ignore the interval
|
|
|
|
*/
|
|
|
|
jsSchedulingOverhead = 0;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
NSTimeInterval targetTime = jsDuration - jsSchedulingOverhead;
|
|
|
|
if (jsDuration < 0.018) { // Make sure short intervals run each frame
|
|
|
|
jsDuration = 0;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTTimer *timer = [[RCTTimer alloc] initWithCallbackID:callbackID
|
2015-04-08 15:52:48 +00:00
|
|
|
interval:jsDuration
|
2015-02-20 04:10:52 +00:00
|
|
|
targetTime:targetTime
|
|
|
|
repeats:repeats];
|
2015-04-18 17:43:20 +00:00
|
|
|
_timers[callbackID] = timer;
|
|
|
|
[self startTimers];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-07-31 13:55:47 +00:00
|
|
|
RCT_EXPORT_METHOD(deleteTimer:(nonnull NSNumber *)timerID)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-07-31 13:55:47 +00:00
|
|
|
_timers[timerID] = nil;
|
|
|
|
if (_timers.count == 0) {
|
|
|
|
[self stopTimers];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|