mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-26 21:41:48 +02:00
fix(sql) Allow tauri-plugin-sql to work when Tauri is running async (#2038)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"sql": "patch"
|
||||||
|
---
|
||||||
|
|
||||||
|
Allow blocking on async code without creating a nested runtime.
|
||||||
+11
-2
@@ -102,6 +102,15 @@ impl MigrationSource<'static> for MigrationList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Allows blocking on async code without creating a nested runtime.
|
||||||
|
fn run_async_command<F: std::future::Future>(cmd: F) -> F::Output {
|
||||||
|
if tokio::runtime::Handle::try_current().is_ok() {
|
||||||
|
tokio::task::block_in_place(|| tokio::runtime::Handle::current().block_on(cmd))
|
||||||
|
} else {
|
||||||
|
tauri::async_runtime::block_on(cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Tauri SQL plugin builder.
|
/// Tauri SQL plugin builder.
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Builder {
|
pub struct Builder {
|
||||||
@@ -136,7 +145,7 @@ impl Builder {
|
|||||||
.setup(|app, api| {
|
.setup(|app, api| {
|
||||||
let config = api.config().clone().unwrap_or_default();
|
let config = api.config().clone().unwrap_or_default();
|
||||||
|
|
||||||
tauri::async_runtime::block_on(async move {
|
run_async_command(async move {
|
||||||
let instances = DbInstances::default();
|
let instances = DbInstances::default();
|
||||||
let mut lock = instances.0.write().await;
|
let mut lock = instances.0.write().await;
|
||||||
|
|
||||||
@@ -164,7 +173,7 @@ impl Builder {
|
|||||||
})
|
})
|
||||||
.on_event(|app, event| {
|
.on_event(|app, event| {
|
||||||
if let RunEvent::Exit = event {
|
if let RunEvent::Exit = event {
|
||||||
tauri::async_runtime::block_on(async move {
|
run_async_command(async move {
|
||||||
let instances = &*app.state::<DbInstances>();
|
let instances = &*app.state::<DbInstances>();
|
||||||
let instances = instances.0.read().await;
|
let instances = instances.0.read().await;
|
||||||
for value in instances.values() {
|
for value in instances.values() {
|
||||||
|
|||||||
Reference in New Issue
Block a user