gpui-async▌
longbridge/gpui-component · updated Apr 8, 2026
GPUI provides integrated async runtime for foreground UI updates and background computation.
Overview
GPUI provides integrated async runtime for foreground UI updates and background computation.
Key Concepts:
- Foreground tasks: UI thread, can update entities (
cx.spawn) - Background tasks: Worker threads, CPU-intensive work (
cx.background_spawn) - All entity updates happen on foreground thread
Quick Start
Foreground Tasks (UI Updates)
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 Tasks (Heavy Work)
impl MyComponent {
fn process_file(&mut self, cx: &mut Context<Self>) {
let entity = cx.entity().downgrade();
cx.background_spawn(async move {
// Runs on background thread, CPU-intensive
let result = heavy_computation().await;
result
})
.then(cx.spawn(move |result, cx| {
// Back to foreground to update UI
entity.update(cx, |state, cx| {
state.result = result;
cx.notify();
}).ok();
}))
.detach();
}
}
Task Management
struct MyView {
_task: Task<()>, // Prefix with _ if stored but not accessed
}
impl MyView {
fn new(cx: &mut Context<Self>) -> Self {
let entity = cx.entity().downgrade();
let _task = cx.spawn(async move |cx| {
// Task automatically cancelled when dropped
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
entity.update(cx, |state, cx| {
state.tick();
cx.notify();
}).ok();
}
});
Self { _task }
}
}
Core Patterns
1. Async Data Fetching
cx.spawn(async move |cx| {
let data = fetch_data().await?;
entity.update(cx, |state, cx| {
state.data = Some(data);
cx.notify();
})?;
Ok::<_, anyhow::Error>(())
}).detach();
2. Background Computation + UI Update
cx.background_spawn(async move {
heavy_work()
})
.then(cx.spawn(move |result, cx| {
entity.update(cx, |state, cx| {
state.result = result;
cx.notify();
}).ok();
}))
.detach();
3. Periodic Tasks
cx.spawn(async move |cx| {
loop {
tokio::time::sleep(Duration::from_secs(5)).await;
// Update every 5 seconds
}
}).detach();
4. Task Cancellation
Tasks are automatically cancelled when dropped. Store in struct to keep alive.
Common Pitfalls
❌ Don't: Update entities from background tasks
// ❌ Wrong: Can't update entities from background thread
cx.background_spawn(async move {
entity.update(cx, |state, cx| { // Compile error!
state.data = data;
});
});
✅ Do: Use foreground task or chain
// ✅ Correct: Chain with foreground task
cx.background_spawn(async move { data })
.then(cx.spawn(move |data, cx| {
entity.update(cx, |state, cx| {
state.data = data;
cx.notify();
}).ok();
}))
.detach();
Reference Documentation
Complete Guides
-
API Reference: See api-reference.md
- Task types, spawning methods, contexts
- Executors, cancellation, error handling
-
Patterns: See patterns.md
- Data fetching, background processing
- Polling, debouncing, parallel tasks
- Pattern selection guide
-
Best Practices: See best-practices.md
- Error handling, cancellation
- Performance optimization, testing
- Common pitfalls and solutions
Discussion
Product Hunt–style comments (not star reviews)- No comments yet — start the thread.
Ratings
4.8★★★★★44 reviews- ★★★★★Noor Thomas· Dec 28, 2024
We added gpui-async from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.
- ★★★★★Xiao Liu· Dec 24, 2024
Keeps context tight: gpui-async is the kind of skill you can hand to a new teammate without a long onboarding doc.
- ★★★★★Advait Thompson· Dec 24, 2024
gpui-async fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.
- ★★★★★Dhruvi Jain· Dec 16, 2024
gpui-async fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.
- ★★★★★Noor Anderson· Nov 19, 2024
Solid pick for teams standardizing on skills: gpui-async is focused, and the summary matches what you get after install.
- ★★★★★Kwame Johnson· Nov 19, 2024
gpui-async reduced setup friction for our internal harness; good balance of opinion and flexibility.
- ★★★★★Kwame Brown· Nov 15, 2024
I recommend gpui-async for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.
- ★★★★★Carlos Rahman· Nov 15, 2024
Registry listing for gpui-async matched our evaluation — installs cleanly and behaves as described in the markdown.
- ★★★★★Oshnikdeep· Nov 7, 2024
Registry listing for gpui-async matched our evaluation — installs cleanly and behaves as described in the markdown.
- ★★★★★Ganesh Mohane· Oct 26, 2024
gpui-async reduced setup friction for our internal harness; good balance of opinion and flexibility.
showing 1-10 of 44