From faf0f89c2ac1b6fd25a07e1c161ef7d93809a903 Mon Sep 17 00:00:00 2001 From: James Hugman Date: Tue, 10 Sep 2024 17:51:46 +0100 Subject: [PATCH] Add UniffiCallInvoker::invokeNonBlocking --- cpp/includes/UniffiCallInvoker.h | 10 ++++++++++ .../gen_cpp/templates/CallbackFunction.cpp | 4 ++++ crates/ubrn_bindgen/src/bindings/react_native/mod.rs | 4 ++++ 3 files changed, 18 insertions(+) diff --git a/cpp/includes/UniffiCallInvoker.h b/cpp/includes/UniffiCallInvoker.h index 598305c5..67c3c0fa 100644 --- a/cpp/includes/UniffiCallInvoker.h +++ b/cpp/includes/UniffiCallInvoker.h @@ -76,5 +76,15 @@ class UniffiCallInvoker { future.wait(); } } + + /** + * Invokes the given function on the JS thread, by adding to + * the event queue. + */ + void invokeNonBlocking(jsi::Runtime &rt, UniffiCallFunc func) { + // react::CallFunc wrapper = [func](jsi::Runtime &rt) { + 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..385ad427 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 @@ -187,7 +187,11 @@ namespace {{ ns }} { }; // We'll then call that lambda from the callInvoker which will // look after calling it on the correct thread. + {% if callback.is_blocking() -%} callInvoker->invokeBlocking(rt, jsLambda); + {%- else %} + callInvoker->invokeNonBlocking(rt, jsLambda); + {%- endif %} }; return callback; } diff --git a/crates/ubrn_bindgen/src/bindings/react_native/mod.rs b/crates/ubrn_bindgen/src/bindings/react_native/mod.rs index cce42976..1f12d6e8 100644 --- a/crates/ubrn_bindgen/src/bindings/react_native/mod.rs +++ b/crates/ubrn_bindgen/src/bindings/react_native/mod.rs @@ -501,6 +501,10 @@ impl FfiCallbackFunction { .find(|a| a.is_return() && !a.type_().is_void()); arg.map(|a| a.type_()) } + + fn is_blocking(&self) -> bool { + self.name() != "RustFutureContinuationCallback" + } } fn is_future(nm: &str) -> bool {