user_store.rs

 1use std::sync::Arc;
 2use std::time::Duration;
 3
 4use anyhow::Context as _;
 5use cloud_api_client::{AuthenticatedUser, CloudApiClient};
 6use gpui::{Context, Task};
 7use util::{ResultExt as _, maybe};
 8
 9pub struct CloudUserStore {
10    authenticated_user: Option<AuthenticatedUser>,
11    _fetch_authenticated_user_task: Task<()>,
12}
13
14impl CloudUserStore {
15    pub fn new(cloud_client: Arc<CloudApiClient>, cx: &mut Context<Self>) -> Self {
16        Self {
17            authenticated_user: None,
18            _fetch_authenticated_user_task: cx.spawn(async move |this, cx| {
19                maybe!(async move {
20                    loop {
21                        if cloud_client.has_credentials() {
22                            break;
23                        }
24
25                        cx.background_executor()
26                            .timer(Duration::from_millis(100))
27                            .await;
28                    }
29
30                    let response = cloud_client.get_authenticated_user().await?;
31                    this.update(cx, |this, _cx| {
32                        this.authenticated_user = Some(response.user);
33                    })
34                })
35                .await
36                .context("failed to fetch authenticated user")
37                .log_err();
38            }),
39        }
40    }
41}