Commit ddfb21b
committed
perf(scenes): stop replacing the entire overlay node on each UI change
Right now UIManager is more React-like in data flow, but it still does a full root swap. That’s
acceptable for this small UI, but the cleaner next step would be one of these:
- Keep a stable root and re-render only its children.
This avoids replacing the mounted DOM subtree and is a better fit if you add more UI state or
transitions.
I’d keep #ui-overlay mounted once and make only its contents change.
Right now UIManager creates a new root <div id="ui-overlay">...</div> and replaces the old one on each
meaningful UI update. That works, but it means every state change destroys and recreates the whole
overlay subtree, including the button element and any future transition/focus state.
The cleaner version is:
1. Mount a stable root once in the constructor.
Create this.root = document.createElement('div'), give it the static overlay classes/id, append it to
document.body, and never replace that node again.
2. Add a dedicated content container inside the root.
For example this.contentRoot = document.createElement('div') or just render directly into this.root
if jsx-dom supports replacing children cleanly. The point is that the outer shell stays stable.
3. Compute a pure UI view model in render(state, score).
Something like:
- scoreText
- isScoreVisible
- panelContent
- buttonMode
This isolates game-state mapping from DOM work.
4. Re-render only the children from props.
GameOverlay becomes a pure presentational component:
- <GameOverlay scoreText=... isScoreVisible=... panelContent=... onPrimaryAction=... />
Then UIManager.render(...) clears/replaces the children inside the stable root instead of
replacing the root itself.
5. Keep callbacks stable.
The button click handler should still call this.handlePrimaryAction(), and buttonMode stays owned by
UIManager.
Why this is better:
- #ui-overlay remains mounted, so future transitions, focus handling, and DOM hooks are more reliable.
- The UI still feels React-like because rendering is props-driven.
- UIManager becomes a thin container instead of a DOM mutation class.
- It makes the next step easy: extracting getUIState(...) into a pure function and testing it
independently.1 parent 0764155 commit ddfb21b
1 file changed
Lines changed: 24 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | | - | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
28 | 36 | | |
| 37 | + | |
29 | 38 | | |
30 | 39 | | |
31 | 40 | | |
| |||
48 | 57 | | |
49 | 58 | | |
50 | 59 | | |
51 | | - | |
| 60 | + | |
52 | 61 | | |
53 | 62 | | |
54 | 63 | | |
55 | 64 | | |
56 | | - | |
57 | | - | |
58 | 65 | | |
59 | 66 | | |
60 | 67 | | |
| |||
113 | 120 | | |
114 | 121 | | |
115 | 122 | | |
116 | | - | |
| 123 | + | |
117 | 124 | | |
118 | 125 | | |
119 | 126 | | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
136 | 138 | | |
137 | 139 | | |
0 commit comments