-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
Differential Revision: D2595414 fb-gh-sync-id: 3b44ce1737bdd1e0861a285a45976631a57ab3b5
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
package com.facebook.react.bridge; | ||
|
||
/** | ||
* Interface that represents a JavaScript Promise which can be passed to the native module as a | ||
* method parameter. | ||
* | ||
* Methods annotated with {@link ReactMethod} that use {@link Promise} as type of the last parameter | ||
* will be marked as "remoteAsync" and will return a promise when invoked from JavaScript. | ||
*/ | ||
public interface Promise { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
davidaurelio
Author
Contributor
|
||
void resolve(Object value); | ||
void reject(String reason); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
/** | ||
* Implementation of two javascript functions that can be used to resolve or reject a js promise. | ||
*/ | ||
package com.facebook.react.bridge; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
public class PromiseImpl implements Promise { | ||
private @Nullable Callback mResolve; | ||
private @Nullable Callback mReject; | ||
|
||
public PromiseImpl(@Nullable Callback resolve, @Nullable Callback reject) { | ||
mResolve = resolve; | ||
mReject = reject; | ||
} | ||
|
||
@Override | ||
public void resolve(Object value) { | ||
if (mResolve != null) { | ||
mResolve.invoke(value); | ||
} | ||
} | ||
|
||
@Override | ||
public void reject(String reason) { | ||
if (mReject != null) { | ||
// The JavaScript side expects a map with at least the error message. | ||
// It is possible to expose all kind of information. It will be available on the JS | ||
// error instance. | ||
// TODO(8850038): add more useful information, e.g. the native stack trace. | ||
WritableNativeMap errorInfo = new WritableNativeMap(); | ||
errorInfo.putString("message", reason); | ||
mReject.invoke(errorInfo); | ||
} | ||
} | ||
} |
9 comments
on commit b86a6e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Context from @davidaurelio:
iOS has support for “async” bridged methods since May (see #1232). Async methods return a Promise object in JavaScript rather than accepting callback functions.
The implementation adds a new interface (Promise) and exports methods that use that as type for the last parameter as async functions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @ide it'll make you happy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woohoo!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidaurelio, wow this is really awesome! I have a couple of suggestions that I'll leave in inline comments. This is exciting to announce for 0.15-rc!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any docs on how to use it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@davidaurelio Cool. I'm on master though, so would love a hint :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just accept a Promise
as last parameter in your bridged method (instead of the usual Callback
):
import com.facebook.react.bridge.Promise
// …
@ReactMethod
void bridgedMethod(int parameter, Promise promise) { /* … */ }
that’s it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An object with
resolve
andreject
methods is typically called a "Deferred" object so you might want to rename this class. (The public API of standard Promises isthen
/catch
but notresolve
/reject
.)https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred