mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-07 02:08:34 +08:00
Revert "[ReactNative] Add completionBlock to -[RCTBridge enqueueJSCall:args:]"
Test plan: broke catalyst apps Summary: This reverts commit 9fba6a360dc5f8bf. fbobjc/Tools/revert Reverter: jprado @build-break commit 9fba6a360dc5f8bf014f3d3c584c545b16da5100 Author: Tadeu Zagallo <tadeuzagallo@fb.com> AuthorDate: Thu May 28 08:29:19 2015 -0700 Commit: Service User <svcscm@fb.com> CommitDate: Thu May 28 09:53:53 2015 -0700 [ReactNative] Add completionBlock to -[RCTBridge enqueueJSCall:args:] Summary: @public Allow to pass an optional completion block to the bridge JS calls. The block will be called from the JS thread, after the javascript has finished running and the returned calls have been processed/dispatched to the native modules. Test Plan: Added `testCallbackIsCalledOnTheRightTime` to `RKBatchedBridgeTests`
This commit is contained in:
@@ -74,17 +74,6 @@ RCT_EXTERN NSString *RCTBridgeModuleNameForClass(Class bridgeModuleClass);
|
||||
*/
|
||||
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args;
|
||||
|
||||
/**
|
||||
* This is the same `enqueueJSCall:args:` but with a handler to notify that JS
|
||||
* has finished executing the call
|
||||
*
|
||||
* NOTE: The `completionHandler` will be called on the *JS* thread, so any expensive
|
||||
* calls should be avoided here
|
||||
*/
|
||||
- (void)enqueueJSCall:(NSString *)moduleDotMethod
|
||||
withArguments:(NSArray *)arguments
|
||||
completionBlock:(void (^)(void))completionBlock;
|
||||
|
||||
/**
|
||||
* This macro is used to register a JS method to be called via the enqueueJSCall
|
||||
* bridge method. You should place this macro inside any file that uses the
|
||||
|
||||
@@ -214,8 +214,7 @@ static NSArray *RCTBridgeModuleClassesByModuleID(void)
|
||||
- (void)_invokeAndProcessModule:(NSString *)module
|
||||
method:(NSString *)method
|
||||
arguments:(NSArray *)args
|
||||
context:(NSNumber *)context
|
||||
completionBlock:(void (^)(void))completionBlock;
|
||||
context:(NSNumber *)context;
|
||||
|
||||
@end
|
||||
|
||||
@@ -228,8 +227,7 @@ static NSArray *RCTBridgeModuleClassesByModuleID(void)
|
||||
- (void)_actuallyInvokeAndProcessModule:(NSString *)module
|
||||
method:(NSString *)method
|
||||
arguments:(NSArray *)args
|
||||
context:(NSNumber *)context
|
||||
completionBlock:(void (^)(void))completionBlock;
|
||||
context:(NSNumber *)context;
|
||||
|
||||
@end
|
||||
|
||||
@@ -348,8 +346,7 @@ static NSString *RCTStringUpToFirstArgument(NSString *methodName)
|
||||
[bridge _invokeAndProcessModule:@"BatchedBridge"
|
||||
method:@"invokeCallbackAndReturnFlushedQueue"
|
||||
arguments:@[json, args]
|
||||
context:context
|
||||
completionBlock:nil];
|
||||
context:context];
|
||||
} : ^(NSArray *unused) {});
|
||||
)
|
||||
};
|
||||
@@ -885,25 +882,10 @@ static id<RCTJavaScriptExecutor> _latestJSExecutor;
|
||||
|
||||
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args
|
||||
{
|
||||
[self.batchedBridge enqueueJSCall:moduleDotMethod
|
||||
withArguments:args
|
||||
completionBlock:nil];
|
||||
[self.batchedBridge enqueueJSCall:moduleDotMethod args:args];
|
||||
}
|
||||
|
||||
- (void)enqueueJSCall:(NSString *)moduleDotMethod
|
||||
withArguments:(NSArray *)arguments
|
||||
completionBlock:(void (^)(void))completionBlock
|
||||
{
|
||||
[self.batchedBridge enqueueJSCall:moduleDotMethod
|
||||
withArguments:arguments
|
||||
completionBlock:completionBlock];
|
||||
}
|
||||
|
||||
RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
method:(NSString *)method
|
||||
arguments:(NSArray *)args
|
||||
context:(NSNumber *)context
|
||||
completionBlock:(void (^)(void))completionBlock)
|
||||
RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module method:(NSString *)method arguments:(NSArray *)args context:(NSNumber *)context)
|
||||
|
||||
@end
|
||||
|
||||
@@ -1111,7 +1093,6 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
// Allow testing without a script
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
_loading = NO;
|
||||
[_jsDisplayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidLoadNotification
|
||||
object:_parentBridge
|
||||
userInfo:@{ @"bridge": self }];
|
||||
@@ -1253,18 +1234,6 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
* Public. Can be invoked from any thread.
|
||||
*/
|
||||
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args
|
||||
{
|
||||
[self enqueueJSCall:moduleDotMethod
|
||||
withArguments:args
|
||||
completionBlock:nil];
|
||||
}
|
||||
|
||||
/**
|
||||
* Public. Can be invoked from any thread.
|
||||
*/
|
||||
- (void)enqueueJSCall:(NSString *)moduleDotMethod
|
||||
withArguments:(NSArray *)arguments
|
||||
completionBlock:(void (^)(void))completionBlock
|
||||
{
|
||||
NSNumber *moduleID = RCTLocalModuleIDs[moduleDotMethod];
|
||||
RCTAssert(moduleID != nil, @"Module '%@' not registered.",
|
||||
@@ -1275,9 +1244,8 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
|
||||
[self _invokeAndProcessModule:@"BatchedBridge"
|
||||
method:@"callFunctionReturnFlushedQueue"
|
||||
arguments:@[moduleID ?: @0, methodID ?: @0, arguments ?: @[]]
|
||||
context:RCTGetExecutorID(_javaScriptExecutor)
|
||||
completionBlock:completionBlock];
|
||||
arguments:@[moduleID ?: @0, methodID ?: @0, args ?: @[]]
|
||||
context:RCTGetExecutorID(_javaScriptExecutor)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1299,8 +1267,7 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
[self _actuallyInvokeAndProcessModule:@"BatchedBridge"
|
||||
method:@"callFunctionReturnFlushedQueue"
|
||||
arguments:@[moduleID, methodID, @[@[timer]]]
|
||||
context:RCTGetExecutorID(_javaScriptExecutor)
|
||||
completionBlock:nil];
|
||||
context:RCTGetExecutorID(_javaScriptExecutor)];
|
||||
};
|
||||
|
||||
if ([_javaScriptExecutor respondsToSelector:@selector(executeAsyncBlockOnJavaScriptQueue:)]) {
|
||||
@@ -1366,11 +1333,7 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
* Called by enqueueJSCall from any thread, or from _immediatelyCallTimer,
|
||||
* on the JS thread, but only in non-batched mode.
|
||||
*/
|
||||
- (void)_invokeAndProcessModule:(NSString *)module
|
||||
method:(NSString *)method
|
||||
arguments:(NSArray *)args
|
||||
context:(NSNumber *)context
|
||||
completionBlock:(void (^)(void))completionBlock
|
||||
- (void)_invokeAndProcessModule:(NSString *)module method:(NSString *)method arguments:(NSArray *)args context:(NSNumber *)context
|
||||
{
|
||||
/**
|
||||
* AnyThread
|
||||
@@ -1386,13 +1349,10 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
}
|
||||
|
||||
id call = @{
|
||||
@"js_args": @{
|
||||
@"module": module,
|
||||
@"method": method,
|
||||
@"args": args,
|
||||
},
|
||||
@"module": module,
|
||||
@"method": method,
|
||||
@"args": args,
|
||||
@"context": context ?: @0,
|
||||
@"callback": (id)completionBlock ?: [NSNull null],
|
||||
};
|
||||
|
||||
if ([method isEqualToString:@"invokeCallbackAndReturnFlushedQueue"]) {
|
||||
@@ -1405,11 +1365,7 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)_actuallyInvokeAndProcessModule:(NSString *)module
|
||||
method:(NSString *)method
|
||||
arguments:(NSArray *)args
|
||||
context:(NSNumber *)context
|
||||
completionBlock:(void (^)(void))completionBlock
|
||||
- (void)_actuallyInvokeAndProcessModule:(NSString *)module method:(NSString *)method arguments:(NSArray *)args context:(NSNumber *)context
|
||||
{
|
||||
RCTAssertJSThread();
|
||||
|
||||
@@ -1421,10 +1377,6 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
}
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTDequeueNotification object:nil userInfo:nil];
|
||||
[self _handleBuffer:json context:context];
|
||||
|
||||
if (completionBlock) {
|
||||
completionBlock();
|
||||
}
|
||||
};
|
||||
|
||||
[_javaScriptExecutor executeJSCall:module
|
||||
@@ -1593,23 +1545,12 @@ RCT_INNER_BRIDGE_ONLY(_invokeAndProcessModule:(NSString *)module
|
||||
return [call[@"context"] isEqualToNumber:currentExecutorID];
|
||||
}]];
|
||||
if (calls.count > 0) {
|
||||
void (^completionBlock)(void) = ^{
|
||||
for (NSDictionary *call in calls) {
|
||||
id callback = call[@"callback"];
|
||||
|
||||
if (callback && callback != [NSNull null]) {
|
||||
((void (^)(void))callback)();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
_scheduledCalls = [[NSMutableArray alloc] init];
|
||||
_scheduledCallbacks = [[RCTSparseArray alloc] init];
|
||||
[self _actuallyInvokeAndProcessModule:@"BatchedBridge"
|
||||
method:@"processBatch"
|
||||
arguments:@[[calls valueForKey:@"js_args"]]
|
||||
context:RCTGetExecutorID(_javaScriptExecutor)
|
||||
completionBlock:completionBlock];
|
||||
arguments:@[calls]
|
||||
context:RCTGetExecutorID(_javaScriptExecutor)];
|
||||
}
|
||||
|
||||
RCTProfileEndEvent(@"DispatchFrameUpdate", @"objc_call", nil);
|
||||
|
||||
@@ -55,12 +55,3 @@ RCT_EXTERN BOOL RCTRunningInTestEnvironment(void);
|
||||
|
||||
// Return YES if image has an alpha component
|
||||
RCT_EXTERN BOOL RCTImageHasAlpha(CGImageRef image);
|
||||
|
||||
/**
|
||||
* Helper for async tests, run the runloop while the condition becomes true or
|
||||
* until timeout
|
||||
*/
|
||||
#define RCTRunLoopRunWhile(condition, timeout) \
|
||||
_RCTRunLoopRunWhile(^BOOL{ return condition; }, timeout)
|
||||
|
||||
RCT_EXTERN BOOL _RCTRunLoopRunWhile(BOOL (^)(void), NSTimeInterval);
|
||||
|
||||
@@ -273,14 +273,3 @@ BOOL RCTImageHasAlpha(CGImageRef image)
|
||||
return YES;
|
||||
}
|
||||
}
|
||||
|
||||
BOOL _RCTRunLoopRunWhile(BOOL (^block)(void), NSTimeInterval timeout)
|
||||
{
|
||||
NSDate *timeoutDate = [[NSDate date] dateByAddingTimeInterval:timeout];
|
||||
|
||||
while (block() && [timeoutDate timeIntervalSinceNow] > 0) {
|
||||
[[NSRunLoop mainRunLoop] runMode:NSDefaultRunLoopMode beforeDate:timeoutDate];
|
||||
}
|
||||
|
||||
return !block();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user