Skip to content

Commit 90ef77c

Browse files
fix(sql) Allow tauri-plugin-sql to work when Tauri is running async (tauri-apps#2038)
1 parent 51856e9 commit 90ef77c

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sql": "patch"
3+
---
4+
5+
Allow blocking on async code without creating a nested runtime.

plugins/sql/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ impl MigrationSource<'static> for MigrationList {
102102
}
103103
}
104104

105+
/// Allows blocking on async code without creating a nested runtime.
106+
fn run_async_command<F: std::future::Future>(cmd: F) -> F::Output {
107+
if tokio::runtime::Handle::try_current().is_ok() {
108+
tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(cmd))
109+
} else {
110+
tauri::async_runtime::block_on(cmd)
111+
}
112+
}
113+
105114
/// Tauri SQL plugin builder.
106115
#[derive(Default)]
107116
pub struct Builder {
@@ -136,7 +145,7 @@ impl Builder {
136145
.setup(|app, api| {
137146
let config = api.config().clone().unwrap_or_default();
138147

139-
tauri::async_runtime::block_on(async move {
148+
run_async_command(async move {
140149
let instances = DbInstances::default();
141150
let mut lock = instances.0.write().await;
142151

@@ -164,7 +173,7 @@ impl Builder {
164173
})
165174
.on_event(|app, event| {
166175
if let RunEvent::Exit = event {
167-
tauri::async_runtime::block_on(async move {
176+
run_async_command(async move {
168177
let instances = &*app.state::<DbInstances>();
169178
let instances = instances.0.read().await;
170179
for value in instances.values() {

0 commit comments

Comments
 (0)