From e95cdf7d76064eb8166a36823a202cf5e0f03859 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Wed, 18 Nov 2026 08:34:12 +0000 Subject: [PATCH] fix(core): each editing client takes its own port, so a sealed preview answers Studio again (#8154) * test(core): two editing clients of one preview frame each get their replies * fix(core): each editing client takes its own port, so a sealed preview answers Studio again --- packages/core/src/runtime/editingPort.test.ts | 38 +++++++++++++++++++ packages/core/src/runtime/editingPort.ts | 5 ++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/packages/core/src/runtime/editingPort.test.ts b/packages/core/src/runtime/editingPort.test.ts index d0d013eb4f..5dc030bb20 100644 --- a/packages/core/src/runtime/editingPort.test.ts +++ b/packages/core/src/runtime/editingPort.test.ts @@ -14,6 +14,16 @@ function fakeHost() { return { host: host as unknown as Window, deliver: (e: Partial) => listener?.(e) }; } +function hostReachingEveryListenerOfADispatch() { + const listeners = new Set(); + const host = { + addEventListener: (_type: string, fn: Listener) => listeners.add(fn), + removeEventListener: (_type: string, fn: Listener) => listeners.delete(fn), + }; + const deliver = (e: Partial) => [...listeners].forEach((fn) => fn(e)); + return { host: host as unknown as Window, deliver }; +} + function previewFrame() { const iframe = document.createElement("iframe"); document.body.appendChild(iframe); @@ -99,6 +109,34 @@ describe("createEditingPortClient", () => { } }); + it("gives two clients of one frame a port each, so both get their replies", async () => { + const { iframe, frame } = previewFrame(); + const { host, deliver } = hostReachingEveryListenerOfADispatch(); + const parent = document.body.appendChild(document.createElement("iframe")).contentWindow!; + parent.postMessage = ((data: unknown, _origin: string, transfer: MessagePort[]) => { + ports.push(...transfer); + setTimeout(() => deliver({ source: frame, data, ports: transfer })); + }) as Window["postMessage"]; + frame.postMessage = ((data: unknown) => + window.dispatchEvent( + new MessageEvent("message", { source: parent, data }), + )) as Window["postMessage"]; + const agent = installMarkedEditingAgent(parent); + try { + const first = createEditingPortClient({ iframe, host, timeoutMs: 1_000 }); + const second = createEditingPortClient({ iframe, host, timeoutMs: 1_000 }); + clients.push(first, second); + await Promise.all([first.ready, second.ready]); + const [z, loop] = await Promise.all([ + first.request("max-z-index", {}), + second.request("set-loop-start", { seconds: null }), + ]); + expect([z, loop]).toEqual([{ zIndex: 0 }, { applied: false }]); + } finally { + agent.dispose(); + } + }); + it("hands each key the frame forwards to its listeners and drops off-shape ones", async () => { const { iframe, frame } = previewFrame(); const { host, deliver } = fakeHost(); diff --git a/packages/core/src/runtime/editingPort.ts b/packages/core/src/runtime/editingPort.ts index dedc7b25de..f6823b0da1 100644 --- a/packages/core/src/runtime/editingPort.ts +++ b/packages/core/src/runtime/editingPort.ts @@ -588,6 +588,8 @@ function readHandoff( const onlyPort = (ports: readonly MessagePort[]): MessagePort | null => ports.length === 1 ? (ports[0] ?? null) : null; +const portsTaken = new WeakSet(); + type Pending = { resolve: (result: unknown) => void; reject: (error: EditingPortError) => void; @@ -653,7 +655,8 @@ export function createEditingPortClient(options: { const onWindowMessage = (event: MessageEvent) => { const handoff = readHandoff(event, iframe.contentWindow); if (handoff === "ready") return connect(); - if (!handoff) return; + if (!handoff || portsTaken.has(handoff)) return; + portsTaken.add(handoff); port = handoff; host.removeEventListener("message", onWindowMessage); port.onmessage = onReply;