|
| 1 | +// |
| 2 | +// RCTDisplayLink.m |
| 3 | +// React |
| 4 | +// |
| 5 | +// Created by Stanislav Vishnevskiy on 6/8/15. |
| 6 | +// Copyright (c) 2015 Facebook. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "DCDDisplayLink.h" |
| 10 | +#import <UIKit/UIKit.h> |
| 11 | + |
| 12 | +@interface DCDDisplayLink () |
| 13 | + |
| 14 | +@property(nonatomic) NSRunLoop *runloop; |
| 15 | +@property(nonatomic) NSString *mode; |
| 16 | +@property(nonatomic) id target; |
| 17 | +@property(nonatomic) SEL selector; |
| 18 | +@property(nonatomic) NSTimer *timer; |
| 19 | +@property(nonatomic) CADisplayLink *displayLink; |
| 20 | + |
| 21 | +// CADisplayLink is not thread safe. |
| 22 | +// Add a flag to avoid the crash of removing invalidated CADisplayLink from the run loop. |
| 23 | +@property(nonatomic) BOOL resourcesLoaded; |
| 24 | + |
| 25 | +@end |
| 26 | + |
| 27 | +@implementation DCDDisplayLink |
| 28 | + |
| 29 | ++ (DCDDisplayLink *)displayLinkWithTarget:(id)target selector:(SEL)sel { |
| 30 | + return [[self alloc] initWithTarget:target selector:sel]; |
| 31 | +} |
| 32 | + |
| 33 | +- (instancetype)initWithTarget:(id)target selector:(SEL)sel { |
| 34 | + if (self = [super init]) { |
| 35 | + _target = target; |
| 36 | + _selector = sel; |
| 37 | + _displayLink = [CADisplayLink displayLinkWithTarget:target selector:sel]; |
| 38 | + _resourcesLoaded = YES; |
| 39 | + |
| 40 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 41 | + selector:@selector(switchToTimer) |
| 42 | + name:UIApplicationDidEnterBackgroundNotification |
| 43 | + object:nil]; |
| 44 | + [[NSNotificationCenter defaultCenter] addObserver:self |
| 45 | + selector:@selector(switchToDisplayLink) |
| 46 | + name:UIApplicationWillEnterForegroundNotification |
| 47 | + object:nil]; |
| 48 | + } |
| 49 | + return self; |
| 50 | +} |
| 51 | + |
| 52 | +- (void)dealloc { |
| 53 | + [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 54 | +} |
| 55 | + |
| 56 | +- (void)switchToDisplayLink { |
| 57 | + if (_timer) { |
| 58 | + [_timer invalidate]; |
| 59 | + _timer = nil; |
| 60 | + [self setPaused:_paused]; |
| 61 | + if (_runloop) { |
| 62 | + [_displayLink addToRunLoop:_runloop forMode:_mode]; |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +- (void)switchToTimer { |
| 68 | + if (!_timer) { |
| 69 | + [self maybeResetTimer]; |
| 70 | + [self setPaused:_paused]; |
| 71 | + if (_runloop && _resourcesLoaded) { |
| 72 | + [_displayLink removeFromRunLoop:_runloop forMode:_mode]; |
| 73 | + [_runloop addTimer:_timer forMode:_mode]; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +- (void)addToRunLoop:(NSRunLoop *)runloop forMode:(NSString *)mode { |
| 79 | + _runloop = runloop; |
| 80 | + _mode = mode; |
| 81 | + if (_timer) { |
| 82 | + [self maybeResetTimer]; |
| 83 | + [runloop addTimer:_timer forMode:mode]; |
| 84 | + } |
| 85 | + else { |
| 86 | + [_displayLink addToRunLoop:runloop forMode:mode]; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +- (void)removeFromRunLoop:(NSRunLoop *)runloop forMode:(NSString *)mode { |
| 91 | + _runloop = nil; |
| 92 | + _mode = nil; |
| 93 | + if (_timer) { |
| 94 | + [_timer invalidate]; |
| 95 | + } |
| 96 | + else { |
| 97 | + [_displayLink removeFromRunLoop:runloop forMode:mode]; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +- (void)invalidate { |
| 102 | + _resourcesLoaded = NO; |
| 103 | + if (_timer) { |
| 104 | + [_timer invalidate]; |
| 105 | + } |
| 106 | + else { |
| 107 | + [_displayLink invalidate]; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +- (void)setPaused:(BOOL)paused { |
| 112 | + _paused = paused; |
| 113 | + if (_timer) { |
| 114 | + if (paused) { |
| 115 | + [_timer invalidate]; |
| 116 | + } |
| 117 | + else { |
| 118 | + [self maybeResetTimer]; |
| 119 | + if (_runloop) { |
| 120 | + [_runloop addTimer:_timer forMode:_mode]; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + else { |
| 125 | + _displayLink.paused = paused; |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +- (CFTimeInterval)timestamp { |
| 130 | + if (_timer) { |
| 131 | + // TODO: Does React Native actually need this? |
| 132 | + return 0; |
| 133 | + } |
| 134 | + return _displayLink.timestamp; |
| 135 | +} |
| 136 | + |
| 137 | +- (CFTimeInterval)duration { |
| 138 | + if (_timer) { |
| 139 | + // TODO: Does React Native actually need this? |
| 140 | + return 0; |
| 141 | + } |
| 142 | + return _displayLink.duration; |
| 143 | +} |
| 144 | + |
| 145 | +- (void)maybeResetTimer { |
| 146 | + if (!_timer || ![_timer isValid]) { |
| 147 | + _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerLoop) userInfo:nil repeats:YES]; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +- (void)timerLoop { |
| 152 | + if (_target) { |
| 153 | + IMP imp = [_target methodForSelector:_selector]; |
| 154 | + void (*func)(id, SEL, DCDDisplayLink *) = (void *)imp; |
| 155 | + func(_target, _selector, self); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +@end |
0 commit comments