2015-02-20 04:10:52 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#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"
|
|
|
|
|
|
|
|
@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;
|
|
|
|
id _updateTimer;
|
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
@synthesize bridge = _bridge;
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
+ (NSArray *)JSMethods
|
|
|
|
{
|
|
|
|
return @[@"RCTJSTimers.callTimers"];
|
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
- (instancetype)init
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
_timers = [[RCTSparseArray alloc] init];
|
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];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
|
|
|
return _bridge != nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
|
|
|
[self stopTimers];
|
|
|
|
_bridge = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)stopTimers
|
|
|
|
{
|
|
|
|
[_updateTimer invalidate];
|
|
|
|
_updateTimer = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startTimers
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
2015-03-01 23:33:55 +00:00
|
|
|
|
|
|
|
if (![self isValid] || _updateTimer != nil || _timers.count == 0) {
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateTimer = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
|
|
|
|
if (_updateTimer) {
|
2015-03-01 23:33:55 +00:00
|
|
|
[_updateTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
2015-02-20 04:10:52 +00:00
|
|
|
} else {
|
|
|
|
RCTLogWarn(@"Failed to create a display link (probably on buildbot) - using an NSTimer for AppEngine instead.");
|
|
|
|
_updateTimer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60)
|
|
|
|
target:self
|
|
|
|
selector:@selector(update)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)update
|
|
|
|
{
|
|
|
|
RCTAssertMainThread();
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSMutableArray *timersToCall = [[NSMutableArray alloc] init];
|
|
|
|
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
|
|
|
|
if ([timersToCall count] > 0) {
|
|
|
|
[_bridge enqueueJSCall:@"RCTJSTimers.callTimers" args:@[timersToCall]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
- (void)createTimer:(NSNumber *)callbackID
|
|
|
|
duration:(double)jsDuration
|
|
|
|
jsSchedulingTime:(double)jsSchedulingTime
|
|
|
|
repeats:(BOOL)repeats
|
|
|
|
{
|
|
|
|
RCT_EXPORT();
|
|
|
|
|
|
|
|
NSTimeInterval interval = jsDuration / 1000;
|
|
|
|
NSTimeInterval jsCreationTimeSinceUnixEpoch = jsSchedulingTime / 1000;
|
|
|
|
NSTimeInterval currentTimeSinceUnixEpoch = [[NSDate date] timeIntervalSince1970];
|
|
|
|
NSTimeInterval jsSchedulingOverhead = currentTimeSinceUnixEpoch - jsCreationTimeSinceUnixEpoch;
|
|
|
|
if (jsSchedulingOverhead < 0) {
|
|
|
|
RCTLogWarn(@"jsSchedulingOverhead (%ims) should be positive", (int)(jsSchedulingOverhead * 1000));
|
|
|
|
}
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSTimeInterval targetTime = interval - jsSchedulingOverhead;
|
|
|
|
if (interval < 0.018) { // Make sure short intervals run each frame
|
|
|
|
interval = 0;
|
|
|
|
}
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTTimer *timer = [[RCTTimer alloc] initWithCallbackID:callbackID
|
|
|
|
interval:interval
|
|
|
|
targetTime:targetTime
|
|
|
|
repeats:repeats];
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
_timers[callbackID] = timer;
|
2015-03-01 23:33:55 +00:00
|
|
|
[self startTimers];
|
2015-02-20 04:10:52 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)deleteTimer:(NSNumber *)timerID
|
|
|
|
{
|
|
|
|
RCT_EXPORT();
|
|
|
|
|
|
|
|
if (timerID) {
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
_timers[timerID] = nil;
|
2015-03-01 23:33:55 +00:00
|
|
|
if (_timers.count == 0) {
|
|
|
|
[self stopTimers];
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
RCTLogWarn(@"Called deleteTimer: with a nil timerID");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|