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"
|
|
|
|
|
|
|
|
@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
|
|
|
|
{
|
2015-11-14 18:25:00 +00:00
|
|
|
NSMutableDictionary<NSNumber *, RCTTimer *> *_timers;
|
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()
|
|
|
|
|
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-11-14 18:25:00 +00:00
|
|
|
_timers = [NSMutableDictionary 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-09-29 12:26:01 +00:00
|
|
|
self.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-09-29 12:26:01 +00:00
|
|
|
self.paused = NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setPaused:(BOOL)paused
|
|
|
|
{
|
|
|
|
if (_paused != paused) {
|
|
|
|
_paused = paused;
|
|
|
|
if (_pauseCallback) {
|
|
|
|
_pauseCallback();
|
|
|
|
}
|
|
|
|
}
|
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-11-03 22:45:46 +00:00
|
|
|
NSMutableArray<NSNumber *> *timersToCall = [NSMutableArray new];
|
2015-11-14 18:25:00 +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];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-11-14 18:25:00 +00:00
|
|
|
[_timers removeObjectForKey:timerID];
|
2015-07-31 13:55:47 +00:00
|
|
|
if (_timers.count == 0) {
|
|
|
|
[self stopTimers];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|