AgentSkillsCN

lv:assigns

借助 5 个并行的专业子代理,开展全方位的项目健康审计。从架构、性能、安全、测试质量与依赖关系等多个维度进行分析,最终生成附带健康评分的可执行报告。适用于每季度一次,或在重大版本发布前使用。

SKILL.md
--- frontmatter
name: lv:assigns
description: Audit LiveView socket assigns for memory issues and clarity. Use when reviewing LiveView performance or debugging memory problems.
argument: path/to/live_view.ex

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

Quick Audit Commands

Extract All Assigns

bash
grep -E "assign\(|assign_new\(" path/to/live_view.ex

Find Large Data Patterns

bash
# Lists stored in assigns
grep -E "assign.*\[\]|assign.*Repo\.all" path/to/live_view.ex

# Full schema storage
grep -E "assign.*Repo\.get|assign.*%.*\{\}" path/to/live_view.ex

Audit Checklist

1. Memory Issues

PatternProblemSolution
assign(:items, Repo.all(...))Unbounded listUse stream/3
assign(:user, Repo.get!(...))Full schemaSelect only needed fields
assign(:file_data, binary)Large binaryStore reference, not data
Nested preloadsExcessive dataPreload only what's rendered

2. Missing temporary_assigns

Should use temporary_assigns:

  • Flash messages
  • Form errors after submission
  • One-time notifications
  • Upload progress
elixir
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:

bash
# Find assigns
grep -oE "assign\(:(\w+)" live_view.ex | sort -u

# Compare with template usage
grep -oE "@\w+" template.html.heex | sort -u

4. Missing Initialization

elixir
# 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 TypeApprox SizeConcern Level
Integer8 bytesLow
String (100 chars)~200 bytesLow
List of 100 maps~10-50 KBMedium
List of 1000 items~100-500 KBHigh
Binary (image)VariesCritical
Full Ecto schema~1-5 KB eachMedium

Usage

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