Cmd.map Fractal Dashboard
Demonstrates Fractal Architecture using Cmd.map for component composition.
Problem
Without composition, a complex app needs one giant case statement handling every possible message from every childβthe βGod Reducerβ anti-pattern. This doesnβt scale.
Solution
Cmd.map wraps child commands so their results route through parents:
# Child produces [:system_info, {stdout:, ...}] child_cmd = SystemInfoWidget.fetch_cmd # Parent wraps to produce [:stats, :system_info, {...}] parent_cmd = Cmd.map(child_cmd) { |m| [:stats, *m] }
Each layer handles only its own messages. Parents pattern-match on the first element to route to the correct child.
Architecture
Dashboard (root) βββ StatsPanel β βββ SystemInfoWidget β Cmd.exec("uname -a", :system_info) β βββ DiskUsageWidget β Cmd.exec("df -h", :disk_usage) βββ NetworkPanel βββ PingWidget β Cmd.exec("ping -c 1 localhost", :ping) βββ UptimeWidget β Cmd.exec("uptime", :uptime)
Hotkeys
| Key | Action |
|---|---|
s |
Fetch system info |
d |
Fetch disk usage |
p |
Ping localhost |
u |
Fetch uptime |
q |
Quit |
Key Concepts
-
Widget isolation: Each widget has its own
Model,UPDATE, andfetch_cmd. It knows nothing about parents. -
Message routing: Parents prefix child messages (
:stats,:network) and pattern-match to route. -
Recursive dispatch:
Cmd.mapdelegates inner command execution to the runtime, then transforms the result.
Usage
ruby examples/widget_cmd_map/app.rb