From 1785b6a393e47a432e87d6a415c2bf4d6c0eeaa4 Mon Sep 17 00:00:00 2001 From: "REDMOND\\acoates" Date: Mon, 8 Oct 2018 21:18:14 -0700 Subject: [PATCH 1/5] Allow CxxModules to implement functions which take two callbacks --- ReactCommon/cxxreact/CxxModule.h | 42 +++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/ReactCommon/cxxreact/CxxModule.h b/ReactCommon/cxxreact/CxxModule.h index f253c108c2eea7..9a8ba870498ed0 100644 --- a/ReactCommon/cxxreact/CxxModule.h +++ b/ReactCommon/cxxreact/CxxModule.h @@ -71,9 +71,11 @@ class CxxModule { std::function syncFunc; + bool isPromise; + 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 +84,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 +122,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 +167,7 @@ class CxxModule { SyncTagType) : name(std::move(aname)) , callbacks(0) + , isPromise(false) , syncFunc([afunc=std::move(afunc)] (const folly::dynamic&) { return afunc(); }) {} @@ -148,6 +177,7 @@ class CxxModule { SyncTagType) : name(std::move(aname)) , callbacks(0) + , isPromise(false) , syncFunc(std::move(afunc)) {} }; From db95ceb6e9bd00f974fef9070797ac18df11345a Mon Sep 17 00:00:00 2001 From: "REDMOND\\acoates" Date: Mon, 8 Oct 2018 21:34:09 -0700 Subject: [PATCH 2/5] Add sample implementation of promise and async function to sample --- ReactCommon/cxxreact/SampleCxxModule.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ReactCommon/cxxreact/SampleCxxModule.cpp b/ReactCommon/cxxreact/SampleCxxModule.cpp index d0b3199f869c08..8f67dd6ccad627 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", [this](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", [this](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), + }; } From c0137061635869e887fe9f8053338b83a1ea18d1 Mon Sep 17 00:00:00 2001 From: "REDMOND\\acoates" Date: Mon, 8 Oct 2018 22:35:55 -0700 Subject: [PATCH 3/5] reorder members to match initialization order --- ReactCommon/cxxreact/CxxModule.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ReactCommon/cxxreact/CxxModule.h b/ReactCommon/cxxreact/CxxModule.h index 9a8ba870498ed0..5a4d330e67f7f3 100644 --- a/ReactCommon/cxxreact/CxxModule.h +++ b/ReactCommon/cxxreact/CxxModule.h @@ -67,12 +67,11 @@ class CxxModule { std::string name; size_t callbacks; + bool isPromise; std::function func; std::function syncFunc; - bool isPromise; - const char *getType() { assert(func || syncFunc); return func ? (isPromise ? "promise" : "async") : "sync"; From f55033ae3479e902d2a0b2e8e796acd944b68184 Mon Sep 17 00:00:00 2001 From: "REDMOND\\acoates" Date: Tue, 9 Oct 2018 10:12:59 -0700 Subject: [PATCH 4/5] Build fix --- ReactCommon/cxxreact/SampleCxxModule.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ReactCommon/cxxreact/SampleCxxModule.cpp b/ReactCommon/cxxreact/SampleCxxModule.cpp index 8f67dd6ccad627..4bc51622fe867c 100644 --- a/ReactCommon/cxxreact/SampleCxxModule.cpp +++ b/ReactCommon/cxxreact/SampleCxxModule.cpp @@ -118,18 +118,18 @@ auto SampleCxxModule::getMethods() -> std::vector { auto a = jsArgAsDouble(args, 0); auto b = jsArgAsDouble(args, 1); if (a < 0 || b < 0) { - cbError("Negative number!"); + cbError({"Negative number!"}); } else { - cb(a + b); + cb({a + b}); } }), Method("addIfPositiveAsAsync", [this](dynamic args, Callback cb, Callback cbError) { auto a = jsArgAsDouble(args, 0); auto b = jsArgAsDouble(args, 1); if (a < 0 || b < 0) { - cbError("Negative number!"); + cbError({"Negative number!"}); } else { - cb(a + b); + cb({a + b}); } }, AsyncTag), From 6fe9c097844db91e42ef605bed6a0ab2d95085d4 Mon Sep 17 00:00:00 2001 From: "REDMOND\\acoates" Date: Tue, 13 Nov 2018 15:02:01 -0800 Subject: [PATCH 5/5] Remove unused this --- ReactCommon/cxxreact/SampleCxxModule.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ReactCommon/cxxreact/SampleCxxModule.cpp b/ReactCommon/cxxreact/SampleCxxModule.cpp index 4bc51622fe867c..1862c0f08da4bc 100644 --- a/ReactCommon/cxxreact/SampleCxxModule.cpp +++ b/ReactCommon/cxxreact/SampleCxxModule.cpp @@ -114,7 +114,7 @@ auto SampleCxxModule::getMethods() -> std::vector { sample_->hello(); return nullptr; }, SyncTag), - Method("addIfPositiveAsPromise", [this](dynamic args, Callback cb, Callback cbError) { + Method("addIfPositiveAsPromise", [](dynamic args, Callback cb, Callback cbError) { auto a = jsArgAsDouble(args, 0); auto b = jsArgAsDouble(args, 1); if (a < 0 || b < 0) { @@ -123,7 +123,7 @@ auto SampleCxxModule::getMethods() -> std::vector { cb({a + b}); } }), - Method("addIfPositiveAsAsync", [this](dynamic args, Callback cb, Callback cbError) { + Method("addIfPositiveAsAsync", [](dynamic args, Callback cb, Callback cbError) { auto a = jsArgAsDouble(args, 0); auto b = jsArgAsDouble(args, 1); if (a < 0 || b < 0) {