mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-19 11:26:02 +00:00
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
This commit is contained in:
@@ -14,6 +14,16 @@ function fakeHost() {
|
||||
return { host: host as unknown as Window, deliver: (e: Partial<MessageEvent>) => listener?.(e) };
|
||||
}
|
||||
|
||||
function hostReachingEveryListenerOfADispatch() {
|
||||
const listeners = new Set<Listener>();
|
||||
const host = {
|
||||
addEventListener: (_type: string, fn: Listener) => listeners.add(fn),
|
||||
removeEventListener: (_type: string, fn: Listener) => listeners.delete(fn),
|
||||
};
|
||||
const deliver = (e: Partial<MessageEvent>) => [...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();
|
||||
|
||||
@@ -588,6 +588,8 @@ function readHandoff(
|
||||
const onlyPort = (ports: readonly MessagePort[]): MessagePort | null =>
|
||||
ports.length === 1 ? (ports[0] ?? null) : null;
|
||||
|
||||
const portsTaken = new WeakSet<MessagePort>();
|
||||
|
||||
type Pending = {
|
||||
resolve: (result: unknown) => void;
|
||||
reject: (error: EditingPortError) => void;
|
||||
@@ -653,7 +655,8 @@ export function createEditingPortClient(options: {
|
||||
const onWindowMessage = (event: MessageEvent<unknown>) => {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user