Skip to content

Commit 3ce954e

Browse files
committed
fix: handle xterm selection disposable cleanup
1 parent 5d8a23b commit 3ce954e

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

packages/web/src/features/terminal-panel/__tests__/xterm-host.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,20 @@ describe("XtermHost", () => {
244244
expect(consoleSpy).toHaveBeenCalledWith("Failed to dispose xterm instance:", expect.any(Error));
245245
});
246246

247+
it("does not crash on unmount when onSelectionChange returns a disposable object", () => {
248+
mockTerminal.onSelectionChange.mockImplementationOnce(() => ({
249+
dispose: vi.fn(),
250+
}));
251+
252+
const { unmount } = render(
253+
<JotaiProvider>
254+
<XtermHost terminalId="selection-disposable-terminal" workspaceId="test-workspace" />
255+
</JotaiProvider>
256+
);
257+
258+
expect(() => unmount()).not.toThrow();
259+
});
260+
247261
it("renders without crashing", () => {
248262
const { container } = render(
249263
<JotaiProvider>

packages/web/src/features/terminal-panel/views/shared/xterm-host.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,7 +1115,7 @@ export function XtermHost({
11151115
terminal.onData((data) => {
11161116
void handleInputRef.current(data);
11171117
});
1118-
const disposeSelectionChange =
1118+
const selectionChangeDisposable =
11191119
typeof terminal.onSelectionChange === "function"
11201120
? terminal.onSelectionChange(() => {
11211121
selectedTextRef.current = terminal.hasSelection() ? terminal.getSelection() : "";
@@ -1778,7 +1778,11 @@ export function XtermHost({
17781778
terminalRef.current = null;
17791779
fitAddonRef.current = null;
17801780
}
1781-
disposeSelectionChange?.();
1781+
if (typeof selectionChangeDisposable === "function") {
1782+
selectionChangeDisposable();
1783+
} else {
1784+
selectionChangeDisposable?.dispose?.();
1785+
}
17821786
};
17831787
}, [
17841788
dispatch,

0 commit comments

Comments
 (0)