Skip to content

Commit

Permalink
Close the postgres connection only when clicking X on main window
Browse files Browse the repository at this point in the history
  • Loading branch information
tachyonicbytes committed Jan 25, 2024
1 parent 5a8ac68 commit 31a8712
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
36 changes: 21 additions & 15 deletions examples/tauri-postgres/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,27 @@ fn main() {
}
});

// Close postgres only when closing the main window
let main_window = app.get_window("main").unwrap();
main_window.on_window_event({
let main_window = main_window.clone();
move |event| match event {
// When we click X, stop postgres gracefully first
// BUG: this closes the connection regardless of which window is closed
// so don't close the postgres terminal
WindowEvent::Destroyed => {
let db_connection: State<DbConnection> = main_window.state();
block_on(async {
let mut db = db_connection.db.lock().await;
if let Some(mut connection) = db.take() {
connection.stop_db().await.unwrap();
}
})
}
_ => {}
}
});

Ok(())
})
.invoke_handler(tauri::generate_handler![
Expand All @@ -626,21 +647,6 @@ fn main() {
stop_chat,
open_postgres,
])
.on_window_event(move |event| match event.event() {
// When we click X, stop postgres gracefully first
// BUG: this closes the connection regardless of which window is closed
// so don't close the postgres terminal
WindowEvent::Destroyed => {
let db_connection: State<DbConnection> = event.window().state();
block_on(async {
let mut db = db_connection.db.lock().await;
if let Some(mut connection) = db.take() {
connection.stop_db().await.unwrap();
}
})
}
_ => {}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Expand Down
1 change: 1 addition & 0 deletions examples/tauri-postgres/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"height": 600,
"resizable": true,
"title": "Linearlite",
"label": "main",
"width": 800
}
]
Expand Down

0 comments on commit 31a8712

Please sign in to comment.