Skip to main content

event_loop

Function event_loop 

Source
pub async fn event_loop<B, M>(
    terminal: &mut Terminal<B>,
    model: M,
    rx: UnboundedReceiver<ReplEvent>,
    apply: impl FnMut(&mut M, ReplEvent),
    render: impl FnMut(&mut Frame<'_>, &M),
) -> Result<M>
where B: Backend, B::Error: Error + Send + Sync + 'static, M: TuiModel,
Expand description

The terminal-generic tick/event select loop.

Why: split out from run specifically so it can be exercised in tests against ratatui::backend::TestBackend — run itself requires a real TTY (via TerminalGuard::enter) and so cannot run in CI/sandboxes. What: draws once immediately, then loops a biased tokio::select! between the TICK interval (redraw only) and rx.recv(). A received event is applied, and then every event ALREADY queued behind it is applied too (try_recv, which never waits) before the single redraw that follows — see [drain_ready] for why (#8240). Exits when model.should_quit() becomes true or rx closes (all senders dropped — mirrors crates/trusty-agents/src/repl/tui/run.rs::event_loop’s None => return Ok(())). Returns the final model so callers/tests can inspect it. Test: tests::event_loop_applies_events_and_redraws, tests::event_loop_stops_when_model_requests_quit, tests::event_loop_stops_when_channel_closes, tests::event_loop_redraws_on_tick_even_without_events, tests::event_loop_coalesces_a_burst_into_one_redraw.