feat: add PluginHandle::run_mobile_plugin_async (#13895)

* add async

* chore: fmt

* feat: add run_mobile_plugin_async

* changes

* chore: fix misplaced `}`

* chore: fix minor pattern matching error

* fix: copy the response handling directly from run_mobile_plugin

* fix android build

* Fix clippy lint

* lint

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Akshanabha Chakraborty 2025-08-17 20:44:16 +05:30 committed by GitHub
parent 737364b8d3
commit 7c2eb31c83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": minor:feat
---
Introduces `PluginHandle::run_mobile_plugin_async` as an async alternative to `run_mobile_plugin`

View File

@ -13,6 +13,8 @@ use crate::{
#[cfg(mobile)]
use std::sync::atomic::{AtomicI32, Ordering};
#[cfg(mobile)]
use tokio::sync::oneshot;
use serde::{de::DeserializeOwned, Serialize};
@ -279,6 +281,38 @@ impl<R: Runtime, C: DeserializeOwned> PluginApi<R, C> {
}
impl<R: Runtime> PluginHandle<R> {
/// Executes the given mobile command.
/// This is an async optimized variant of run_mobile_plugin
pub async fn run_mobile_plugin_async<T: DeserializeOwned>(
&self,
command: impl AsRef<str>,
payload: impl Serialize,
) -> Result<T, PluginInvokeError> {
let (tx, rx) = oneshot::channel();
// the closure is an FnOnce but on Android we need to clone it (error handling)
let tx = std::sync::Arc::new(std::sync::Mutex::new(Some(tx)));
run_command(
self.name,
&self.handle,
command,
serde_json::to_value(payload).map_err(PluginInvokeError::CannotSerializePayload)?,
move |response| {
tx.lock().unwrap().take().unwrap().send(response).unwrap();
},
)?;
let response = rx.await.unwrap();
match response {
Ok(r) => serde_json::from_value(r).map_err(PluginInvokeError::CannotDeserializeResponse),
Err(r) => Err(
serde_json::from_value::<ErrorResponse>(r)
.map(Into::into)
.map_err(PluginInvokeError::CannotDeserializeResponse)?,
),
}
}
/// Executes the given mobile command.
pub fn run_mobile_plugin<T: DeserializeOwned>(
&self,