Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sql) Allow tauri-plugin-sql to work when Tauri is running async #2038

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/sql-allow-blocking-without-nested-runtime.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"sql": "patch"
---

Allow blocking on async code without creating a nested runtime.
13 changes: 11 additions & 2 deletions plugins/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,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.
#[derive(Default)]
pub struct Builder {
Expand Down Expand Up @@ -138,7 +147,7 @@ impl Builder {
.setup(|app, api| {
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 mut lock = instances.0.write().await;

Expand Down Expand Up @@ -166,7 +175,7 @@ impl Builder {
})
.on_event(|app, 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 = instances.0.read().await;
for value in instances.values() {
Expand Down
Loading