Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/studio/src/components/editor/previewDrafts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ describe("gesture drafts on an opaque frame", () => {
expect(sent).toEqual([[expect.objectContaining({ translate: { x: -50, y: -20 }, size })]]);
});

it("keeps what each draw in one task set: a later field wins, and a size drawn earlier stays", async () => {
it("keeps what each draw before one send set: a later field wins, and a size drawn earlier stays", async () => {
const element = await standIn(sealedPreviewFrame().iframe, "#card");
vi.mocked(previewRequest).mockClear();
drawPreviewDraft(element, { size: null });
Expand All @@ -217,7 +217,7 @@ describe("gesture drafts on an opaque frame", () => {
]);
});

it("sends a draw waiting in this task before a save that carries its end", async () => {
it("sends a draw still waiting for its microtask before a save that carries its end", async () => {
const iframe = sealedPreviewFrame().iframe;
const element = await standIn(iframe, "#card");
const member = dragMember(element);
Expand Down
16 changes: 8 additions & 8 deletions packages/studio/src/components/editor/previewDrafts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* A gesture on a preview frame Studio cannot script edits the selection's stand-in element. What the
* gesture draws on the stand-in is drawn on the frame's own element too, over the frame's channel,
* so the preview follows the pointer. One request is in flight per frame; draws in one task, or waiting, merge.
* so the preview follows the pointer. One request is in flight per frame; draws until a microtask, or waiting, merge.
*/
import {
MAX_DESCRIBE_HANDLES,
Expand All @@ -25,7 +25,7 @@ interface Queue {
drawn: Map<string, Entry>;
sending: boolean;
sent: Array<() => void>;
sendsAtTaskEnd: boolean;
sendQueued: boolean;
}

const standIns = new WeakMap<Element, { iframe: HTMLIFrameElement; handle: string }>();
Expand All @@ -51,7 +51,7 @@ export function previewDraftLive(standIn: Element): boolean {
function queueFor(iframe: HTMLIFrameElement): Queue {
let queue = queues.get(iframe);
if (!queue) {
queue = { ops: [], drawn: new Map(), sending: false, sent: [], sendsAtTaskEnd: false };
queue = { ops: [], drawn: new Map(), sending: false, sent: [], sendQueued: false };
queues.set(iframe, queue);
}
return queue;
Expand All @@ -78,11 +78,11 @@ function send(iframe: HTMLIFrameElement, queue: Queue): void {
});
}

function sendAtTaskEnd(iframe: HTMLIFrameElement, queue: Queue): void {
if (queue.sendsAtTaskEnd) return;
queue.sendsAtTaskEnd = true;
function sendOnMicrotask(iframe: HTMLIFrameElement, queue: Queue): void {
if (queue.sendQueued) return;
queue.sendQueued = true;
queueMicrotask(() => {
queue.sendsAtTaskEnd = false;
queue.sendQueued = false;
send(iframe, queue);
});
}
Expand All @@ -107,7 +107,7 @@ export function drawPreviewDraft(element: Element, part: Partial<Drawn>): void {
const waiting = queue.ops[last];
if (waiting && !("end" in waiting)) queue.ops[last] = op;
else queue.ops.push(op);
sendAtTaskEnd(target.iframe, queue);
sendOnMicrotask(target.iframe, queue);
}

/** A gesture's commit started: the save that patches `selector` may carry this draft's end. */
Expand Down
Loading