gpui-entity by longbridge/gpui-component
npx skills add https://github.com/longbridge/gpui-component --skill gpui-entityEntity<T> 是一个指向类型为 T 的状态的句柄,提供安全的访问和更新功能。
核心方法:
entity.read(cx) → &T - 只读访问entity.read_with(cx, |state, cx| ...) → R - 使用闭包读取entity.update(cx, |state, cx| ...) → R - 可变更新entity.downgrade() → - 创建弱引用广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
WeakEntity<T>entity.entity_id() → EntityId - 唯一标识符实体类型:
Entity<T>: 强引用(增加引用计数)WeakEntity<T>: 弱引用(不阻止清理,返回 Result)// 创建实体
let counter = cx.new(|cx| Counter { count: 0 });
// 读取状态
let count = counter.read(cx).count;
// 更新状态
counter.update(cx, |state, cx| {
state.count += 1;
cx.notify(); // 触发重新渲染
});
// 弱引用(用于闭包/回调)
let weak = counter.downgrade();
let _ = weak.update(cx, |state, cx| {
state.count += 1;
cx.notify();
});
struct MyComponent {
shared_state: Entity<SharedData>,
}
impl MyComponent {
fn new(cx: &mut App) -> Entity<Self> {
let shared = cx.new(|_| SharedData::default());
cx.new(|cx| Self {
shared_state: shared,
})
}
fn update_shared(&mut self, cx: &mut Context<Self>) {
self.shared_state.update(cx, |state, cx| {
state.value = 42;
cx.notify();
});
}
}
impl MyComponent {
fn fetch_data(&mut self, cx: &mut Context<Self>) {
let weak_self = cx.entity().downgrade();
cx.spawn(async move |cx| {
let data = fetch_from_api().await;
// 安全地更新实体
let _ = weak_self.update(cx, |state, cx| {
state.data = Some(data);
cx.notify();
});
}).detach();
}
}
// ✅ 良好:弱引用防止循环引用
let weak = cx.entity().downgrade();
callback(move || {
let _ = weak.update(cx, |state, cx| cx.notify());
});
// ❌ 不佳:强引用可能导致内存泄漏
let strong = cx.entity();
callback(move || {
strong.update(cx, |state, cx| cx.notify());
});
// ✅ 良好:使用闭包中的内部 cx
entity.update(cx, |state, inner_cx| {
inner_cx.notify(); // 正确
});
// ❌ 不佳:使用外部 cx(多重借用错误)
entity.update(cx, |state, inner_cx| {
cx.notify(); // 错误!
});
// ✅ 良好:顺序更新
entity1.update(cx, |state, cx| { /* ... */ });
entity2.update(cx, |state, cx| { /* ... */ });
// ❌ 不佳:嵌套更新(可能导致恐慌)
entity1.update(cx, |_, cx| {
entity2.update(cx, |_, cx| { /* ... */ });
});
模式 : 参见 patterns.md
最佳实践 : 参见 best-practices.md
高级模式 : 参见 advanced.md
每周安装量
143
代码仓库
GitHub 星标数
10.7K
首次出现
2026年1月21日
安全审计
安装于
opencode129
gemini-cli121
codex121
github-copilot112
cursor109
claude-code103
An Entity<T> is a handle to state of type T, providing safe access and updates.
Key Methods:
entity.read(cx) → &T - Read-only accessentity.read_with(cx, |state, cx| ...) → R - Read with closureentity.update(cx, |state, cx| ...) → R - Mutable updateentity.downgrade() → WeakEntity<T> - Create weak referenceentity.entity_id() → EntityId - Unique identifierEntity Types:
Entity<T>: Strong reference (increases ref count)WeakEntity<T>: Weak reference (doesn't prevent cleanup, returns Result)// Create entity
let counter = cx.new(|cx| Counter { count: 0 });
// Read state
let count = counter.read(cx).count;
// Update state
counter.update(cx, |state, cx| {
state.count += 1;
cx.notify(); // Trigger re-render
});
// Weak reference (for closures/callbacks)
let weak = counter.downgrade();
let _ = weak.update(cx, |state, cx| {
state.count += 1;
cx.notify();
});
struct MyComponent {
shared_state: Entity<SharedData>,
}
impl MyComponent {
fn new(cx: &mut App) -> Entity<Self> {
let shared = cx.new(|_| SharedData::default());
cx.new(|cx| Self {
shared_state: shared,
})
}
fn update_shared(&mut self, cx: &mut Context<Self>) {
self.shared_state.update(cx, |state, cx| {
state.value = 42;
cx.notify();
});
}
}
impl MyComponent {
fn fetch_data(&mut self, cx: &mut Context<Self>) {
let weak_self = cx.entity().downgrade();
cx.spawn(async move |cx| {
let data = fetch_from_api().await;
// Update entity safely
let _ = weak_self.update(cx, |state, cx| {
state.data = Some(data);
cx.notify();
});
}).detach();
}
}
// ✅ Good: Weak reference prevents retain cycles
let weak = cx.entity().downgrade();
callback(move || {
let _ = weak.update(cx, |state, cx| cx.notify());
});
// ❌ Bad: Strong reference may cause memory leak
let strong = cx.entity();
callback(move || {
strong.update(cx, |state, cx| cx.notify());
});
// ✅ Good: Use inner cx from closure
entity.update(cx, |state, inner_cx| {
inner_cx.notify(); // Correct
});
// ❌ Bad: Use outer cx (multiple borrow error)
entity.update(cx, |state, inner_cx| {
cx.notify(); // Wrong!
});
// ✅ Good: Sequential updates
entity1.update(cx, |state, cx| { /* ... */ });
entity2.update(cx, |state, cx| { /* ... */ });
// ❌ Bad: Nested updates (may panic)
entity1.update(cx, |_, cx| {
entity2.update(cx, |_, cx| { /* ... */ });
});
Patterns : See patterns.md
Best Practices : See best-practices.md
Advanced Patterns : See advanced.md
Weekly Installs
143
Repository
GitHub Stars
10.7K
First Seen
Jan 21, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode129
gemini-cli121
codex121
github-copilot112
cursor109
claude-code103
Rust性能优化指南:m10-performance技能详解,包含算法、数据结构与内存优化策略
718 周安装