Permalink
Browse files

Flush queue every 5ms during JS execution

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 31f9a690f3b3524adf08aa9d8c01843e8524453e
@@ -24,7 +24,7 @@ @interface RCTBridge (Testing)
@property (nonatomic, strong, readonly) RCTBridge *batchedBridge;
- (void)_handleBuffer:(id)buffer;
- (void)handleBuffer:(id)buffer;
- (void)setUp;
@end
@@ -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
@@ -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");
}
@@ -63,6 +63,7 @@ @implementation RCTBatchedBridge
{
BOOL _loading;
BOOL _valid;
BOOL _wasBatchActive;
__weak id<RCTJavaScriptExecutor> _javaScriptExecutor;
NSMutableArray *_pendingCalls;
NSMutableArray *_moduleDataByID;
@@ -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 }];
@@ -613,7 +615,7 @@ - (void)enqueueApplicationScript:(NSData *)script
@"error": RCTNullIfNil(error),
});
[self _handleBuffer:json];
[self handleBuffer:json batchEnded:YES];
onComplete(error);
}];
@@ -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
@@ -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
@@ -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)]) {
@@ -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;
@@ -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"];

0 comments on commit 31f9a69

Please sign in to comment.