mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-24 04:16:00 +08:00
Allow RCTDisplayLink to pause more often
Summary: By default we run the the JS display link, even if there are no modules listening. Given that most listeners will be lazily constructed, let's make it paused by default. Since RCTTiming almost never unpauses due to some long-lived timers, implement a sleep timer that pauses the displaylink but uses an NSTimer to wake up in time. Reviewed By: mhorowitz Differential Revision: D3235044 fbshipit-source-id: 4a340fea552ada1bd8bc0d83b596a7df6f992387
This commit is contained in:
committed by
Facebook Github Bot 4
parent
4840233bcd
commit
7b718b03eb
@@ -18,6 +18,10 @@
|
||||
#import "RCTModuleData.h"
|
||||
#import "RCTProfile.h"
|
||||
|
||||
#define RCTAssertRunLoop() \
|
||||
RCTAssert(_runLoop == [NSRunLoop currentRunLoop], \
|
||||
@"This method must be called on the CADisplayLink run loop")
|
||||
|
||||
@implementation RCTDisplayLink
|
||||
{
|
||||
CADisplayLink *_jsDisplayLink;
|
||||
@@ -38,12 +42,13 @@
|
||||
- (void)registerModuleForFrameUpdates:(id<RCTBridgeModule>)module
|
||||
withModuleData:(RCTModuleData *)moduleData
|
||||
{
|
||||
if ([_frameUpdateObservers containsObject:moduleData] ||
|
||||
![moduleData.moduleClass conformsToProtocol:@protocol(RCTFrameUpdateObserver)]) {
|
||||
if (![moduleData.moduleClass conformsToProtocol:@protocol(RCTFrameUpdateObserver)] ||
|
||||
[_frameUpdateObservers containsObject:moduleData]) {
|
||||
return;
|
||||
}
|
||||
|
||||
[_frameUpdateObservers addObject:moduleData];
|
||||
|
||||
// Don't access the module instance via moduleData, as this will cause deadlock
|
||||
id<RCTFrameUpdateObserver> observer = (id<RCTFrameUpdateObserver>)module;
|
||||
__weak typeof(self) weakSelf = self;
|
||||
@@ -54,16 +59,28 @@
|
||||
}
|
||||
|
||||
CFRunLoopRef cfRunLoop = [strongSelf->_runLoop getCFRunLoop];
|
||||
|
||||
if (!self->_runLoop) {
|
||||
if (!cfRunLoop) {
|
||||
return;
|
||||
}
|
||||
|
||||
CFRunLoopPerformBlock(cfRunLoop, kCFRunLoopDefaultMode, ^{
|
||||
[weakSelf updateJSDisplayLinkState];
|
||||
});
|
||||
CFRunLoopWakeUp(cfRunLoop);
|
||||
if ([NSRunLoop currentRunLoop] == strongSelf->_runLoop) {
|
||||
[weakSelf updateJSDisplayLinkState];
|
||||
} else {
|
||||
CFRunLoopPerformBlock(cfRunLoop, kCFRunLoopDefaultMode, ^{
|
||||
[weakSelf updateJSDisplayLinkState];
|
||||
});
|
||||
CFRunLoopWakeUp(cfRunLoop);
|
||||
}
|
||||
};
|
||||
|
||||
// Assuming we're paused right now, we only need to update the display link's state
|
||||
// when the new observer is not paused. If it not paused, the observer will immediately
|
||||
// start receiving updates anyway.
|
||||
if (![observer isPaused] && _runLoop) {
|
||||
CFRunLoopPerformBlock([_runLoop getCFRunLoop], kCFRunLoopDefaultMode, ^{
|
||||
[self updateJSDisplayLinkState];
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
- (void)addToRunLoop:(NSRunLoop *)runLoop
|
||||
@@ -77,12 +94,6 @@
|
||||
[_jsDisplayLink invalidate];
|
||||
}
|
||||
|
||||
- (void)assertOnRunLoop
|
||||
{
|
||||
RCTAssert(_runLoop == [NSRunLoop currentRunLoop],
|
||||
@"This method must be called on the CADisplayLink run loop");
|
||||
}
|
||||
|
||||
- (void)dispatchBlock:(dispatch_block_t)block
|
||||
queue:(dispatch_queue_t)queue
|
||||
{
|
||||
@@ -95,7 +106,7 @@
|
||||
|
||||
- (void)_jsThreadUpdate:(CADisplayLink *)displayLink
|
||||
{
|
||||
[self assertOnRunLoop];
|
||||
RCTAssertRunLoop();
|
||||
|
||||
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways, @"-[RCTDisplayLink _jsThreadUpdate:]", nil);
|
||||
|
||||
@@ -121,7 +132,7 @@
|
||||
|
||||
- (void)updateJSDisplayLinkState
|
||||
{
|
||||
[self assertOnRunLoop];
|
||||
RCTAssertRunLoop();
|
||||
|
||||
BOOL pauseDisplayLink = YES;
|
||||
for (RCTModuleData *moduleData in _frameUpdateObservers) {
|
||||
@@ -131,6 +142,7 @@
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_jsDisplayLink.paused = pauseDisplayLink;
|
||||
}
|
||||
|
||||
|
||||
@@ -426,7 +426,7 @@ SEL RCTParseMethodSignature(NSString *methodSignature, NSArray<RCTMethodArgument
|
||||
|
||||
- (NSDictionary *)profileArgs
|
||||
{
|
||||
if (_profileArgs) {
|
||||
if (!_profileArgs) {
|
||||
// This sets _selector
|
||||
[self processMethodSignature];
|
||||
_profileArgs = @{
|
||||
|
||||
Reference in New Issue
Block a user