diff --git a/ReactCommon/cxxreact/CxxModule.h b/ReactCommon/cxxreact/CxxModule.h index f253c108c2eea7..5a4d330e67f7f3 100644 --- a/ReactCommon/cxxreact/CxxModule.h +++ b/ReactCommon/cxxreact/CxxModule.h @@ -67,13 +67,14 @@ class CxxModule { std::string name; size_t callbacks; + bool isPromise; std::function func; std::function syncFunc; const char *getType() { assert(func || syncFunc); - return func ? (callbacks == 2 ? "promise" : "async") : "sync"; + return func ? (isPromise ? "promise" : "async") : "sync"; } // std::function/lambda ctors @@ -82,24 +83,36 @@ class CxxModule { std::function&& afunc) : name(std::move(aname)) , callbacks(0) + , isPromise(false) , func(std::bind(std::move(afunc))) {} Method(std::string aname, std::function&& afunc) : name(std::move(aname)) , callbacks(0) - , func(std::bind(std::move(afunc), _1)) {} + , isPromise(false) + , func(std::bind(std::move(afunc), std::placeholders::_1)) {} Method(std::string aname, std::function&& afunc) : name(std::move(aname)) , callbacks(1) - , func(std::bind(std::move(afunc), _1, _2)) {} + , isPromise(false) + , func(std::bind(std::move(afunc), std::placeholders::_1, std::placeholders::_2)) {} Method(std::string aname, std::function&& afunc) : name(std::move(aname)) , callbacks(2) + , isPromise(true) + , func(std::move(afunc)) {} + + Method(std::string aname, + std::function&& afunc, + AsyncTagType) + : name(std::move(aname)) + , callbacks(2) + , isPromise(false) , func(std::move(afunc)) {} // method pointer ctors @@ -108,25 +121,39 @@ class CxxModule { Method(std::string aname, T* t, void (T::*method)()) : name(std::move(aname)) , callbacks(0) + , isPromise(false) , func(std::bind(method, t)) {} template Method(std::string aname, T* t, void (T::*method)(folly::dynamic)) : name(std::move(aname)) , callbacks(0) - , func(std::bind(method, t, _1)) {} + , isPromise(false) + , func(std::bind(method, t, std::placeholders::_1)) {} template Method(std::string aname, T* t, void (T::*method)(folly::dynamic, Callback)) : name(std::move(aname)) , callbacks(1) - , func(std::bind(method, t, _1, _2)) {} + , isPromise(false) + , func(std::bind(method, t, std::placeholders::_1, std::placeholders::_2)) {} template Method(std::string aname, T* t, void (T::*method)(folly::dynamic, Callback, Callback)) : name(std::move(aname)) , callbacks(2) - , func(std::bind(method, t, _1, _2, _3)) {} + , isPromise(true) + , func(std::bind(method, t, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)) {} + + template + Method(std::string aname, + T* t, + void (T::*method)(folly::dynamic, Callback, Callback), + AsyncTagType) + : name(std::move(aname)) + , callbacks(2) + , isPromise(false) + , func(std::bind(method, t, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)) {} // sync std::function/lambda ctors @@ -139,6 +166,7 @@ class CxxModule { SyncTagType) : name(std::move(aname)) , callbacks(0) + , isPromise(false) , syncFunc([afunc=std::move(afunc)] (const folly::dynamic&) { return afunc(); }) {} @@ -148,6 +176,7 @@ class CxxModule { SyncTagType) : name(std::move(aname)) , callbacks(0) + , isPromise(false) , syncFunc(std::move(afunc)) {} }; diff --git a/ReactCommon/cxxreact/SampleCxxModule.cpp b/ReactCommon/cxxreact/SampleCxxModule.cpp index d0b3199f869c08..1862c0f08da4bc 100644 --- a/ReactCommon/cxxreact/SampleCxxModule.cpp +++ b/ReactCommon/cxxreact/SampleCxxModule.cpp @@ -114,6 +114,25 @@ auto SampleCxxModule::getMethods() -> std::vector { sample_->hello(); return nullptr; }, SyncTag), + Method("addIfPositiveAsPromise", [](dynamic args, Callback cb, Callback cbError) { + auto a = jsArgAsDouble(args, 0); + auto b = jsArgAsDouble(args, 1); + if (a < 0 || b < 0) { + cbError({"Negative number!"}); + } else { + cb({a + b}); + } + }), + Method("addIfPositiveAsAsync", [](dynamic args, Callback cb, Callback cbError) { + auto a = jsArgAsDouble(args, 0); + auto b = jsArgAsDouble(args, 1); + if (a < 0 || b < 0) { + cbError({"Negative number!"}); + } else { + cb({a + b}); + } + }, AsyncTag), + }; }