Skip to content

Commit 2a930a7

Browse files
saphidclaude
andcommitted
fix(client-runtime): stopping the browser stream also clears the file picker
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 4b964e1 commit 2a930a7

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

‎packages/client-runtime/src/preview/serverBrowserStream.test.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,29 @@ describe("preview stream control", () => {
108108
client.stop();
109109
});
110110

111-
it("withdraws an offered file picker when the stream drops", () => {
111+
it("withdraws an offered file picker when the stream drops or stops", () => {
112112
const onFileChooser = vi.fn();
113-
const client = createPreviewStreamClient(target, {
113+
const events = {
114114
onFrame: vi.fn(),
115115
onFileChooser,
116116
onViewport: vi.fn(),
117117
onConnectedChange: vi.fn(),
118118
onUnauthorized: vi.fn(),
119-
});
119+
};
120+
const client = createPreviewStreamClient(target, events);
120121
const socket = FakeSocket.current;
121122
socket.dispatchEvent(new Event("open"));
122123
socket.message(JSON.stringify({ type: "fileChooser", id: "c1", multiple: false, accept: "" }));
123124
socket.dispatchEvent(Object.assign(new Event("close"), { code: 1012 }));
124125
expect(onFileChooser).toHaveBeenLastCalledWith(null);
125126
client.stop();
127+
128+
const restarted = createPreviewStreamClient(target, events);
129+
FakeSocket.current.message(
130+
JSON.stringify({ type: "fileChooser", id: "c2", multiple: false, accept: "" }),
131+
);
132+
restarted.stop();
133+
expect(onFileChooser).toHaveBeenLastCalledWith(null);
126134
});
127135
});
128136

‎packages/client-runtime/src/preview/serverBrowserStream.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ export function createPreviewStreamClient(
224224
let control: PreviewStreamControl | null = null;
225225
let fileChooser: string | null = null;
226226

227+
// The server re-offers a picker still open, under a new id, when this viewer reattaches.
228+
const withdrawFileChooser = () => {
229+
if (fileChooser === null) return;
230+
fileChooser = null;
231+
events.onFileChooser?.(null);
232+
};
233+
227234
const connect = () => {
228235
if (stopped) return;
229236
const ws = new WebSocket(url);
@@ -346,11 +353,7 @@ export function createPreviewStreamClient(
346353
if (socket !== ws) return;
347354
socket = null;
348355
control = null;
349-
// The server re-offers a picker still open, under a new id, when this viewer reattaches.
350-
if (fileChooser !== null) {
351-
fileChooser = null;
352-
events.onFileChooser?.(null);
353-
}
356+
withdrawFileChooser();
354357
if (opened) events.onConnectedChange(false);
355358
if (stopped) return;
356359
if (event.code === 4404) {
@@ -390,6 +393,7 @@ export function createPreviewStreamClient(
390393
const ws = socket;
391394
socket = null;
392395
control = null;
396+
withdrawFileChooser();
393397
ws?.close();
394398
},
395399
};

0 commit comments

Comments
 (0)