fix(hello-plugin): prévient le crash de l'asset protocol dans le runtime Tauri
- Ajoute block_on_protocol_future() pour éviter l'erreur 'Cannot start a runtime from within a runtime' - Ajoute test unitaire pour vérifier la compatibilité avec le runtime Tauri - Ajoute test d'intégration pour le hello-plugin du SDK
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
//! Plugin Tauri commands and asset protocol.
|
||||
|
||||
use std::future::Future;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use application::{ReviewPluginPackageInput, SetPluginEnabledInput, UninstallPluginInput};
|
||||
@ -178,7 +179,7 @@ fn plugin_asset_response(
|
||||
let rel =
|
||||
RelativePath::new(rel.to_owned()).map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))?;
|
||||
let state = app.state::<AppState>();
|
||||
let allowed = tauri::async_runtime::block_on(asset_allowed(
|
||||
let allowed = block_on_protocol_future(asset_allowed(
|
||||
&plugin_id,
|
||||
hash,
|
||||
&rel,
|
||||
@ -255,6 +256,13 @@ async fn asset_allowed(
|
||||
Ok(declared_main || declared_icon || rel.as_str().starts_with("assets/"))
|
||||
}
|
||||
|
||||
fn block_on_protocol_future<F: Future>(future: F) -> F::Output {
|
||||
match tokio::runtime::Handle::try_current() {
|
||||
Ok(handle) => tokio::task::block_in_place(|| handle.block_on(future)),
|
||||
Err(_) => tauri::async_runtime::block_on(future),
|
||||
}
|
||||
}
|
||||
|
||||
fn content_type(path: &Path) -> &'static str {
|
||||
match path
|
||||
.extension()
|
||||
@ -291,3 +299,15 @@ fn open_folder(path: &PathBuf) -> Result<(), ErrorDto> {
|
||||
.map(|_| ())
|
||||
.map_err(|e| ErrorDto::invalid(e.to_string()))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::block_on_protocol_future;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn protocol_future_can_be_waited_inside_tauri_runtime() {
|
||||
let value = block_on_protocol_future(async { 42 });
|
||||
|
||||
assert_eq!(value, 42);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user