use std::ops::Deref; use std::sync::Arc; use anyhow::anyhow; use tokio::sync::OnceCell; use crate::LauncherResult; use crate::api::proxy::LauncherProxy; static PROXY_STATE: OnceCell> = OnceCell::const_new(); pub struct ProxyState { inner: Box, } impl Deref for ProxyState { type Target = Box; fn deref(&self) -> &Self::Target { &self.inner } } impl ProxyState { #[tracing::instrument(level = "debug")] pub async fn initialize(proxy: impl LauncherProxy + 'static) -> LauncherResult> { PROXY_STATE .get_or_try_init(|| async { Ok(Arc::new(Self { inner: Box::new(proxy), })) }) .await .cloned() } pub fn get() -> LauncherResult> { Ok(PROXY_STATE .get() .ok_or_else(|| anyhow!("proxy state not initialized")) .cloned()?) } }