Skip to content

Commit

Permalink
Try again with async non-blockers
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Salinas authored and Daniel Salinas committed Sep 12, 2024
1 parent 9bae622 commit de58bdd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
11 changes: 11 additions & 0 deletions cpp/includes/UniffiCallInvoker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> wrapper = [&func, &rt]() {
func(rt);
};
callInvoker_->invokeAsync(std::move(wrapper));
}
}
};
} // namespace uniffi_runtime
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,11 @@ namespace {{ ns }} {
makeCallbackFunction( // {{ ns }}
jsi::Runtime &rt,
std::shared_ptr<uniffi_runtime::UniffiCallInvoker> callInvoker,
const jsi::Value &value) {
const jsi::Value &value,
bool invokeAsync = false) {
auto callbackFunction = value.asObject(rt).asFunction(rt);
auto callbackValue = std::make_shared<jsi::Value>(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_{}") %}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ template <> struct Bridging<UniffiRustFutureContinuationCallback> {
static auto callback = {{ cb_type.borrow()|cpp_namespace(ci) }}::makeCallbackFunction(
rt,
callInvoker,
value
value,
true
);
return callback;
} catch (const std::logic_error &e) {
Expand Down

0 comments on commit de58bdd

Please sign in to comment.