make update_needed check lazy

This commit is contained in:
Conrad Ludgate 2023-02-06 08:55:12 +00:00
parent a40e41caff
commit d2a1548dae
No known key found for this signature in database
GPG Key ID: 197E3CACA1C980B5
3 changed files with 34 additions and 9 deletions

13
Cargo.lock generated
View File

@ -89,6 +89,7 @@ dependencies = [
"env_logger",
"eyre",
"fs-err",
"futures-util",
"indicatif",
"interim",
"itertools",
@ -727,6 +728,17 @@ dependencies = [
"parking_lot 0.11.2",
]
[[package]]
name = "futures-macro"
version = "0.3.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42cd15d1c7456c04dbdf7e88bcd69760d74f3a798d6444e16974b505b0e62f17"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.24"
@ -746,6 +758,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44fb6cb1be61cc1d2e43b262516aafcf63b241cffdb1d3fa115f91d9c7b09c90"
dependencies = [
"futures-core",
"futures-macro",
"futures-sink",
"futures-task",
"pin-project-lite",

View File

@ -72,6 +72,7 @@ whoami = "1.1.2"
rpassword = "7.0"
semver = "1.0.14"
runtime-format = "0.1.2"
futures-util = "0.3"
# from tui
bitflags = "1.3"

View File

@ -16,6 +16,7 @@ use crossterm::{
execute, terminal,
};
use eyre::Result;
use futures_util::FutureExt;
use semver::Version;
use unicode_width::UnicodeWidthStr;
@ -393,7 +394,8 @@ pub async fn history(
// Put the cursor at the end of the query by default
input.end();
let update_needed = settings.needs_update().await;
let update_needed = settings.needs_update().fuse();
tokio::pin!(update_needed);
let mut app = State {
history_count: db.history_count().await?,
@ -405,7 +407,7 @@ pub async fn history(
} else {
settings.filter_mode
},
update_needed,
update_needed: None,
};
let mut results = app.query_results(settings.search_mode, db).await?;
@ -427,15 +429,24 @@ pub async fn history(
let initial_input = app.input.as_str().to_owned();
let initial_filter_mode = app.filter_mode;
if event::poll(Duration::from_millis(250))? {
loop {
if let Some(i) = app.handle_input(settings, &event::read()?, results.len()) {
break 'render i;
}
if !event::poll(Duration::ZERO)? {
break;
let event_ready = tokio::task::spawn_blocking(|| event::poll(Duration::from_millis(250)));
tokio::select! {
event_ready = event_ready => {
if event_ready?? {
loop {
if let Some(i) = app.handle_input(settings, &event::read()?, results.len()) {
break 'render i;
}
if !event::poll(Duration::ZERO)? {
break;
}
}
}
}
update_needed = &mut update_needed => {
app.update_needed = update_needed;
}
}
if initial_input != app.input.as_str() || initial_filter_mode != app.filter_mode {