鱼皮AI导航
鱼皮AI导航
  • 主页
  • 社区
  • AI工具
  • AI知识库
  • AI提示词
  • MCP
  • Skills
  • AI学习资源
  • AI资讯
  • GitHub开源
登录/注册

gpui-entity

提供在 GPUI 框架中安全管理应用状态的核心能力,支持多组件间共享与协同操作实体数据,确保并发读写安全,并通过强弱引用机制避免内存泄漏,同时无缝集成异步任务与响应式更新。

软件开发
访问 GitHub
文档Skills.MD
name
gpui-entity
description
Entity management and state handling in GPUI. Use when working with entities, managing component state, coordinating between components, handling async operations with state updates, or implementing reactive patterns. Entities provide safe concurrent access to application state.

Overview

An Entity<T> is a handle to state of type T, providing safe access and updates.

Key Methods:

  • entity.read(cx) → &T - Read-only access
  • entity.read_with(cx, |state, cx| ...) → R - Read with closure
  • entity.update(cx, |state, cx| ...) → R - Mutable update
  • entity.downgrade() → WeakEntity<T> - Create weak reference
  • entity.entity_id() → EntityId - Unique identifier

Entity Types:

  • Entity<T>: Strong reference (increases ref count)
  • WeakEntity<T>: Weak reference (doesn't prevent cleanup, returns Result)

Quick Start

Creating and Using Entities

▼
rust
复制代码
// 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(); });

In Components

▼
rust
复制代码
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(); }); } }

Async Operations

▼
rust
复制代码
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(); } }

Core Principles

Always Use Weak References in Closures

▼
rust
复制代码
// ✅ 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()); });

Use Inner Context

▼
rust
复制代码
// ✅ 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! });

Avoid Nested Updates

▼
rust
复制代码
// ✅ 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| { /* ... */ }); });

Common Use Cases

  1. Component State: Internal state that needs reactivity
  2. Shared State: State shared between multiple components
  3. Parent-Child: Coordinating between related components (use weak refs)
  4. Async State: Managing state that changes from async operations
  5. Observations: Reacting to changes in other entities

Reference Documentation

Complete API Documentation

  • Entity API: See api-reference.md
    • Entity types, methods, lifecycle
    • Context methods, async operations
    • Error handling, type conversions

Implementation Guides

  • Patterns: See patterns.md

    • Model-view separation, state management
    • Cross-entity communication, async operations
    • Observer pattern, event subscription
    • Pattern selection guide
  • Best Practices: See best-practices.md

    • Avoiding common pitfalls, memory leaks
    • Performance optimization, batching updates
    • Lifecycle management, cleanup
    • Async best practices, testing
  • Advanced Patterns: See advanced.md

    • Entity collections, registry pattern
    • Debounced/throttled updates, state machines
    • Entity snapshots, transactions, pools
软件开发

快捷安装

复制到终端去安装 Skill

npxskillsaddlongbridge/gpui-component--skill"gpui-entity"
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
站长:程序员鱼皮 老鱼简历 商务合作 增值电信业务经营许可证
2026 鱼皮AI导航 | 沪ICP备19026706号-6
🔥热门工具
朱雀AI检测朱雀AI检测
朱雀AI检测助手朱雀AI检测助手
LovartLovart
老鱼简历老鱼简历
WritePass官网WritePass官网
面多多面多多
CursorCursor
星月写作星月写作
朱雀AI检测助手朱雀AI检测助手
零感AI零感AI
Skills 推荐
debug
debug 是一个系统化定位软件缺陷根因的技能,适用于用户报告 'fix'、'broken'、'error' 等问题或测试意外失败时。它通过可验证的假设、最小复现、因果链分析和回归测试,确保在修复前准确定位根本原因。
retrieving-developer-knowledge
retrieving-developer-knowledge 用于搜索、获取和整合 Google 官方开发者文档,覆盖 Google Cloud、AI/Gemini、Android、Chrome、Web、Flutter、Go、Firebase 等平台,适用于查询 gcloud CLI 命令、API 语法、IAM 权限或架构对比等场景。
diff-intake
diff-intake 读取 PR 或分支差异,生成结构化 YAML 变更摘要,用于下游 analytics instrumentation 技能。适用于用户提交 PR 链接、分支对比或原始 diff 后需理解变更内容、评估分析覆盖或启动埋点流程的场景。
implement
实现 Linus Dashboard 的功能开发与集成,基于 TypeScript 和 Python 构建前后端组件,遵循严格的代码规范与架构模式,完成视图、卡片、服务及传感器等模块的创建与验证,确保高质量交付并兼容 Home Assistant 生态。
pypi-server
支持从源码构建 Python 软件包,并搭建本地 PyPI 服务,实现私有包的托管与分发,使团队可通过 pip 直接安装内部开发的模块。