|
| 1 | +--- |
| 2 | +name: solidjs-patterns |
| 3 | +description: SolidJS reactivity + UI state patterns for OpenWork |
| 4 | +--- |
| 5 | + |
| 6 | +## Why this skill exists |
| 7 | + |
| 8 | +OpenWork’s UI is SolidJS: it updates via **signals**, not React-style rerenders. |
| 9 | +Most “UI stuck” bugs are actually **state coupling** bugs (e.g. one global `busy()` disabling an unrelated action), not rerender issues. |
| 10 | + |
| 11 | +This skill captures the patterns we want to consistently use in OpenWork. |
| 12 | + |
| 13 | +## Core rules |
| 14 | + |
| 15 | +- Prefer **fine-grained signals** over shared global flags. |
| 16 | +- Keep async actions **scoped** (each action gets its own `pending` state). |
| 17 | +- Derive UI state via `createMemo()` instead of duplicating booleans. |
| 18 | +- Avoid mutating arrays/objects stored in signals; always create new values. |
| 19 | + |
| 20 | +## Scoped async actions (recommended) |
| 21 | + |
| 22 | +When an operation can overlap with others (permissions, installs, background refresh), don’t reuse a global `busy()`. |
| 23 | + |
| 24 | +Use a dedicated signal per action: |
| 25 | + |
| 26 | +```ts |
| 27 | +const [replying, setReplying] = createSignal(false); |
| 28 | + |
| 29 | +async function respond() { |
| 30 | + if (replying()) return; |
| 31 | + setReplying(true); |
| 32 | + try { |
| 33 | + await doTheThing(); |
| 34 | + } finally { |
| 35 | + setReplying(false); |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### Why |
| 41 | + |
| 42 | +A single `busy()` boolean creates deadlocks: |
| 43 | + |
| 44 | +- Long-running task sets `busy(true)` |
| 45 | +- A permission prompt appears and its buttons are disabled by `busy()` |
| 46 | +- The task can’t continue until permission is answered |
| 47 | +- The user can’t answer because buttons are disabled |
| 48 | + |
| 49 | +Fix: permission UI must be disabled only by a **permission-specific** pending state. |
| 50 | + |
| 51 | +## Signal snapshots in async handlers |
| 52 | + |
| 53 | +If you read signals inside an async function and you need stable values, snapshot early: |
| 54 | + |
| 55 | +```ts |
| 56 | +const request = activePermission(); |
| 57 | +if (!request) return; |
| 58 | +const requestID = request.id; |
| 59 | + |
| 60 | +await respondPermission(requestID, "always"); |
| 61 | +``` |
| 62 | + |
| 63 | +## Derived UI state |
| 64 | + |
| 65 | +Prefer `createMemo()` for computed disabled states: |
| 66 | + |
| 67 | +```ts |
| 68 | +const canSend = createMemo(() => prompt().trim().length > 0 && !busy()); |
| 69 | +``` |
| 70 | + |
| 71 | +## Lists |
| 72 | + |
| 73 | +- Use setter callbacks for derived updates: |
| 74 | + |
| 75 | +```ts |
| 76 | +setItems((current) => current.filter((x) => x.id !== id)); |
| 77 | +``` |
| 78 | + |
| 79 | +- Don’t mutate `current` in-place. |
| 80 | + |
| 81 | +## Practical checklist (SolidJS UI changes) |
| 82 | + |
| 83 | +- Does any button depend on a global flag that could be true during long-running work? |
| 84 | +- Could two async actions overlap and fight over one boolean? |
| 85 | +- Is any UI state duplicated (can be derived instead)? |
| 86 | +- Do event handlers read signals after an `await` where values might have changed? |
| 87 | + |
| 88 | +## References |
| 89 | + |
| 90 | +- SolidJS: https://www.solidjs.com/docs/latest |
| 91 | +- SolidJS signals: https://www.solidjs.com/docs/latest/api#createsignal |
| 92 | +- SolidJS memos: https://www.solidjs.com/docs/latest/api#creatememo |
0 commit comments