Skip to content

Commit

Permalink
Flush queue every 5ms during JS execution
Browse files Browse the repository at this point in the history
Summary: public

Implement the iOS side of the optmisation previously implemented in android
(D2485402)

Depends on D2540746

Reviewed By: javache

Differential Revision: D2541118

fb-gh-sync-id: f3590600a6defa2da75c5b7b2cced6ad8bfea6cb
  • Loading branch information
tadeuzagallo authored and facebook-github-bot-2 committed Oct 19, 2015
1 parent 607527c commit 31f9a69
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Examples/UIExplorer/UIExplorerUnitTests/RCTBridgeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ @interface RCTBridge (Testing)

@property (nonatomic, strong, readonly) RCTBridge *batchedBridge;

- (void)_handleBuffer:(id)buffer;
- (void)handleBuffer:(id)buffer;
- (void)setUp;

@end
Expand Down Expand Up @@ -169,7 +169,7 @@ - (void)testCallNativeMethod
NSArray *args = @[@1234, @5678, @"stringy", @{@"a": @1}, @42];
NSArray *buffer = @[@[testModuleID], @[testMethodID], @[args], @[], @1234567];

[_bridge.batchedBridge _handleBuffer:buffer];
[_bridge.batchedBridge handleBuffer:buffer];

dispatch_sync(_methodQueue, ^{
// clear the queue
Expand All @@ -180,7 +180,7 @@ - (void)testCallNativeMethod
- (void)DISABLED_testBadArgumentsCount
{
//NSArray *bufferWithMissingArgument = @[@[@1], @[@0], @[@[@1234, @5678, @"stringy", @{@"a": @1}/*, @42*/]], @[], @1234567];
//[_bridge _handleBuffer:bufferWithMissingArgument];
//[_bridge handleBuffer:bufferWithMissingArgument];
NSLog(@"WARNING: testBadArgumentsCount is temporarily disabled until we have a better way to test cases that we expect to trigger redbox errors");
}

Expand Down
27 changes: 22 additions & 5 deletions React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ @implementation RCTBatchedBridge
{
BOOL _loading;
BOOL _valid;
BOOL _wasBatchActive;
__weak id<RCTJavaScriptExecutor> _javaScriptExecutor;
NSMutableArray *_pendingCalls;
NSMutableArray *_moduleDataByID;
Expand Down Expand Up @@ -386,6 +387,7 @@ - (void)executeSourceCode:(NSData *)sourceCode
// timing issues with RCTRootView
dispatch_async(dispatch_get_main_queue(), ^{
[self didFinishLoading];

[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidLoadNotification
object:_parentBridge
userInfo:@{ @"bridge": self }];
Expand Down Expand Up @@ -613,7 +615,7 @@ - (void)enqueueApplicationScript:(NSData *)script
@"error": RCTNullIfNil(error),
});

[self _handleBuffer:json];
[self handleBuffer:json batchEnded:YES];

onComplete(error);
}];
Expand Down Expand Up @@ -669,7 +671,7 @@ - (void)_actuallyInvokeAndProcessModule:(NSString *)module
return;
}
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDequeueNotification object:nil userInfo:nil];
[self _handleBuffer:json];
[self handleBuffer:json batchEnded:YES];
};

[_javaScriptExecutor executeJSCall:module
Expand All @@ -680,14 +682,26 @@ - (void)_actuallyInvokeAndProcessModule:(NSString *)module

#pragma mark - Payload Processing

- (void)_handleBuffer:(id)buffer
- (void)handleBuffer:(id)buffer batchEnded:(BOOL)batchEnded
{
RCTAssertJSThread();

if (buffer == nil || buffer == (id)kCFNull) {
return;
if (buffer != nil && buffer != (id)kCFNull) {
_wasBatchActive = YES;
[self handleBuffer:buffer];
}

if (batchEnded) {
if (_wasBatchActive) {
[self batchDidComplete];
}

_wasBatchActive = NO;
}
}

- (void)handleBuffer:(id)buffer
{
NSArray *requestsArray = [RCTConvert NSArray:buffer];

#if RCT_DEBUG
Expand Down Expand Up @@ -765,7 +779,10 @@ - (void)_handleBuffer:(id)buffer
dispatch_async(queue, block);
}
}
}

- (void)batchDidComplete
{
// TODO: batchDidComplete is only used by RCTUIManager - can we eliminate this special case?
for (RCTModuleData *moduleData in _moduleDataByID) {
if ([moduleData.instance respondsToSelector:@selector(batchDidComplete)]) {
Expand Down
17 changes: 17 additions & 0 deletions React/Executors/RCTContextExecutor.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ - (void)dealloc

@end

// Private bridge interface to allow middle-batch calls
@interface RCTBridge (RCTContextExecutor)

- (void)handleBuffer:(NSArray *)buffer batchEnded:(BOOL)hasEnded;

@end

@implementation RCTContextExecutor
{
RCTJavaScriptContext *_context;
Expand Down Expand Up @@ -336,6 +343,16 @@ - (void)setUp
}
[strongSelf _addNativeHook:RCTNativeLoggingHook withName:"nativeLoggingHook"];
[strongSelf _addNativeHook:RCTNoop withName:"noop"];

__weak RCTBridge *bridge = strongSelf->_bridge;
strongSelf->_context.context[@"nativeFlushQueueImmediate"] = ^(NSArray *calls){
if (!weakSelf.valid || !calls) {
return;
}

[bridge handleBuffer:calls batchEnded:NO];
};

#if RCT_DEV
[strongSelf _addNativeHook:RCTNativeTraceBeginSection withName:"nativeTraceBeginSection"];
[strongSelf _addNativeHook:RCTNativeTraceEndSection withName:"nativeTraceEndSection"];
Expand Down

0 comments on commit 31f9a69

Please sign in to comment.