‹ 首页

assigns-audit

@oliver-kriska · 收录于 4 天前 · 上游提交 4 天前

Inspect LiveView socket assigns for memory bloat — missing temporary_assigns, unused assigns, unbounded lists needing streams, memory estimates. Use when LiveView memory grows or you need to add temporary_assigns.

适合你,如果 LiveView 内存不断增长,你需要找出未使用的 assigns

/ 通过 npx 安装 校验哈希
npx oh-my-skill add oliver-kriska/claude-elixir-phoenix/assigns-audit
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- oliver-kriska/claude-elixir-phoenix/assigns-audit
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify oliver-kriska/claude-elixir-phoenix/assigns-audit
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
503GitHub stars
~665上下文体积 · 单文件
索引托管

怎么用

商店整理自技能原文 · 版本 88c1194 · 表述以原文为准
它做什么

检查 LiveView socket assigns 的内存效率:识别缺失的 temporary_assigns、未使用的 assigns、大列表未用 stream,并给出内存估计和优化建议。

什么时候触发

用户提供 LiveView 文件路径并运行命令 `/lv:assigns path/to/live_view.ex` 时触发,通常用于 LiveView 内存增长或需要添加 temporary_assigns 的场景。

装好后可以这样说
它会分析assigns的内存使用。
会对比模板发现未引用的assign。
识别适合临时的assigns。
技能原文 SKILL.md作者撰写 · MIT · 88c1194

LiveView Assigns Audit

Analyze LiveView socket assigns for memory efficiency, clarity, and best practices.

Iron Laws - Never Violate These
  1. Use streams for lists > 100 items - Never store large lists directly in assigns
  2. Use temporary_assigns for transient data - Flash messages, temp errors, notifications
  3. Preload only needed fields - Don't store full Ecto schemas when only needing subset
  4. Initialize all assigns in mount - Never access assigns that might not exist
  5. NEVER modify assigns or code during audit — this is a read-only diagnostic; report findings only
Quick Audit Commands
Extract All Assigns

Use Grep to find all assign( and assign_new( calls in the target LiveView file.

Find Large Data Patterns

Use Grep to find large data patterns: lists stored in assigns (assign.*\[\], assign.*Repo\.all) and full schema storage (assign.*Repo\.get) in the target file.

Audit Checklist
1. Memory Issues

| Pattern | Problem | Solution | |---------|---------|----------| | assign(:items, Repo.all(...)) | Unbounded list | Use stream/3 | | assign(:user, Repo.get!(...)) | Full schema | Select only needed fields | | assign(:file_data, binary) | Large binary | Store reference, not data | | Nested preloads | Excessive data | Preload only what's rendered |

2. Missing temporary_assigns

Should use temporary_assigns:

  • Flash messages
  • Form errors after submission
  • One-time notifications
  • Upload progress
def mount(_params, _session, socket) do
  {:ok, socket, temporary_assigns: [flash_message: nil]}
end
3. Unused Assigns

Search for assigns defined but never used in templates:

Use Grep to extract all assign names (assign\(:(\w+)) from the LiveView file, then use Grep to find all @\w+ references in the corresponding .heex template. Compare to find unused assigns.

4. Missing Initialization
# BAD: @items might not exist
def render(assigns) do
  ~H"<%= for item <- @items do %>"
end

# GOOD: Initialize in mount
def mount(_params, _session, socket) do
  {:ok, assign(socket, items: [])}
end
Memory Estimation

For each assign, estimate memory footprint:

| Data Type | Approx Size | Concern Level | |-----------|-------------|---------------| | Integer | 8 bytes | Low | | String (100 chars) | ~200 bytes | Low | | List of 100 maps | ~10-50 KB | Medium | | List of 1000 items | ~100-500 KB | High | | Binary (image) | Varies | Critical | | Full Ecto schema | ~1-5 KB each | Medium |

Usage

Run /lv:assigns path/to/live_view.ex to generate an assigns inventory with memory estimates and optimization recommendations.

按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。