Skip to content

Commit

Permalink
fix(app): Handle manual disconnect for serial transport (#55)
Browse files Browse the repository at this point in the history
* Properly cancel the port read process when manually disconnected
  • Loading branch information
petejohanson authored Oct 9, 2024
1 parent 42f4966 commit 3da464f
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src-tauri/src/transport/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,11 @@ pub async fn serial_connect(

let (mut reader, mut writer) = tokio::io::split(port);

let ahc = app_handle.clone();
let (send, mut recv) = channel(5);
*state.conn.lock().await = Some(Box::new(send));
tauri::async_runtime::spawn(async move {
while let Some(data) = recv.next().await {
let _res = writer.write(&data).await;
}
});

tauri::async_runtime::spawn(async move {
let read_process = tauri::async_runtime::spawn(async move {
use tauri::Manager;
use tauri::Emitter;

Expand All @@ -51,6 +47,18 @@ pub async fn serial_connect(
app_handle.emit("connection_disconnected", ());
});

tauri::async_runtime::spawn(async move {
use tauri::Manager;

while let Some(data) = recv.next().await {
let _res = writer.write(&data).await;
}

let state = ahc.state::<super::commands::ActiveConnection>();
read_process.abort();
*state.conn.lock().await = None;
});

Ok(true)
}
Err(e) => {
Expand Down

0 comments on commit 3da464f

Please sign in to comment.