mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-03-27 22:54:46 +08:00
[Bridge] Add support for JS async functions to RCT_EXPORT_METHOD
Summary:
Adds support for JS async methods and helps guide people writing native modules w.r.t. the callbacks. With this diff, on the native side you write:
```objc
RCT_EXPORT_METHOD(getValueAsync:(NSString *)key
resolver:(RCTPromiseResolver)resolve
rejecter:(RCTPromiseRejecter)reject)
{
NSError *error = nil;
id value = [_nativeDataStore valueForKey:key error:&error];
// "resolve" and "reject" are automatically defined blocks that take
// any object (nil is OK) and an NSError, respectively
if (!error) {
resolve(value);
} else {
reject(error);
}
}
```
On the JS side, you can write:
```js
var {DemoDataStore} = require('react-native').NativeModules;
DemoDataStore.getValueAsync('sample-key').then((value) => {
console.log('Got:', value);
}, (error) => {
console.error(error);
// "error" is an Error object whose message is the NSError's description.
// The NSError's code and domain are also set, and the native trace i
Closes https://github.com/facebook/react-native/pull/1232
Github Author: James Ide <ide@jameside.com>
Test Plan: Imported from GitHub, without a `Test Plan:` line.
This commit is contained in:
@@ -17,6 +17,20 @@
|
||||
*/
|
||||
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
|
||||
@@ -37,7 +51,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;
|
||||
|
||||
@@ -70,6 +84,26 @@ 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)
|
||||
@@ -118,7 +152,7 @@ extern const dispatch_queue_t RCTJSThread;
|
||||
RCT_EXTERN_REMAP_MODULE(, objc_name, objc_supername)
|
||||
|
||||
/**
|
||||
* Similar to RCT_EXTERN_MODULE but allows setting a custom JavaScript name
|
||||
* Like 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 \
|
||||
@@ -136,7 +170,7 @@ extern const dispatch_queue_t RCTJSThread;
|
||||
RCT_EXTERN_REMAP_METHOD(, method)
|
||||
|
||||
/**
|
||||
* Similar to RCT_EXTERN_REMAP_METHOD but allows setting a custom JavaScript name
|
||||
* Like 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