TypeScript
fix(preview): a dropped stream withdraws its stale file picker (#29655)
Alex Southwellc185d780fc3f
diff --git a/packages/client-runtime/src/preview/serverBrowserStream.test.ts b/packages/client-runtime/src/preview/serverBrowserStream.test.tsindex cfde00363a0..7b749acea44 100644--- a/packages/client-runtime/src/preview/serverBrowserStream.test.ts+++ b/packages/client-runtime/src/preview/serverBrowserStream.test.ts@@ -107,6 +107,31 @@ describe("preview stream control", () => {expect(client.send({ type: "dialog", accept: false })).toBe(false);client.stop();});++ it("withdraws an offered file picker when the stream drops or stops", () => {+ const onFileChooser = vi.fn();+ const events = {+ onFrame: vi.fn(),+ onFileChooser,+ onViewport: vi.fn(),+ onConnectedChange: vi.fn(),+ onUnauthorized: vi.fn(),+ };+ const client = createPreviewStreamClient(target, events);+ const socket = FakeSocket.current;+ socket.dispatchEvent(new Event("open"));+ socket.message(JSON.stringify({ type: "fileChooser", id: "c1", multiple: false, accept: "" }));+ socket.dispatchEvent(Object.assign(new Event("close"), { code: 1012 }));+ expect(onFileChooser).toHaveBeenLastCalledWith(null);+ client.stop();++ const restarted = createPreviewStreamClient(target, events);+ FakeSocket.current.message(+ JSON.stringify({ type: "fileChooser", id: "c2", multiple: false, accept: "" }),+ );+ restarted.stop();+ expect(onFileChooser).toHaveBeenLastCalledWith(null);+ });});describe("preview stream downloads", () => {diff --git a/packages/client-runtime/src/preview/serverBrowserStream.ts b/packages/client-runtime/src/preview/serverBrowserStream.tsindex b71fff335f8..617c1d18424 100644--- a/packages/client-runtime/src/preview/serverBrowserStream.ts+++ b/packages/client-runtime/src/preview/serverBrowserStream.ts@@ -224,6 +224,13 @@ export function createPreviewStreamClient(let control: PreviewStreamControl | null = null;let fileChooser: string | null = null;+ // The server re-offers a picker still open, under a new id, when this viewer reattaches.+ const withdrawFileChooser = () => {+ if (fileChooser === null) return;+ fileChooser = null;+ events.onFileChooser?.(null);+ };+const connect = () => {if (stopped) return;const ws = new WebSocket(url);@@ -346,6 +353,7 @@ export function createPreviewStreamClient(if (socket !== ws) return;socket = null;control = null;+ withdrawFileChooser();if (opened) events.onConnectedChange(false);if (stopped) return;if (event.code === 4404) {@@ -385,6 +393,7 @@ export function createPreviewStreamClient(const ws = socket;socket = null;control = null;+ withdrawFileChooser();ws?.close();},};