From de58bddef7835e5c93112dc003c61866edf45380 Mon Sep 17 00:00:00 2001 From: Daniel Salinas Date: Thu, 12 Sep 2024 15:45:35 -0400 Subject: [PATCH] Try again with async non-blockers --- cpp/includes/UniffiCallInvoker.h | 11 +++++++++++ .../gen_cpp/templates/CallbackFunction.cpp | 11 ++++++++--- .../react_native/gen_cpp/templates/Future.cpp | 3 ++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/cpp/includes/UniffiCallInvoker.h b/cpp/includes/UniffiCallInvoker.h index 90ddd424..8e27c2ba 100644 --- a/cpp/includes/UniffiCallInvoker.h +++ b/cpp/includes/UniffiCallInvoker.h @@ -80,5 +80,16 @@ class UniffiCallInvoker { cv.wait(lock, [&done] { return done; }); } } + + void invokeAsync(jsi::Runtime &rt, UniffiCallFunc &func) { + if (std::this_thread::get_id() == threadId_) { + func(rt); + } else { + std::function wrapper = [&func, &rt]() { + func(rt); + }; + callInvoker_->invokeAsync(std::move(wrapper)); + } + } }; } // namespace uniffi_runtime diff --git a/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/CallbackFunction.cpp b/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/CallbackFunction.cpp index 20aadbd2..156ff367 100644 --- a/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/CallbackFunction.cpp +++ b/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/CallbackFunction.cpp @@ -148,10 +148,11 @@ namespace {{ ns }} { makeCallbackFunction( // {{ ns }} jsi::Runtime &rt, std::shared_ptr callInvoker, - const jsi::Value &value) { + const jsi::Value &value, + bool invokeAsync = false) { auto callbackFunction = value.asObject(rt).asFunction(rt); auto callbackValue = std::make_shared(rt, callbackFunction); - rsLambda = [&rt, callInvoker, callbackValue]( + rsLambda = [&rt, callInvoker, callbackValue, invokeAsync]( {%- for arg in callback.arguments() %} {%- let arg_t = arg.type_().borrow()|ffi_type_name %} {%- let arg_nm_rs = arg.name()|var_name|fmt("rs_{}") %} @@ -187,7 +188,11 @@ namespace {{ ns }} { }; // We'll then call that lambda from the callInvoker which will // look after calling it on the correct thread. - callInvoker->invokeBlocking(rt, jsLambda); + if (invokeAsync) { + callInvoker->invokeAsync(rt, jsLambda); + } else { + callInvoker->invokeBlocking(rt, jsLambda); + } }; return callback; } diff --git a/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/Future.cpp b/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/Future.cpp index a7de0135..5980f643 100644 --- a/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/Future.cpp +++ b/crates/ubrn_bindgen/src/bindings/react_native/gen_cpp/templates/Future.cpp @@ -14,7 +14,8 @@ template <> struct Bridging { static auto callback = {{ cb_type.borrow()|cpp_namespace(ci) }}::makeCallbackFunction( rt, callInvoker, - value + value, + true ); return callback; } catch (const std::logic_error &e) {