Skip to content

Commit

Permalink
chore(tui): move ticks to dedicated task
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-olszewski committed Oct 17, 2024
1 parent d83484a commit 76122e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
18 changes: 3 additions & 15 deletions crates/turborepo-ui/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tokio::{
};
use tracing::{debug, trace};

const FRAMERATE: Duration = Duration::from_millis(3);
pub const FRAMERATE: Duration = Duration::from_millis(3);
const RESIZE_DEBOUNCE_DELAY: Duration = Duration::from_millis(10);

use super::{
Expand Down Expand Up @@ -590,14 +590,7 @@ async fn run_app_inner<B: Backend + std::io::Write>(
let mut resize_debouncer = Debouncer::new(RESIZE_DEBOUNCE_DELAY);
let mut callback = None;
let mut needs_rerender = true;
while let Some(event) = poll(
app.input_options()?,
&mut receiver,
&mut crossterm_rx,
last_render + FRAMERATE,
)
.await
{
while let Some(event) = poll(app.input_options()?, &mut receiver, &mut crossterm_rx).await {
// If we only receive ticks, then there's been no state change so no update
// needed
if !matches!(event, Event::Tick) {
Expand Down Expand Up @@ -639,7 +632,6 @@ async fn poll<'a>(
input_options: InputOptions<'a>,
receiver: &mut AppReceiver,
crossterm_rx: &mut mpsc::Receiver<crossterm::event::Event>,
deadline: Instant,
) -> Option<Event> {
let input_closed = crossterm_rx.is_closed();
let input_fut = async {
Expand All @@ -660,11 +652,7 @@ async fn poll<'a>(
}
};

match tokio::time::timeout_at(deadline, event_fut).await {
Ok(Some(e)) => Some(e),
Err(_timeout) => Some(Event::Tick),
Ok(None) => None,
}
event_fut.await
}

const MIN_HEIGHT: u16 = 10;
Expand Down
11 changes: 11 additions & 0 deletions crates/turborepo-ui/src/tui/handle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use tokio::sync::{mpsc, oneshot};

use super::{
app::FRAMERATE,
event::{CacheResult, OutputLogs, PaneSize},
Error, Event, TaskResult,
};
Expand All @@ -24,6 +25,16 @@ impl TuiSender {
/// AppReceiver should be passed to `crate::tui::run_app`
pub fn new() -> (Self, AppReceiver) {
let (primary_tx, primary_rx) = mpsc::unbounded_channel();
let tick_sender = primary_tx.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(FRAMERATE);
loop {
interval.tick().await;
if tick_sender.send(Event::Tick).is_err() {
break;
}
}
});
(
Self {
primary: primary_tx,
Expand Down

0 comments on commit 76122e0

Please sign in to comment.