lv-assigns
Inspect LiveView socket assigns for memory bloat — missing temporary_assigns,
它会碰到什么
这一栏是扫描器报的事实,不是结论。命中多不等于有毒(安全工具、规则库、示例脚本本来就会包含危险写法),命中少也不等于干净。它和你手上的凭据、文件、网络有什么关系,需要你自己看。
技能内容
LiveView Assigns Audit
Analyze LiveView socket assigns for memory efficiency, clarity, and best practices.
Iron Laws - Never Violate These
- Use streams for lists > 100 items - Never store large lists directly in assigns
- Use temporary_assigns for transient data - Flash messages, temp errors, notifications
- Preload only needed fields - Don't store full Ecto schemas when only needing subset
- Initialize all assigns in mount - Never access assigns that might not exist
- 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 / Apache 等)的技能按仓库打包整理到网盘,点一下转存到你自己的网盘,不用一个个从 GitHub 拉。许可未声明的技能只给原始仓库链接,不打包。
它属于哪个仓库
targets/dsh/skills/lv-assigns/SKILL.md同一个仓库里的其他技能
同名技能的其他版本
有 5 个不同仓库或目录里都有叫 lv-assigns 的技能。它们内容并不相同,别混用:
- oliver-kriska/claude-elixir-phoenix — Inspect LiveView socket assigns for memory bloat — missing temporary_assigns,
- oliver-kriska/claude-elixir-phoenix — Inspect LiveView socket assigns for memory bloat — missing; Use when
- oliver-kriska/claude-elixir-phoenix — Inspect LiveView socket assigns for memory bloat — missing temporary_assigns,
- oliver-kriska/claude-elixir-phoenix — Inspect LiveView socket assigns for memory bloat — missing temporary_assigns,