Skip to content

Commit

Permalink
Ensure crumb is loaded before querying API (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah authored Jun 17, 2024
1 parent 8a04f54 commit fb53601
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and `Removed`.

## [Unreleased]

### Fixed

- Race condition preventing data from loading if a long update interval was defined

## [0.14.9] - 2023-07-25

### Fixed
Expand Down
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 fb53601

Please sign in to comment.