Reworked loop to lower latency and continue throttling until it's out of events

This commit is contained in:
Mikayla Maki 2022-08-03 12:18:11 -07:00
parent ed3d339dec
commit 4126e977b6

View File

@ -356,32 +356,43 @@ impl TerminalBuilder {
use futures::StreamExt;
while let Some(event) = self.events_rx.next().await {
let mut timer = cx.background().timer(Duration::from_millis(2)).fuse();
let mut events = vec![event];
loop {
futures::select_biased! {
_ = timer => break,
event = self.events_rx.next() => {
if let Some(event) = event {
events.push(event);
if events.len() > 100 {
break;
}
} else {
break;
}
},
}
}
this.upgrade(&cx)?.update(&mut cx, |this, cx| {
for event in events {
this.process_event(&event, cx);
}
//Process the first event immediately for lowered latency
this.process_event(&event, cx);
});
smol::future::yield_now().await;
'outer: loop {
let mut events = vec![];
let mut timer = cx.background().timer(Duration::from_millis(4)).fuse();
loop {
futures::select_biased! {
_ = timer => break,
event = self.events_rx.next() => {
if let Some(event) = event {
events.push(event);
if events.len() > 100 {
break;
}
} else {
break;
}
},
}
}
if events.len() == 0 {
smol::future::yield_now().await;
break 'outer;
} else {
this.upgrade(&cx)?.update(&mut cx, |this, cx| {
for event in events {
this.process_event(&event, cx);
}
});
smol::future::yield_now().await;
}
}
}
Some(())