fix: selectSession race when clicking B while A still loading
- SHA
f2d3758fde40e6de51506d2d843907317b2364a4- Parents
-
68a6e58 - Tree
7e75874
f2d3758
f2d3758fde40e6de51506d2d843907317b2364a468a6e58
7e75874| Status | File | + | - |
|---|---|---|---|
| M |
src/lib/store/sessions.ts
|
7 | 0 |
src/lib/store/sessions.tsmodified@@ -263,17 +263,24 @@ export const useSessionStore = create<SessionStore>((set, get) => ({ | ||
| 263 | 263 | error: null, |
| 264 | 264 | }; |
| 265 | 265 | }); |
| 266 | + // Race guard: if the user clicks session B while session A's | |
| 267 | + // readSession is still in flight, A's `set` would otherwise | |
| 268 | + // overwrite B's state a moment later. Check the still-selected | |
| 269 | + // id after each await and bail if the user has moved on. | |
| 270 | + const targetId = session.id; | |
| 266 | 271 | try { |
| 267 | 272 | const detail = await readSession( |
| 268 | 273 | session.projectId, |
| 269 | 274 | session.id, |
| 270 | 275 | session.source, |
| 271 | 276 | ); |
| 277 | + if (get().selectedSessionId !== targetId) return; | |
| 272 | 278 | set((s) => ({ |
| 273 | 279 | detail, |
| 274 | 280 | loading: { ...s.loading, detail: false }, |
| 275 | 281 | })); |
| 276 | 282 | } catch (err) { |
| 283 | + if (get().selectedSessionId !== targetId) return; | |
| 277 | 284 | set((s) => ({ |
| 278 | 285 | loading: { ...s.loading, detail: false }, |
| 279 | 286 | error: formatError(err), |