From 85de48af448f4288e28f288ab0d3e2d6d3b251be Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Mon, 17 Jun 2024 12:36:55 -0700 Subject: [PATCH] Ensure crumb is loaded before querying API --- src/main.rs | 9 +++++---- src/task/company.rs | 2 +- src/task/current_price.rs | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 030ee2c..3bb4230 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,7 +43,7 @@ lazy_static! { pub static ref SHOW_VOLUMES: RwLock = RwLock::new(OPTS.show_volumes); pub static ref DEFAULT_TIMESTAMPS: RwLock>> = Default::default(); pub static ref THEME: theme::Theme = OPTS.theme.unwrap_or_default(); - pub static ref YAHOO_CRUMB: RwLock> = RwLock::default(); + pub static ref YAHOO_CRUMB: async_std::sync::RwLock> = Default::default(); } fn main() { @@ -56,6 +56,7 @@ fn main() { setup_panic_hook(); setup_terminal(); + set_crumb(); let request_redraw = REDRAW_REQUEST.0.clone(); let data_received = DATA_RECEIVED.1.clone(); @@ -80,8 +81,6 @@ fn main() { let default_timestamp_service = DefaultTimestampService::new(); - set_crumb(); - let app = Arc::new(Mutex::new(app::App { mode: starting_mode, stocks: starting_stocks, @@ -236,8 +235,10 @@ fn setup_panic_hook() { fn set_crumb() { async_std::task::spawn(async move { + let mut _guard = YAHOO_CRUMB.write().await; + if let Ok(crumb) = CLIENT.get_crumb().await { - *YAHOO_CRUMB.write() = Some(crumb); + *_guard = Some(crumb); } }); } diff --git a/src/task/company.rs b/src/task/company.rs index b4af146..8ce64d0 100644 --- a/src/task/company.rs +++ b/src/task/company.rs @@ -32,7 +32,7 @@ impl AsyncTask for Company { Box::pin(async move { let symbol = input.as_ref(); - let crumb = YAHOO_CRUMB.read().clone(); + let crumb = YAHOO_CRUMB.read().await.clone(); if let Some(crumb) = crumb { crate::CLIENT.get_company_data(symbol, crumb).await.ok() diff --git a/src/task/current_price.rs b/src/task/current_price.rs index 747ef2a..b89612a 100644 --- a/src/task/current_price.rs +++ b/src/task/current_price.rs @@ -31,7 +31,7 @@ impl AsyncTask for CurrentPrice { Box::pin(async move { let symbol = input.as_ref(); - let crumb = YAHOO_CRUMB.read().clone(); + let crumb = YAHOO_CRUMB.read().await.clone(); if let Some(crumb) = crumb { if let Ok(response) = crate::CLIENT.get_company_data(symbol, crumb).await {