Commit 6248a30
authored
🤖 fix: persist localStorage writes synchronously to prevent data loss (#1036)
## Problem
Reviews attached to chat were lost on Electron restart because
localStorage writes were deferred via `queueMicrotask`. If the app
closed before the microtask ran (e.g., force quit, crash, or immediate
close), the data was never persisted.
## Root Cause
`usePersistedState` hook was using `queueMicrotask` to batch
localStorage writes for "performance optimization" (added in #330).
However, this introduced a race condition:
1. User creates a review → React state updates immediately
2. Microtask queued to write to localStorage
3. User closes app (or it crashes) → Microtask never runs
4. On restart → localStorage has stale/missing data
## Solution
Remove `queueMicrotask` and write to localStorage synchronously. This
ensures data persists immediately after state change, at the cost of a
small blocking operation (~1-2ms for typical JSON.stringify + setItem).
The synchronous write is acceptable because:
- localStorage writes are fast (~1-2ms for typical data)
- Data integrity is more important than micro-optimization
- The deferred write was a premature optimization that caused real bugs
## Testing
- [x] `make typecheck` passes
- [x] `make lint` passes
- [x] `make static-check` passes
- [x] Existing tests pass
_Generated with `mux`_1 parent 6488f22 commit 6248a30
1 file changed
+19
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
153 | | - | |
154 | | - | |
155 | | - | |
156 | | - | |
157 | | - | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
167 | 158 | | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
168 | 168 | | |
169 | | - | |
| 169 | + | |
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| |||
0 commit comments