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"
|
2015-12-15 13:39:30 +00:00
|
|
|
#import "RCTBridge+Private.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
static const NSTimeInterval kMinimumSleepInterval = 1;
|
2016-07-15 20:57:19 +00:00
|
|
|
|
|
|
|
// These timing contants should be kept in sync with the ones in `JSTimersExecution.js`.
|
2016-07-14 05:44:19 +00:00
|
|
|
// The duration of a frame. This assumes that we want to run at 60 fps.
|
|
|
|
static const NSTimeInterval kFrameDuration = 1.0 / 60.0;
|
|
|
|
// The minimum time left in a frame to trigger the idle callback.
|
|
|
|
static const NSTimeInterval kIdleCallbackFrameDeadline = 0.001;
|
2016-07-11 15:25:43 +00:00
|
|
|
|
|
|
|
@interface _RCTTimer : NSObject
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
@implementation _RCTTimer
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
- (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
|
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
@interface _RCTTimingProxy : NSObject
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
// NSTimer retains its target, insert this class to break potential retain cycles
|
|
|
|
@implementation _RCTTimingProxy
|
|
|
|
{
|
|
|
|
__weak id _target;
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)proxyWithTarget:(id)target
|
|
|
|
{
|
|
|
|
_RCTTimingProxy *proxy = [self new];
|
|
|
|
if (proxy) {
|
|
|
|
proxy->_target = target;
|
|
|
|
}
|
|
|
|
return proxy;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)timerDidFire
|
|
|
|
{
|
|
|
|
[_target timerDidFire];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTTiming
|
|
|
|
{
|
2016-07-11 15:25:43 +00:00
|
|
|
NSMutableDictionary<NSNumber *, _RCTTimer *> *_timers;
|
|
|
|
NSTimer *_sleepTimer;
|
2016-07-14 05:44:19 +00:00
|
|
|
BOOL _sendIdleEvents;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
@synthesize bridge = _bridge;
|
2015-05-04 17:35:49 +00:00
|
|
|
@synthesize paused = _paused;
|
2015-09-29 12:26:01 +00:00
|
|
|
@synthesize pauseCallback = _pauseCallback;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2016-05-25 17:28:40 +00:00
|
|
|
- (void)setBridge:(RCTBridge *)bridge
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2016-05-25 17:28:40 +00:00
|
|
|
RCTAssert(!_bridge, @"Should never be initialized twice!");
|
|
|
|
|
|
|
|
_paused = YES;
|
|
|
|
_timers = [NSMutableDictionary new];
|
|
|
|
|
|
|
|
for (NSString *name in @[UIApplicationWillResignActiveNotification,
|
|
|
|
UIApplicationDidEnterBackgroundNotification,
|
|
|
|
UIApplicationWillTerminateNotification]) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(stopTimers)
|
|
|
|
name:name
|
|
|
|
object:nil];
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2016-05-25 17:28:40 +00:00
|
|
|
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
|
|
|
|
UIApplicationWillEnterForegroundNotification]) {
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(startTimers)
|
|
|
|
name:name
|
|
|
|
object:nil];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2016-05-25 17:28:40 +00:00
|
|
|
|
|
|
|
_bridge = bridge;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
2016-07-11 15:25:43 +00:00
|
|
|
[_sleepTimer invalidate];
|
2015-02-20 04:10:52 +00:00
|
|
|
[[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
|
|
|
|
{
|
2016-07-11 15:25:43 +00:00
|
|
|
if (!_paused) {
|
|
|
|
_paused = YES;
|
|
|
|
if (_pauseCallback) {
|
|
|
|
_pauseCallback();
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)startTimers
|
|
|
|
{
|
2016-07-14 05:44:19 +00:00
|
|
|
if (!_bridge || ![self hasPendingTimers]) {
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
if (_paused) {
|
|
|
|
_paused = NO;
|
2015-09-29 12:26:01 +00:00
|
|
|
if (_pauseCallback) {
|
|
|
|
_pauseCallback();
|
|
|
|
}
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-07-14 05:44:19 +00:00
|
|
|
- (BOOL)hasPendingTimers
|
|
|
|
{
|
|
|
|
return _sendIdleEvents || _timers.count > 0;
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:31:07 +00:00
|
|
|
- (void)didUpdateFrame:(RCTFrameUpdate *)update
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2016-07-11 15:25:43 +00:00
|
|
|
NSDate *nextScheduledTarget = [NSDate distantFuture];
|
2015-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<NSNumber *> *timersToCall = [NSMutableArray new];
|
2016-07-11 15:25:43 +00:00
|
|
|
for (_RCTTimer *timer in _timers.allValues) {
|
2015-02-20 04:10:52 +00:00
|
|
|
if ([timer updateFoundNeedsJSUpdate]) {
|
|
|
|
[timersToCall addObject:timer.callbackID];
|
|
|
|
}
|
|
|
|
if (!timer.target) {
|
2015-11-14 18:25:00 +00:00
|
|
|
[_timers removeObjectForKey:timer.callbackID];
|
2016-07-11 15:25:43 +00:00
|
|
|
} else {
|
|
|
|
nextScheduledTarget = [nextScheduledTarget earlierDate:timer.target];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
// Call timers that need to be called
|
2015-08-24 10:14:33 +00:00
|
|
|
if (timersToCall.count > 0) {
|
2016-08-02 18:06:19 +00:00
|
|
|
[_bridge enqueueJSCall:@"JSTimersExecution"
|
|
|
|
method:@"callTimers"
|
|
|
|
args:@[timersToCall]
|
|
|
|
completion:NULL];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-06-07 15:42:31 +00:00
|
|
|
|
2016-07-14 05:44:19 +00:00
|
|
|
if (_sendIdleEvents) {
|
|
|
|
NSTimeInterval frameElapsed = (CACurrentMediaTime() - update.timestamp);
|
|
|
|
if (kFrameDuration - frameElapsed >= kIdleCallbackFrameDeadline) {
|
|
|
|
NSTimeInterval currentTimestamp = [[NSDate date] timeIntervalSince1970];
|
|
|
|
NSNumber *absoluteFrameStartMS = @((currentTimestamp - frameElapsed) * 1000);
|
2016-08-02 18:06:19 +00:00
|
|
|
[_bridge enqueueJSCall:@"JSTimersExecution"
|
|
|
|
method:@"callIdleCallbacks"
|
|
|
|
args:@[absoluteFrameStartMS]
|
|
|
|
completion:NULL];
|
2016-07-14 05:44:19 +00:00
|
|
|
}
|
2016-07-11 15:25:43 +00:00
|
|
|
}
|
2016-07-14 05:44:19 +00:00
|
|
|
|
|
|
|
// Switch to a paused state only if we didn't call any timer this frame, so if
|
|
|
|
// in response to this timer another timer is scheduled, we don't pause and unpause
|
|
|
|
// the displaylink frivolously.
|
|
|
|
if (!_sendIdleEvents && timersToCall.count == 0) {
|
|
|
|
// No need to call the pauseCallback as RCTDisplayLink will ask us about our paused
|
|
|
|
// status immediately after completing this call
|
|
|
|
if (_timers.count == 0) {
|
|
|
|
_paused = YES;
|
|
|
|
}
|
|
|
|
// If the next timer is more than 1 second out, pause and schedule an NSTimer;
|
|
|
|
else if ([nextScheduledTarget timeIntervalSinceNow] > kMinimumSleepInterval) {
|
|
|
|
[self scheduleSleepTimer:nextScheduledTarget];
|
|
|
|
_paused = YES;
|
|
|
|
}
|
2016-07-11 15:25:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scheduleSleepTimer:(NSDate *)sleepTarget
|
|
|
|
{
|
|
|
|
if (!_sleepTimer || !_sleepTimer.valid) {
|
|
|
|
_sleepTimer = [[NSTimer alloc] initWithFireDate:sleepTarget
|
|
|
|
interval:0
|
|
|
|
target:[_RCTTimingProxy proxyWithTarget:self]
|
|
|
|
selector:@selector(timerDidFire)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:NO];
|
|
|
|
[[NSRunLoop currentRunLoop] addTimer:_sleepTimer forMode:NSDefaultRunLoopMode];
|
|
|
|
} else {
|
|
|
|
_sleepTimer.fireDate = [_sleepTimer.fireDate earlierDate:sleepTarget];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)timerDidFire
|
|
|
|
{
|
|
|
|
_sleepTimer = nil;
|
|
|
|
if (_paused) {
|
|
|
|
[self startTimers];
|
|
|
|
|
|
|
|
// Immediately dispatch frame, so we don't have to wait on the displaylink.
|
|
|
|
[self didUpdateFrame:nil];
|
2015-06-07 15:42:31 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-05 01:27:07 +00:00
|
|
|
NSTimeInterval jsSchedulingOverhead = MAX(-jsSchedulingTime.timeIntervalSinceNow, 0);
|
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
|
|
|
|
2016-07-11 15:25:43 +00:00
|
|
|
_RCTTimer *timer = [[_RCTTimer alloc] initWithCallbackID:callbackID
|
|
|
|
interval:jsDuration
|
|
|
|
targetTime:targetTime
|
|
|
|
repeats:repeats];
|
2015-04-18 17:43:20 +00:00
|
|
|
_timers[callbackID] = timer;
|
2016-07-11 15:25:43 +00:00
|
|
|
if (_paused) {
|
|
|
|
if ([timer.target timeIntervalSinceNow] > kMinimumSleepInterval) {
|
|
|
|
[self scheduleSleepTimer:timer.target];
|
|
|
|
} else {
|
|
|
|
[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-11-14 18:25:00 +00:00
|
|
|
[_timers removeObjectForKey:timerID];
|
2016-07-14 05:44:19 +00:00
|
|
|
if (![self hasPendingTimers]) {
|
|
|
|
[self stopTimers];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(setSendIdleEvents:(BOOL)sendIdleEvents)
|
|
|
|
{
|
|
|
|
_sendIdleEvents = sendIdleEvents;
|
|
|
|
if (sendIdleEvents) {
|
|
|
|
[self startTimers];
|
|
|
|
} else if (![self hasPendingTimers]) {
|
2015-07-31 13:55:47 +00:00
|
|
|
[self stopTimers];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|