Skip to content

Commit

Permalink
Ensure crumb is loaded before querying API
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Jun 17, 2024
1 parent 8a04f54 commit 85de48a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ lazy_static! {
pub static ref SHOW_VOLUMES: RwLock<bool> = RwLock::new(OPTS.show_volumes);
pub static ref DEFAULT_TIMESTAMPS: RwLock<HashMap<TimeFrame, Vec<i64>>> = Default::default();
pub static ref THEME: theme::Theme = OPTS.theme.unwrap_or_default();
pub static ref YAHOO_CRUMB: RwLock<Option<CrumbData>> = RwLock::default();
pub static ref YAHOO_CRUMB: async_std::sync::RwLock<Option<CrumbData>> = Default::default();
}

fn main() {
Expand All @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
});
}
2 changes: 1 addition & 1 deletion src/task/company.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/task/current_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 85de48a

Please sign in to comment.