What problem does it solve?
gpui-async coordinates foreground UI updates with long-running background tasks in GPUI apps.
Core Features & Use Cases
- Foreground tasks: schedule UI updates using cx.spawn, ensuring updates occur on the UI thread.
- Background tasks: offload CPU-heavy work with cx.background_spawn and chain results back to the UI via then.
- Task lifecycle: automatic cancellation on drop and safe cleanup to prevent leaks.
- Periodic tasks and coordination: support recurring work and synchronization between foreground and background work.
Quick Start
Foreground Task example:
impl MyComponent {
fn fetch_data(&mut self, cx: &mut Context<Self>) {
let entity = cx.entity().downgrade();
cx.spawn(async move |cx| {
// Runs on UI thread, can await and update entities
let data = fetch_from_api().await;
entity.update(cx, |state, cx| {
state.data = Some(data);
cx.notify();
}).ok();
}).detach();
}
}
Background Task example:
impl MyComponent {
fn process_file(&mut self, cx: &mut Context<Self>) {
let entity = cx.entity().downgrade();
cx.background_spawn(async move {
let result = heavy_computation().await;
result
}).then(cx.spawn(move |result, cx| {
entity.update(cx, |state, cx| {
state.result = result;
cx.notify();
}).ok();
})).detach();
}
}