diff --git a/.changes/sql-allow-blocking-without-nested-runtime.md b/.changes/sql-allow-blocking-without-nested-runtime.md new file mode 100644 index 00000000..ff2c773f --- /dev/null +++ b/.changes/sql-allow-blocking-without-nested-runtime.md @@ -0,0 +1,5 @@ +--- +"sql": "patch" +--- + +Allow blocking on async code without creating a nested runtime. diff --git a/plugins/sql/src/lib.rs b/plugins/sql/src/lib.rs index 93659098..adcb3a72 100644 --- a/plugins/sql/src/lib.rs +++ b/plugins/sql/src/lib.rs @@ -104,10 +104,9 @@ impl MigrationSource<'static> for MigrationList { } } -macro_rules! run_async_command { - ($cmd:expr) => { - tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on($cmd)) - }; +/// Allows blocking on async code without creating a nested runtime. +fn run_async_command(cmd: F) -> F::Output { + tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(cmd)) } /// Tauri SQL plugin builder. @@ -144,7 +143,7 @@ impl Builder { .setup(|app, api| { let config = api.config().clone().unwrap_or_default(); - run_async_command!(async move { + run_async_command(async move { let instances = DbInstances::default(); let mut lock = instances.0.write().await; @@ -172,7 +171,7 @@ impl Builder { }) .on_event(|app, event| { if let RunEvent::Exit = event { - run_async_command!(async move { + run_async_command(async move { let instances = &*app.state::(); let instances = instances.0.read().await; for value in instances.values() {