tenseleyflow/claudex / f2d3758

Browse files

fix: selectSession race when clicking B while A still loading

Authored by espadonne
SHA
f2d3758fde40e6de51506d2d843907317b2364a4
Parents
68a6e58
Tree
7e75874

1 changed file

StatusFile+-
M src/lib/store/sessions.ts 7 0
src/lib/store/sessions.tsmodified
@@ -263,17 +263,24 @@ export const useSessionStore = create<SessionStore>((set, get) => ({
263263
         error: null,
264264
       };
265265
     });
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;
266271
     try {
267272
       const detail = await readSession(
268273
         session.projectId,
269274
         session.id,
270275
         session.source,
271276
       );
277
+      if (get().selectedSessionId !== targetId) return;
272278
       set((s) => ({
273279
         detail,
274280
         loading: { ...s.loading, detail: false },
275281
       }));
276282
     } catch (err) {
283
+      if (get().selectedSessionId !== targetId) return;
277284
       set((s) => ({
278285
         loading: { ...s.loading, detail: false },
279286
         error: formatError(err),