From 5482f8758e089fdc2ccd945ac805b968259049fc Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Fri, 8 Nov 2024 15:46:42 +0100 Subject: [PATCH] Wait for global state to be populated --- src/frontend/app.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/frontend/app.rs b/src/frontend/app.rs index e057eb4..e4f10ba 100644 --- a/src/frontend/app.rs +++ b/src/frontend/app.rs @@ -41,6 +41,7 @@ use leptos::{ use leptos_meta::{provide_meta_context, *}; use leptos_router::{Route, Router, Routes}; use reqwest::Client; +use std::{thread::sleep, time::Duration}; // https://book.leptos.dev/15_global_state.html #[derive(Clone)] @@ -51,7 +52,13 @@ pub struct GlobalState { impl GlobalState { pub fn api_client() -> ApiClient { - use_context::>() + let mut global_state = use_context::>(); + // Wait for global state to be populated (only needed on instance_details for some reason) + while global_state.is_none() { + sleep(Duration::from_millis(10)); + global_state = use_context::>(); + } + global_state .expect("global state is provided") .get_untracked() .api_client