gpui-async by longbridge/gpui-component
npx skills add https://github.com/longbridge/gpui-component --skill gpui-asyncGPUI 为前台 UI 更新和后台计算提供了集成的异步运行时。
核心概念:
cx.spawn)cx.background_spawn)impl MyComponent {
fn fetch_data(&mut self, cx: &mut Context<Self>) {
let entity = cx.entity().downgrade();
cx.spawn(async move |cx| {
// 在 UI 线程上运行,可以 await 并更新实体
let data = fetch_from_api().await;
entity.update(cx, |state, cx| {
state.data = Some(data);
cx.notify();
}).ok();
}).detach();
}
}
impl MyComponent {
fn process_file(&mut self, cx: &mut Context<Self>) {
let entity = cx.entity().downgrade();
cx.background_spawn(async move {
// 在后台线程上运行,CPU 密集型
let result = heavy_computation().await;
result
})
.then(cx.spawn(move |result, cx| {
// 回到前台更新 UI
entity.update(cx, |state, cx| {
state.result = result;
cx.notify();
}).ok();
}))
.detach();
}
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
struct MyView {
_task: Task<()>, // 如果存储但不访问,则使用 _ 前缀
}
impl MyView {
fn new(cx: &mut Context<Self>) -> Self {
let entity = cx.entity().downgrade();
let _task = cx.spawn(async move |cx| {
// 任务在销毁时自动取消
loop {
tokio::time::sleep(Duration::from_secs(1)).await;
entity.update(cx, |state, cx| {
state.tick();
cx.notify();
}).ok();
}
});
Self { _task }
}
}
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();
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();
cx.spawn(async move |cx| {
loop {
tokio::time::sleep(Duration::from_secs(5)).await;
// 每 5 秒更新一次
}
}).detach();
任务在销毁时自动取消。存储在结构体中以保持其活动状态。
// ❌ 错误:无法从后台线程更新实体
cx.background_spawn(async move {
entity.update(cx, |state, cx| { // 编译错误!
state.data = data;
});
});
// ✅ 正确:与前台任务链式调用
cx.background_spawn(async move { data })
.then(cx.spawn(move |data, cx| {
entity.update(cx, |state, cx| {
state.data = data;
cx.notify();
}).ok();
}))
.detach();
API 参考 : 参见 api-reference.md
模式 : 参见 patterns.md
最佳实践 : 参见 best-practices.md
每周安装量
160
代码仓库
GitHub 星标数
10.7K
首次出现
2026 年 1 月 21 日
安全审计
安装于
opencode145
codex138
gemini-cli137
github-copilot126
cursor119
amp117
GPUI provides integrated async runtime for foreground UI updates and background computation.
Key Concepts:
cx.spawn)cx.background_spawn)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();
}
}
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();
}
}
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 }
}
}
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();
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();
cx.spawn(async move |cx| {
loop {
tokio::time::sleep(Duration::from_secs(5)).await;
// Update every 5 seconds
}
}).detach();
Tasks are automatically cancelled when dropped. Store in struct to keep alive.
// ❌ Wrong: Can't update entities from background thread
cx.background_spawn(async move {
entity.update(cx, |state, cx| { // Compile error!
state.data = data;
});
});
// ✅ 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();
API Reference : See api-reference.md
Patterns : See patterns.md
Best Practices : See best-practices.md
Weekly Installs
160
Repository
GitHub Stars
10.7K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode145
codex138
gemini-cli137
github-copilot126
cursor119
amp117
TanStack Query v5 完全指南:React 数据管理、乐观更新、离线支持
2,500 周安装
Sentry 错误追踪指南:Node.js/Python 异常监控、性能分析与最佳实践
162 周安装
iOS TestFlight崩溃调查指南:使用Xcode Organizer快速定位与修复Beta测试问题
162 周安装
iOS小组件与扩展开发指南:解决数据更新、实时活动与控制中心问题
161 周安装
会话管理最佳实践:JWT令牌、CSRF防护、Redis存储与安全实现指南
161 周安装
AI智能体上下文管理技能context-surfing:防止漂移与幻觉,实现高保真执行
168 周安装
AI安全文档生成工具 | 一键创建合规策略、安全指南与最佳实践
162 周安装