Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CxxModules to implement methods with two callbacks #21586

Closed
wants to merge 5 commits into from

Conversation

acoates-ms
Copy link
Contributor

When writing a native module in C++ using CxxModule, its not currently possible to write async methods which take two callbacks, that shouldn't be projected to JS as a promise. I hit this when trying to implement the AppState module in c++. AppState has a single method on it, getCurrentAppState, which takes two callbacks (a success and a failure callback).

This change adds a couple of new CxxModule::Method ctors, which allow devs to specify that they want two callbacks, but not a promise. This is done using an extra tag in the ctor, similar to how you specify that you want to make a synchronous method. I used the existing AsyncTag to indicate that the 2 callback version should be async, and not a promise.

Test Plan:

Implemented the AppState module using a CxxModule for an out of tree platform. Added sample usage to the Sample CxxModule.

Release Notes:

Help reviewers and the release process by writing your own release notes. See below for an example.

[MINOR] [ENHANCEMENT] [CxxModule.h] - Allow CxxModules to implement functions which take two callbacks

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@matthargett
Copy link
Contributor

Looks pretty good to me, some questions:

  • is the AppState module you reference using platform agnostic C++? If so, why not contribute that in this PR as well?
  • getType is computed on each call, but is a Native Module even able to change this dynamically? You didn’t change this, but I wonder if this can be a constant calculated in the ctor and assigned to a field, in preparation for it being constexpr later.

@acoates-ms
Copy link
Contributor Author

AppState itself is pretty platform specific. And the one I'm working on isn't one of the base RN platforms.
getType is only called in getMethods which should only be called once when the native module is preparing the config to send to JS. So its probably not worth the optimization?

@acoates-ms acoates-ms changed the title Acoates/async2 Allow CxxModules to implement methods with two callbacks Oct 17, 2018
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 23, 2018
@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hramos has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@@ -114,6 +114,25 @@ auto SampleCxxModule::getMethods() -> std::vector<Method> {
sample_->hello();
return nullptr;
}, SyncTag),
Method("addIfPositiveAsPromise", [this](dynamic args, Callback cb, Callback cbError) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the capture of this is actually not used, failing the build internally:

react-native-github/ReactCommon/cxxreact/SampleCxxModule.cpp:117:39: error: lambda capture 'this' is not used [-Werror,-Wunused-lambda-capture]
    Method("addIfPositiveAsPromise", [this](dynamic args, Callback cb, Callback cbError) {
                                      ^
react-native-github/ReactCommon/cxxreact/SampleCxxModule.cpp:126:37: error: lambda capture 'this' is not used [-Werror,-Wunused-lambda-capture]
    Method("addIfPositiveAsAsync", [this](dynamic args, Callback cb, Callback cbError) {
                                    ^

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I can remove that.

Copy link
Contributor

@facebook-github-bot facebook-github-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fkgozali has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@react-native-bot
Copy link
Collaborator

REDMOND\acoates merged commit 8826d8b into facebook:master.

@facebook facebook locked as resolved and limited conversation to collaborators Nov 14, 2018
@react-native-bot react-native-bot added the Merged This PR has been merged. label Nov 14, 2018
t-nanava pushed a commit to microsoft/react-native-macos that referenced this pull request Jun 17, 2019
)

Summary:
When writing a native module in C++ using CxxModule, its not currently possible to write async methods which take two callbacks, that shouldn't be projected to JS as a promise.  I hit this when trying to implement the AppState module in c++.  AppState has a single method on it, getCurrentAppState, which takes two callbacks (a success and a failure callback).

This change adds a couple of new CxxModule::Method ctors, which allow devs to specify that they want two callbacks, but not a promise.  This is done using an extra tag in the ctor, similar to how you specify that you want to make a synchronous method.  I used the existing AsyncTag to indicate that the 2 callback version should be async, and not a promise.
Pull Request resolved: facebook#21586

Reviewed By: shergin

Differential Revision: D10520204

Pulled By: fkgozali

fbshipit-source-id: bcb2dbd91cba3c1db987dc18960247218fdbc032
@acoates-ms acoates-ms deleted the acoates/async2 branch March 9, 2020 17:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants