mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-28 12:15:37 +08:00
Revert "[Bridge] Add support for JS async functions to RCT_EXPORT_METHOD"
This commit is contained in:
@@ -47,11 +47,6 @@ typedef NS_ENUM(NSUInteger, RCTBridgeFields) {
|
||||
RCTBridgeFieldFlushDateMillis
|
||||
};
|
||||
|
||||
typedef NS_ENUM(NSUInteger, RCTJavaScriptFunctionKind) {
|
||||
RCTJavaScriptFunctionKindNormal,
|
||||
RCTJavaScriptFunctionKindAsync,
|
||||
};
|
||||
|
||||
#ifdef __LP64__
|
||||
typedef uint64_t RCTHeaderValue;
|
||||
typedef struct section_64 RCTHeaderSection;
|
||||
@@ -209,27 +204,6 @@ static NSArray *RCTBridgeModuleClassesByModuleID(void)
|
||||
return RCTModuleClassesByID;
|
||||
}
|
||||
|
||||
// TODO: Can we just replace RCTMakeError with this function instead?
|
||||
static NSDictionary *RCTJSErrorFromNSError(NSError *error)
|
||||
{
|
||||
NSString *errorMessage;
|
||||
NSArray *stackTrace = [NSThread callStackSymbols];
|
||||
NSMutableDictionary *errorInfo =
|
||||
[NSMutableDictionary dictionaryWithObject:stackTrace forKey:@"nativeStackIOS"];
|
||||
|
||||
if (error) {
|
||||
errorMessage = error.localizedDescription ?: @"Unknown error from a native module";
|
||||
errorInfo[@"domain"] = error.domain ?: RCTErrorDomain;
|
||||
errorInfo[@"code"] = @(error.code);
|
||||
} else {
|
||||
errorMessage = @"Unknown error from a native module";
|
||||
errorInfo[@"domain"] = RCTErrorDomain;
|
||||
errorInfo[@"code"] = @-1;
|
||||
}
|
||||
|
||||
return RCTMakeError(errorMessage, nil, errorInfo);
|
||||
}
|
||||
|
||||
@class RCTBatchedBridge;
|
||||
|
||||
@interface RCTBridge ()
|
||||
@@ -265,7 +239,6 @@ static NSDictionary *RCTJSErrorFromNSError(NSError *error)
|
||||
@property (nonatomic, copy, readonly) NSString *moduleClassName;
|
||||
@property (nonatomic, copy, readonly) NSString *JSMethodName;
|
||||
@property (nonatomic, assign, readonly) SEL selector;
|
||||
@property (nonatomic, assign, readonly) RCTJavaScriptFunctionKind functionKind;
|
||||
|
||||
@end
|
||||
|
||||
@@ -447,50 +420,6 @@ static NSString *RCTStringUpToFirstArgument(NSString *methodName)
|
||||
} else if ([argumentName isEqualToString:@"RCTResponseSenderBlock"]) {
|
||||
addBlockArgument();
|
||||
useFallback = NO;
|
||||
} else if ([argumentName isEqualToString:@"RCTPromiseResolveBlock"]) {
|
||||
RCTAssert(i == numberOfArguments - 2,
|
||||
@"The RCTPromiseResolveBlock must be the second to last parameter in -[%@ %@]",
|
||||
_moduleClassName, objCMethodName);
|
||||
RCT_ARG_BLOCK(
|
||||
if (RCT_DEBUG && ![json isKindOfClass:[NSNumber class]]) {
|
||||
RCTLogError(@"Argument %tu (%@) of %@.%@ must be a promise resolver ID", index,
|
||||
json, RCTBridgeModuleNameForClass(_moduleClass), _JSMethodName);
|
||||
return;
|
||||
}
|
||||
|
||||
// Marked as autoreleasing, because NSInvocation doesn't retain arguments
|
||||
__autoreleasing RCTPromiseResolveBlock value = (^(id result) {
|
||||
NSArray *arguments = result ? @[result] : @[];
|
||||
[bridge _invokeAndProcessModule:@"BatchedBridge"
|
||||
method:@"invokeCallbackAndReturnFlushedQueue"
|
||||
arguments:@[json, arguments]
|
||||
context:context];
|
||||
});
|
||||
)
|
||||
useFallback = NO;
|
||||
_functionKind = RCTJavaScriptFunctionKindAsync;
|
||||
} else if ([argumentName isEqualToString:@"RCTPromiseRejectBlock"]) {
|
||||
RCTAssert(i == numberOfArguments - 1,
|
||||
@"The RCTPromiseRejectBlock must be the last parameter in -[%@ %@]",
|
||||
_moduleClassName, objCMethodName);
|
||||
RCT_ARG_BLOCK(
|
||||
if (RCT_DEBUG && ![json isKindOfClass:[NSNumber class]]) {
|
||||
RCTLogError(@"Argument %tu (%@) of %@.%@ must be a promise rejecter ID", index,
|
||||
json, RCTBridgeModuleNameForClass(_moduleClass), _JSMethodName);
|
||||
return;
|
||||
}
|
||||
|
||||
// Marked as autoreleasing, because NSInvocation doesn't retain arguments
|
||||
__autoreleasing RCTPromiseRejectBlock value = (^(NSError *error) {
|
||||
NSDictionary *errorJSON = RCTJSErrorFromNSError(error);
|
||||
[bridge _invokeAndProcessModule:@"BatchedBridge"
|
||||
method:@"invokeCallbackAndReturnFlushedQueue"
|
||||
arguments:@[json, @[errorJSON]]
|
||||
context:context];
|
||||
});
|
||||
)
|
||||
useFallback = NO;
|
||||
_functionKind = RCTJavaScriptFunctionKindAsync;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -569,18 +498,9 @@ static NSString *RCTStringUpToFirstArgument(NSString *methodName)
|
||||
|
||||
// Safety check
|
||||
if (arguments.count != _argumentBlocks.count) {
|
||||
NSInteger actualCount = arguments.count;
|
||||
NSInteger expectedCount = _argumentBlocks.count;
|
||||
|
||||
// Subtract the implicit Promise resolver and rejecter functions for implementations of async functions
|
||||
if (_functionKind == RCTJavaScriptFunctionKindAsync) {
|
||||
actualCount -= 2;
|
||||
expectedCount -= 2;
|
||||
}
|
||||
|
||||
RCTLogError(@"%@.%@ was called with %zd arguments, but expects %zd",
|
||||
RCTBridgeModuleNameForClass(_moduleClass), _JSMethodName,
|
||||
actualCount, expectedCount);
|
||||
arguments.count, _argumentBlocks.count);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -605,8 +525,7 @@ static NSString *RCTStringUpToFirstArgument(NSString *methodName)
|
||||
|
||||
- (NSString *)description
|
||||
{
|
||||
return [NSString stringWithFormat:@"<%@: %p; exports %@ as %@;>",
|
||||
NSStringFromClass(self.class), self, _methodName, _JSMethodName];
|
||||
return [NSString stringWithFormat:@"<%@: %p; exports %@ as %@;>", NSStringFromClass(self.class), self, _methodName, _JSMethodName];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -687,7 +606,7 @@ static RCTSparseArray *RCTExportedMethodsByModuleID(void)
|
||||
* },
|
||||
* "methodName2": {
|
||||
* "methodID": 1,
|
||||
* "type": "remoteAsync"
|
||||
* "type": "remote"
|
||||
* },
|
||||
* etc...
|
||||
* },
|
||||
@@ -711,7 +630,7 @@ static NSDictionary *RCTRemoteModulesConfig(NSDictionary *modulesByName)
|
||||
[methods enumerateObjectsUsingBlock:^(RCTModuleMethod *method, NSUInteger methodID, BOOL *_stop) {
|
||||
methodsByName[method.JSMethodName] = @{
|
||||
@"methodID": @(methodID),
|
||||
@"type": method.functionKind == RCTJavaScriptFunctionKindAsync ? @"remoteAsync" : @"remote",
|
||||
@"type": @"remote",
|
||||
};
|
||||
}];
|
||||
|
||||
|
||||
@@ -17,20 +17,6 @@
|
||||
*/
|
||||
typedef void (^RCTResponseSenderBlock)(NSArray *response);
|
||||
|
||||
/**
|
||||
* Block that bridge modules use to resolve the JS promise waiting for a result.
|
||||
* Nil results are supported and are converted to JS's undefined value.
|
||||
*/
|
||||
typedef void (^RCTPromiseResolveBlock)(id result);
|
||||
|
||||
/**
|
||||
* Block that bridge modules use to reject the JS promise waiting for a result.
|
||||
* The error may be nil but it is preferable to pass an NSError object for more
|
||||
* precise error messages.
|
||||
*/
|
||||
typedef void (^RCTPromiseRejectBlock)(NSError *error);
|
||||
|
||||
|
||||
/**
|
||||
* This constant can be returned from +methodQueue to force module
|
||||
* methods to be called on the JavaScript thread. This can have serious
|
||||
@@ -51,7 +37,7 @@ extern const dispatch_queue_t RCTJSThread;
|
||||
* A reference to the RCTBridge. Useful for modules that require access
|
||||
* to bridge features, such as sending events or making JS calls. This
|
||||
* will be set automatically by the bridge when it initializes the module.
|
||||
* To implement this in your module, just add @synthesize bridge = _bridge;
|
||||
* To implement this in your module, just add @synthesize bridge = _bridge;
|
||||
*/
|
||||
@property (nonatomic, weak) RCTBridge *bridge;
|
||||
|
||||
@@ -84,26 +70,6 @@ extern const dispatch_queue_t RCTJSThread;
|
||||
* { ... }
|
||||
*
|
||||
* and is exposed to JavaScript as `NativeModules.ModuleName.doSomething`.
|
||||
*
|
||||
* ## Promises
|
||||
*
|
||||
* Bridge modules can also define methods that are exported to JavaScript as
|
||||
* methods that return a Promise, and are compatible with JS async functions.
|
||||
*
|
||||
* Declare the last two parameters of your native method to be a resolver block
|
||||
* and a rejecter block. The resolver block must precede the rejecter block.
|
||||
*
|
||||
* For example:
|
||||
*
|
||||
* RCT_EXPORT_METHOD(doSomethingAsync:(NSString *)aString
|
||||
* resolver:(RCTPromiseResolveBlock)resolve
|
||||
* rejecter:(RCTPromiseRejectBlock)reject
|
||||
* { ... }
|
||||
*
|
||||
* Calling `NativeModules.ModuleName.doSomethingAsync(aString)` from
|
||||
* JavaScript will return a promise that is resolved or rejected when your
|
||||
* native method implementation calls the respective block.
|
||||
*
|
||||
*/
|
||||
#define RCT_EXPORT_METHOD(method) \
|
||||
RCT_REMAP_METHOD(, method)
|
||||
@@ -152,7 +118,7 @@ extern const dispatch_queue_t RCTJSThread;
|
||||
RCT_EXTERN_REMAP_MODULE(, objc_name, objc_supername)
|
||||
|
||||
/**
|
||||
* Like RCT_EXTERN_MODULE, but allows setting a custom JavaScript name.
|
||||
* Similar to RCT_EXTERN_MODULE but allows setting a custom JavaScript name
|
||||
*/
|
||||
#define RCT_EXTERN_REMAP_MODULE(js_name, objc_name, objc_supername) \
|
||||
objc_name : objc_supername \
|
||||
@@ -170,7 +136,7 @@ extern const dispatch_queue_t RCTJSThread;
|
||||
RCT_EXTERN_REMAP_METHOD(, method)
|
||||
|
||||
/**
|
||||
* Like RCT_EXTERN_REMAP_METHOD, but allows setting a custom JavaScript name.
|
||||
* Similar to RCT_EXTERN_REMAP_METHOD but allows setting a custom JavaScript name
|
||||
*/
|
||||
#define RCT_EXTERN_REMAP_METHOD(js_name, method) \
|
||||
- (void)__rct_export__##method { \
|
||||
|
||||
Reference in New Issue
Block a user