Skip to content

Commit e95cdf7

Browse files
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
1 parent 5bd3e32 commit e95cdf7

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

‎packages/core/src/runtime/editingPort.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ function fakeHost() {
1414
return { host: host as unknown as Window, deliver: (e: Partial<MessageEvent>) => listener?.(e) };
1515
}
1616

17+
function hostReachingEveryListenerOfADispatch() {
18+
const listeners = new Set<Listener>();
19+
const host = {
20+
addEventListener: (_type: string, fn: Listener) => listeners.add(fn),
21+
removeEventListener: (_type: string, fn: Listener) => listeners.delete(fn),
22+
};
23+
const deliver = (e: Partial<MessageEvent>) => [...listeners].forEach((fn) => fn(e));
24+
return { host: host as unknown as Window, deliver };
25+
}
26+
1727
function previewFrame() {
1828
const iframe = document.createElement("iframe");
1929
document.body.appendChild(iframe);
@@ -99,6 +109,34 @@ describe("createEditingPortClient", () => {
99109
}
100110
});
101111

112+
it("gives two clients of one frame a port each, so both get their replies", async () => {
113+
const { iframe, frame } = previewFrame();
114+
const { host, deliver } = hostReachingEveryListenerOfADispatch();
115+
const parent = document.body.appendChild(document.createElement("iframe")).contentWindow!;
116+
parent.postMessage = ((data: unknown, _origin: string, transfer: MessagePort[]) => {
117+
ports.push(...transfer);
118+
setTimeout(() => deliver({ source: frame, data, ports: transfer }));
119+
}) as Window["postMessage"];
120+
frame.postMessage = ((data: unknown) =>
121+
window.dispatchEvent(
122+
new MessageEvent("message", { source: parent, data }),
123+
)) as Window["postMessage"];
124+
const agent = installMarkedEditingAgent(parent);
125+
try {
126+
const first = createEditingPortClient({ iframe, host, timeoutMs: 1_000 });
127+
const second = createEditingPortClient({ iframe, host, timeoutMs: 1_000 });
128+
clients.push(first, second);
129+
await Promise.all([first.ready, second.ready]);
130+
const [z, loop] = await Promise.all([
131+
first.request("max-z-index", {}),
132+
second.request("set-loop-start", { seconds: null }),
133+
]);
134+
expect([z, loop]).toEqual([{ zIndex: 0 }, { applied: false }]);
135+
} finally {
136+
agent.dispose();
137+
}
138+
});
139+
102140
it("hands each key the frame forwards to its listeners and drops off-shape ones", async () => {
103141
const { iframe, frame } = previewFrame();
104142
const { host, deliver } = fakeHost();

‎packages/core/src/runtime/editingPort.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ function readHandoff(
588588
const onlyPort = (ports: readonly MessagePort[]): MessagePort | null =>
589589
ports.length === 1 ? (ports[0] ?? null) : null;
590590

591+
const portsTaken = new WeakSet<MessagePort>();
592+
591593
type Pending = {
592594
resolve: (result: unknown) => void;
593595
reject: (error: EditingPortError) => void;
@@ -653,7 +655,8 @@ export function createEditingPortClient(options: {
653655
const onWindowMessage = (event: MessageEvent<unknown>) => {
654656
const handoff = readHandoff(event, iframe.contentWindow);
655657
if (handoff === "ready") return connect();
656-
if (!handoff) return;
658+
if (!handoff || portsTaken.has(handoff)) return;
659+
portsTaken.add(handoff);
657660
port = handoff;
658661
host.removeEventListener("message", onWindowMessage);
659662
port.onmessage = onReply;

0 commit comments

Comments
 (0)