Revert "[Bridge] Add support for JS async functions to RCT_EXPORT_METHOD"
This commit is contained in:
parent
e6c04df5a1
commit
1ed2542b46
|
@ -19,17 +19,9 @@ var slice = Array.prototype.slice;
|
|||
|
||||
var MethodTypes = keyMirror({
|
||||
remote: null,
|
||||
remoteAsync: null,
|
||||
local: null,
|
||||
});
|
||||
|
||||
type ErrorData = {
|
||||
message: string;
|
||||
domain: string;
|
||||
code: number;
|
||||
nativeStackIOS?: string;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates remotely invokable modules.
|
||||
*/
|
||||
|
@ -44,40 +36,21 @@ var BatchedBridgeFactory = {
|
|||
*/
|
||||
_createBridgedModule: function(messageQueue, moduleConfig, moduleName) {
|
||||
var remoteModule = mapObject(moduleConfig.methods, function(methodConfig, memberName) {
|
||||
switch (methodConfig.type) {
|
||||
case MethodTypes.remote:
|
||||
return function() {
|
||||
var lastArg = arguments.length > 0 ? arguments[arguments.length - 1] : null;
|
||||
var secondLastArg = arguments.length > 1 ? arguments[arguments.length - 2] : null;
|
||||
var hasErrorCB = typeof lastArg === 'function';
|
||||
var hasSuccCB = typeof secondLastArg === 'function';
|
||||
hasSuccCB && invariant(
|
||||
hasErrorCB,
|
||||
'Cannot have a non-function arg after a function arg.'
|
||||
);
|
||||
var numCBs = (hasSuccCB ? 1 : 0) + (hasErrorCB ? 1 : 0);
|
||||
var args = slice.call(arguments, 0, arguments.length - numCBs);
|
||||
var onSucc = hasSuccCB ? secondLastArg : null;
|
||||
var onFail = hasErrorCB ? lastArg : null;
|
||||
messageQueue.call(moduleName, memberName, args, onSucc, onFail);
|
||||
};
|
||||
|
||||
case MethodTypes.remoteAsync:
|
||||
return function(...args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
messageQueue.call(moduleName, memberName, args, resolve, (errorData) => {
|
||||
var error = _createErrorFromErrorData(errorData);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
case MethodTypes.local:
|
||||
return null;
|
||||
|
||||
default:
|
||||
throw new Error('Unknown bridge method type: ' + methodConfig.type);
|
||||
}
|
||||
return methodConfig.type === MethodTypes.local ? null : function() {
|
||||
var lastArg = arguments.length > 0 ? arguments[arguments.length - 1] : null;
|
||||
var secondLastArg = arguments.length > 1 ? arguments[arguments.length - 2] : null;
|
||||
var hasSuccCB = typeof lastArg === 'function';
|
||||
var hasErrorCB = typeof secondLastArg === 'function';
|
||||
hasErrorCB && invariant(
|
||||
hasSuccCB,
|
||||
'Cannot have a non-function arg after a function arg.'
|
||||
);
|
||||
var numCBs = (hasSuccCB ? 1 : 0) + (hasErrorCB ? 1 : 0);
|
||||
var args = slice.call(arguments, 0, arguments.length - numCBs);
|
||||
var onSucc = hasSuccCB ? lastArg : null;
|
||||
var onFail = hasErrorCB ? secondLastArg : null;
|
||||
return messageQueue.call(moduleName, memberName, args, onFail, onSucc);
|
||||
};
|
||||
});
|
||||
for (var constName in moduleConfig.constants) {
|
||||
warning(!remoteModule[constName], 'saw constant and method named %s', constName);
|
||||
|
@ -86,6 +59,7 @@ var BatchedBridgeFactory = {
|
|||
return remoteModule;
|
||||
},
|
||||
|
||||
|
||||
create: function(MessageQueue, modulesConfig, localModulesConfig) {
|
||||
var messageQueue = new MessageQueue(modulesConfig, localModulesConfig);
|
||||
return {
|
||||
|
@ -106,14 +80,4 @@ var BatchedBridgeFactory = {
|
|||
}
|
||||
};
|
||||
|
||||
function _createErrorFromErrorData(errorData: ErrorData): Error {
|
||||
var {
|
||||
message,
|
||||
...extraErrorInfo,
|
||||
} = errorData;
|
||||
var error = new Error(message);
|
||||
error.framesToPop = 1;
|
||||
return Object.assign(error, extraErrorInfo);
|
||||
}
|
||||
|
||||
module.exports = BatchedBridgeFactory;
|
||||
|
|
|
@ -431,14 +431,14 @@ var MessageQueueMixin = {
|
|||
},
|
||||
|
||||
/**
|
||||
* @param {Function} onSucc Function to store in current thread for later
|
||||
* lookup, when request succeeds.
|
||||
* @param {Function} onFail Function to store in current thread for later
|
||||
* lookup, when request fails.
|
||||
* @param {Function} onSucc Function to store in current thread for later
|
||||
* lookup, when request succeeds.
|
||||
* @param {Object?=} scope Scope to invoke `cb` with.
|
||||
* @param {Object?=} res Resulting callback ids. Use `this._POOLED_CBIDS`.
|
||||
*/
|
||||
_storeCallbacksInCurrentThread: function(onSucc, onFail, scope) {
|
||||
_storeCallbacksInCurrentThread: function(onFail, onSucc, scope) {
|
||||
invariant(onFail || onSucc, INTERNAL_ERROR);
|
||||
this._bookkeeping.allocateCallbackIDs(this._POOLED_CBIDS);
|
||||
var succCBID = this._POOLED_CBIDS.successCallbackID;
|
||||
|
@ -496,7 +496,7 @@ var MessageQueueMixin = {
|
|||
return ret;
|
||||
},
|
||||
|
||||
call: function(moduleName, methodName, params, onSucc, onFail, scope) {
|
||||
call: function(moduleName, methodName, params, onFail, onSucc, scope) {
|
||||
invariant(
|
||||
(!onFail || typeof onFail === 'function') &&
|
||||
(!onSucc || typeof onSucc === 'function'),
|
||||
|
@ -504,10 +504,10 @@ var MessageQueueMixin = {
|
|||
);
|
||||
// Store callback _before_ sending the request, just in case the MailBox
|
||||
// returns the response in a blocking manner.
|
||||
if (onSucc || onFail) {
|
||||
this._storeCallbacksInCurrentThread(onSucc, onFail, scope, this._POOLED_CBIDS);
|
||||
onSucc && params.push(this._POOLED_CBIDS.successCallbackID);
|
||||
if (onSucc) {
|
||||
this._storeCallbacksInCurrentThread(onFail, onSucc, scope, this._POOLED_CBIDS);
|
||||
onFail && params.push(this._POOLED_CBIDS.errorCallbackID);
|
||||
params.push(this._POOLED_CBIDS.successCallbackID);
|
||||
}
|
||||
var moduleID = this._remoteModuleNameToModuleID[moduleName];
|
||||
if (moduleID === undefined || moduleID === null) {
|
||||
|
|
|
@ -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 { \
|
||||
|
|
Loading…
Reference in New Issue