Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions apps/web/src/terminalUiStateStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,25 @@ describe("terminalUiStateStore actions", () => {
expect(layout().terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID, "new"]);
});

it.each([
["unmounts right after the close", false],
["sees the closing terminal once more before it unmounts", true],
])("keeps a reopened terminal when the drawer %s", (_case, staleReconcile) => {
const store = useTerminalUiStateStore.getState();
store.setTerminalOpen(THREAD_REF, true);
store.reconcileTerminalIds(THREAD_REF, [DEFAULT_THREAD_TERMINAL_ID], "manager-1");
store.closeTerminal(THREAD_REF, DEFAULT_THREAD_TERMINAL_ID);
if (staleReconcile) {
store.reconcileTerminalIds(THREAD_REF, [DEFAULT_THREAD_TERMINAL_ID], "manager-1");
}
// The drawer is gone now, so nothing reconciles the server dropping the terminal.
store.ensureTerminal(THREAD_REF, DEFAULT_THREAD_TERMINAL_ID, { open: true });
store.reconcileTerminalIds(THREAD_REF, [], "manager-1");

expect(layout().terminalOpen).toBe(true);
expect(layout().terminalIds).toEqual([DEFAULT_THREAD_TERMINAL_ID]);
});

it("keeps the surviving pane active when another client closes its split sibling", () => {
const store = useTerminalUiStateStore.getState();
store.newTerminal(THREAD_REF, "first");
Expand Down
35 changes: 34 additions & 1 deletion apps/web/src/terminalUiStateStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,22 @@ interface TerminalServerObservation {
readonly snapshotId: string | symbol | undefined;
}

function forgetObservedTerminalId(
serverObservationsByThreadKey: Record<string, TerminalServerObservation>,
threadKey: string,
terminalId: string,
): Record<string, TerminalServerObservation> {
const observation = serverObservationsByThreadKey[threadKey];
if (!observation?.ids.includes(terminalId)) {
return serverObservationsByThreadKey;
}
const ids = observation.ids.filter((id) => id !== terminalId);
if (ids.length === 0) {
return removeRecordEntry(serverObservationsByThreadKey, threadKey);
}
return { ...serverObservationsByThreadKey, [threadKey]: { ...observation, ids } };
}

interface TerminalUiStateStoreState {
serverObservationsByThreadKey: Record<string, TerminalServerObservation>;
terminalUiStateByThreadKey: Record<string, ThreadTerminalUiState>;
Expand Down Expand Up @@ -642,15 +658,32 @@ export const useTerminalUiStateStore = create<TerminalUiStateStoreState>()(
suppression.suppressed,
)
: state.suppressedTerminalIdsByThreadKey;
// A listing from before this client placed the id is about an earlier terminal, so it
// must not make reconcile close the new one.
const nextServerObservationsByThreadKey =
suppression &&
!suppression.suppressed &&
!selectThreadTerminalUiState(
state.terminalUiStateByThreadKey,
threadRef,
).terminalIds.includes(suppression.terminalId)
? forgetObservedTerminalId(
state.serverObservationsByThreadKey,
terminalThreadKey(threadRef),
suppression.terminalId,
)
: state.serverObservationsByThreadKey;
if (
nextTerminalUiStateByThreadKey === state.terminalUiStateByThreadKey &&
nextSuppressedTerminalIdsByThreadKey === state.suppressedTerminalIdsByThreadKey
nextSuppressedTerminalIdsByThreadKey === state.suppressedTerminalIdsByThreadKey &&
nextServerObservationsByThreadKey === state.serverObservationsByThreadKey
) {
return state;
}
return {
terminalUiStateByThreadKey: nextTerminalUiStateByThreadKey,
suppressedTerminalIdsByThreadKey: nextSuppressedTerminalIdsByThreadKey,
serverObservationsByThreadKey: nextServerObservationsByThreadKey,
};
});
};
Expand Down
Loading