Skip to content

Commit ac781a7

Browse files
fix(web): queued visits still land when a thread loads after the wait times out
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 55dd5c9 commit ac781a7

2 files changed

Lines changed: 56 additions & 20 deletions

File tree

‎apps/web/src/browserHistoryStore.test.ts‎

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { readPreparedConnection, entities, settingsHydration } = vi.hoisted(() =>
88
entities: {
99
projects: [] as Array<Record<string, unknown>>,
1010
shellProjectId: null as string | null,
11-
shellArrived: new Promise<boolean>(() => {}),
11+
waitForThreadShell: (): Promise<boolean> => new Promise(() => {}),
1212
},
1313
settingsHydration: {
1414
hydrated: true,
@@ -27,7 +27,7 @@ vi.mock("~/state/entities", () => ({
2727
readProjects: () => entities.projects,
2828
useProjects: () => entities.projects,
2929
useQuietThreadShell: () => null,
30-
waitForThreadShell: () => entities.shellArrived,
30+
waitForThreadShell: () => entities.waitForThreadShell(),
3131
waitForProject: () => new Promise(() => {}),
3232
}));
3333
vi.mock("./hooks/useSettings", async () => {
@@ -66,7 +66,7 @@ beforeEach(() => {
6666
readPreparedConnection.mockReturnValue(null);
6767
entities.projects = [];
6868
entities.shellProjectId = null;
69-
entities.shellArrived = new Promise<boolean>(() => {});
69+
entities.waitForThreadShell = () => new Promise(() => {});
7070
settingsHydration.hydrated = true;
7171
settingsHydration.promise = Promise.resolve();
7272
useComposerDraftStore.setState({ draftThreadsByThreadKey: {} });
@@ -304,22 +304,38 @@ describe("thread project resolution", () => {
304304
).toEqual(["http://b.test/", "http://a.test/"]);
305305
});
306306

307-
it("lands a visit queued before the thread loaded once the thread arrives", async () => {
308-
let shellArrives = (_found: boolean) => {};
309-
entities.shellArrived = new Promise<boolean>((resolve) => {
310-
shellArrives = resolve;
307+
it("lands visits queued before the thread loaded when the thread arrives after a timed-out wait", async () => {
308+
const waits: Array<(found: boolean) => void> = [];
309+
let retried = () => {};
310+
const retry = new Promise<void>((resolve) => {
311+
retried = resolve;
312+
});
313+
entities.waitForThreadShell = () =>
314+
new Promise<boolean>((resolve) => {
315+
waits.push(resolve);
316+
if (waits.length === 2) retried();
317+
});
318+
const landed = new Promise<void>((resolve) => {
319+
const unsubscribe = useBrowserHistoryStore.subscribe((state) => {
320+
if (!state.byProjectKey["env-1:/repo"]) return;
321+
unsubscribe();
322+
resolve();
323+
});
311324
});
312325
recordVisitForThread(threadRef, "http://a.test/", 1);
313-
expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({});
326+
recordVisitForThread(threadRef, "http://b.test/", 2);
327+
expect(waits).toHaveLength(1);
314328

329+
waits[0]?.(false);
330+
await retry;
315331
entities.projects = [repoProject];
316332
entities.shellProjectId = "project-1";
317-
shellArrives(true);
318-
await entities.shellArrived;
333+
waits[1]?.(true);
334+
await landed;
319335

320-
expect(useBrowserHistoryStore.getState().byProjectKey).toEqual({
321-
"env-1:/repo": [{ url: "http://a.test/", lastVisitedAt: 1 }],
322-
});
336+
expect(
337+
useBrowserHistoryStore.getState().byProjectKey["env-1:/repo"]?.map((entry) => entry.url),
338+
).toEqual(["http://b.test/", "http://a.test/"]);
323339
expect(useBrowserHistoryStore.getState().pendingVisitsByThreadKey).toEqual({});
324340
});
325341
});

‎apps/web/src/browserHistoryStore.ts‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,31 @@ function readThreadProjectRef(ref: ScopedThreadRef): ScopedProjectRef | null {
377377
return draft ? scopeProjectRef(draft.environmentId, draft.projectId) : null;
378378
}
379379

380+
const awaitingThreadKeys = new Set<string>();
381+
382+
/** Waits keep timing out and retrying while writes are queued, so a slow load still lands them. */
383+
function awaitThreadProject(ref: ScopedThreadRef, projectRef: ScopedProjectRef | null): void {
384+
const threadKey = scopedThreadKey(ref);
385+
if (awaitingThreadKeys.has(threadKey)) return;
386+
awaitingThreadKeys.add(threadKey);
387+
const arrived = projectRef
388+
? waitForProject(projectRef).then(() => true)
389+
: waitForThreadShell(ref);
390+
void arrived
391+
.catch(() => false)
392+
.then((found) => {
393+
awaitingThreadKeys.delete(threadKey);
394+
const state = useBrowserHistoryStore.getState();
395+
if (
396+
found ||
397+
state.pendingVisitsByThreadKey[threadKey] ||
398+
state.pendingTitlesByThreadKey[threadKey]
399+
) {
400+
resolveThreadProjectKey(ref);
401+
}
402+
});
403+
}
404+
380405
/**
381406
* Registers the thread under its current project so queued visits and titles
382407
* land. Until the project loads, or client settings hydrate (the grouping is
@@ -396,13 +421,7 @@ function resolveThreadProjectKey(ref: ScopedThreadRef): string | undefined {
396421
const projectRef = readThreadProjectRef(ref);
397422
const project = projectRef ? readProject(projectRef) : null;
398423
if (!project) {
399-
if (registered === undefined) {
400-
const arrived = projectRef ? waitForProject(projectRef) : waitForThreadShell(ref);
401-
void arrived.then(
402-
(found) => (found ? resolveThreadProjectKey(ref) : undefined),
403-
() => undefined,
404-
);
405-
}
424+
if (registered === undefined) awaitThreadProject(ref, projectRef);
406425
return registered;
407426
}
408427
const projectKey = deriveHistoryProjectKey(
@@ -504,6 +523,7 @@ export function useThreadRecentHistory(
504523
}
505524

506525
export function resetBrowserHistoryForTests(): void {
526+
awaitingThreadKeys.clear();
507527
useBrowserHistoryStore.setState({
508528
byProjectKey: {},
509529
projectKeyByThreadKey: {},

0 commit comments

Comments
 (0)