How to dynamically initialize a global variable in gpui? #41041
-
|
Hello everyone! I'm a rust beginner and I just started to use gpui to build my own tool. Here is my code snippet: pub struct State {
client: Option<SrunClient>,
}
app.run(move |cx| {
cx.set_global(State {
client: None,
});
cx.background_executor().spawn(async move {
let client = SrunClient::new(username, password, Some(Client::new()), None, None)
.await
.unwrap();
cx.update_global::<State, ()>(|state, _| {
state.client = Some(client);
});
});
})You can see that the initialization step of Is there any way to achieve this? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
|
Not only the init code is async, it's not on the main thread, on a background thread — there's no UI on that thread, hence no |
Beta Was this translation helpful? Give feedback.
-
Thank you! |
Beta Was this translation helpful? Give feedback.
Not only the init code is async, it's not on the main thread, on a background thread — there's no UI on that thread, hence no
cx.Instead,
cx.spawnshould be used.