fix(tui): do not freeze the UI on chat send
Some checks failed
ci / test (pull_request) Failing after 1s
Some checks failed
ci / test (pull_request) Failing after 1s
Enter used block_on(Node::send), which retries for 180s on the TUI thread so event::read never ran. Spawn send on the runtime, poll keys at 100ms, show sending/sent/send failed on the footer.
This commit is contained in:
parent
af7d69ace8
commit
7d8363a761
3 changed files with 75 additions and 7 deletions
49
src/tui.rs
49
src/tui.rs
|
|
@ -1,5 +1,6 @@
|
|||
use std::io;
|
||||
use std::sync::Arc;
|
||||
use std::sync::{mpsc, Arc};
|
||||
use std::time::Duration;
|
||||
|
||||
use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
|
||||
use ratatui::layout::{Constraint, Layout, Rect};
|
||||
|
|
@ -19,6 +20,18 @@ const C_STALE: Color = Color::Rgb(210, 180, 80);
|
|||
const C_HL_BG: Color = Color::Rgb(48, 42, 28);
|
||||
const C_BORDER: Color = Color::Rgb(72, 84, 64);
|
||||
|
||||
/// How long the TUI waits for a key before redrawing (so background send can update status).
|
||||
pub const TUI_POLL: Duration = Duration::from_millis(100);
|
||||
|
||||
/// Non-blocking read of a background send/job status. Never waits on Tor.
|
||||
pub fn take_job_result(rx: &mpsc::Receiver<String>) -> Option<String> {
|
||||
match rx.try_recv() {
|
||||
Ok(s) => Some(s),
|
||||
Err(mpsc::TryRecvError::Empty) => None,
|
||||
Err(mpsc::TryRecvError::Disconnected) => Some("send failed".into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn fingerprint_mismatch_banner() -> &'static str {
|
||||
"fingerprint mismatch"
|
||||
}
|
||||
|
|
@ -608,6 +621,7 @@ struct App {
|
|||
help_open: bool,
|
||||
who_open: bool,
|
||||
chat_scroll: u16,
|
||||
job_rx: Option<mpsc::Receiver<String>>,
|
||||
}
|
||||
|
||||
enum Screen {
|
||||
|
|
@ -666,12 +680,17 @@ impl App {
|
|||
help_open: false,
|
||||
who_open: false,
|
||||
chat_scroll: 0,
|
||||
job_rx: None,
|
||||
})
|
||||
}
|
||||
|
||||
fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<AppExit, String> {
|
||||
loop {
|
||||
self.drain_job();
|
||||
terminal.draw(|f| self.draw(f)).map_err(|e| e.to_string())?;
|
||||
if !event::poll(TUI_POLL).map_err(io_err)? {
|
||||
continue;
|
||||
}
|
||||
let Event::Key(key) = event::read().map_err(io_err)? else {
|
||||
continue;
|
||||
};
|
||||
|
|
@ -1234,14 +1253,34 @@ impl App {
|
|||
self.status_note = Some("no friend selected".into());
|
||||
return Ok(());
|
||||
};
|
||||
let pk = friend.pubkey.clone();
|
||||
match self.rt.block_on(self.node.send(&pk, text.as_bytes())) {
|
||||
Ok(()) => self.status_note = Some("sent".into()),
|
||||
Err(e) => self.status_note = Some(format!("send failed: {e}")),
|
||||
if self.job_rx.is_some() {
|
||||
self.status_note = Some("already sending".into());
|
||||
return Ok(());
|
||||
}
|
||||
let pk = friend.pubkey.clone();
|
||||
let body = text.as_bytes().to_vec();
|
||||
let node = Arc::clone(&self.node);
|
||||
let (tx, rx) = mpsc::channel();
|
||||
self.status_note = Some("sending".into());
|
||||
std::mem::drop(self.rt.spawn(async move {
|
||||
let note = match node.send(&pk, &body).await {
|
||||
Ok(()) => "sent".to_string(),
|
||||
Err(e) => format!("send failed: {e}"),
|
||||
};
|
||||
let _ = tx.send(note);
|
||||
}));
|
||||
self.job_rx = Some(rx);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drain_job(&mut self) {
|
||||
let Some(note) = self.job_rx.as_ref().and_then(take_job_result) else {
|
||||
return;
|
||||
};
|
||||
self.status_note = Some(note);
|
||||
self.job_rx = None;
|
||||
}
|
||||
|
||||
fn who_body(&self) -> String {
|
||||
let Some(friend) = self.friends.get(self.selected) else {
|
||||
return "no friend selected\n\nEsc closes".into();
|
||||
|
|
|
|||
Loading…
Reference in a new issue