Skip to content

Commit 8ce3250

Browse files
fix(server): input after a copy key no longer waits for a busy frame
Reading the copy recorder inside the input queue held every key and click after a copy or cut until a busy embedded frame answered. The read now runs after the key leaves the queue, one read at a time in key order. The recorder keeps one entry per copy or cut key instead of a single slot, so a later key cannot overwrite a copy that has not been read yet. Each read drops entries from keys sent before its own key and takes the oldest one left. AI-assisted; verified with the tests and tools listed above.
1 parent 7754818 commit 8ce3250

2 files changed

Lines changed: 37 additions & 19 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,12 +1425,13 @@ it.live("a busy embedded frame does not hold a copy key back from the page", ()
14251425
context.page.emit("framenavigated", frame);
14261426
yield* Effect.promise(() => frame.recording.promise);
14271427

1428-
// The frame answers its copy read only once the page has the key, like a
1429-
// frame busy for longer than the user waits.
1428+
// The frame answers its copy read only once the page has the key and the
1429+
// input after it, like a frame busy for longer than the user waits.
14301430
const keyReached = Promise.withResolvers<void>();
14311431
const pageSend = pageSession.send.getMockImplementation()!;
14321432
pageSession.send.mockImplementation(async (method, input) => {
1433-
if (method === "Input.dispatchKeyEvent") keyReached.resolve();
1433+
if (method === "Input.dispatchKeyEvent" && (input as { type?: string }).type === "keyUp")
1434+
keyReached.resolve();
14341435
return pageSend(method, input);
14351436
});
14361437
const frameSession = context.sessions.at(-1)!;
@@ -1443,11 +1444,19 @@ it.live("a busy embedded frame does not hold a copy key back from the page", ()
14431444
});
14441445

14451446
keyCopy = "copied";
1446-
yield* viewer.input({ type: "key", action: "down", key: "c", code: "KeyC", modifiers: 4 });
1447+
const copying = yield* viewer
1448+
.input({ type: "key", action: "down", key: "c", code: "KeyC", modifiers: 4 })
1449+
.pipe(Effect.forkScoped({ startImmediately: true }));
1450+
yield* viewer.input({ type: "key", action: "up", key: "c", code: "KeyC", modifiers: 4 });
1451+
yield* Fiber.join(copying);
14471452
expect(pageSession.send).toHaveBeenCalledWith(
14481453
"Input.dispatchKeyEvent",
14491454
expect.objectContaining({ key: "c", commands: ["copy"] }),
14501455
);
1456+
const clipboard = yield* Queue.clear(viewer.output);
1457+
expect(clipboard.filter((item) => item._tag === "clipboard")).toEqual([
1458+
{ _tag: "clipboard", text: "copied" },
1459+
]);
14511460
}),
14521461
).pipe(Effect.provide(layer)),
14531462
);

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ interface ServerTab {
295295
readonly page: Page;
296296
readonly cdp: CDPSession;
297297
readonly frameSessions: Map<Frame, CDPSession>;
298+
/** Copy reads run in key order, outside the input queue. */
299+
copyReads: Promise<void>;
298300
readonly createdAt: number;
299301
usedAt: number;
300302
readonly viewers: Set<ViewerState>;
@@ -427,16 +429,15 @@ const CLIPBOARD_WORLD = "t3-preview-clipboard";
427429
// Chromium shares its clipboard across contexts; capture real copy/cut keys in a page-inaccessible world instead.
428430
const CLIPBOARD_SCRIPT = `(() => {
429431
let keying = false;
430-
let keyedAt = 0;
431-
let copied = "";
432+
// One entry per copy or cut key, oldest first, so a later key cannot overwrite an unread copy.
433+
const copies = [];
432434
// The same Ctrl or Cmd + C or X that editingCommand sends as copy or cut.
433435
addEventListener("keydown", (event) => {
434436
if (!event.isTrusted || event.altKey || event.ctrlKey === event.metaKey) return;
435437
const key = event.key.toLowerCase();
436438
if (key !== "c" && key !== "x") return;
437439
keying = true;
438-
keyedAt = Date.now();
439-
copied = "";
440+
copies.push({ at: Date.now(), text: "" });
440441
setTimeout(() => { keying = false; });
441442
}, true);
442443
const selection = () => {
@@ -447,7 +448,8 @@ const CLIPBOARD_SCRIPT = `(() => {
447448
return String(document.getSelection() ?? "");
448449
};
449450
const record = (text) => {
450-
copied = String(text ?? "").slice(0, ${CLIPBOARD_TEXT_LIMIT});
451+
const copy = copies.at(-1);
452+
if (copy) copy.text = String(text ?? "").slice(0, ${CLIPBOARD_TEXT_LIMIT});
451453
};
452454
// Capturing at window runs before page handlers can stop the event, and
453455
// before cut removes the selection.
@@ -462,11 +464,10 @@ const CLIPBOARD_SCRIPT = `(() => {
462464
addEventListener(type, snapshot, true);
463465
addEventListener(type, replace);
464466
}
465-
// Only a key dispatched at or after since made this copy; older ones are stale.
467+
// Copies from keys dispatched before since are stale.
466468
globalThis.takeCopy = (since) => {
467-
const text = keyedAt >= since ? copied : "";
468-
copied = "";
469-
return text;
469+
while (copies.length > 0 && copies[0].at < since) copies.shift();
470+
return copies.shift()?.text ?? "";
470471
};
471472
})();`;
472473

@@ -870,6 +871,7 @@ const make = Effect.gen(function* () {
870871
page,
871872
cdp,
872873
frameSessions: new Map(),
874+
copyReads: Promise.resolve(),
873875
createdAt: Date.now(),
874876
usedAt: Date.now(),
875877
viewers: new Set(),
@@ -2464,11 +2466,14 @@ const make = Effect.gen(function* () {
24642466
),
24652467
});
24662468
viewer.pressedKeys.set(code || key, { key, code });
2467-
if (copying) {
2469+
if (!copying) return;
2470+
// A busy frame answers late; its read must not hold later input.
2471+
const read = tab.copyReads.then(async () => {
24682472
const copied = await takeCopiedText(tab, since);
24692473
if (copied) viewer.push({ _tag: "clipboard", text: copied });
2470-
}
2471-
return;
2474+
});
2475+
tab.copyReads = read;
2476+
return () => read;
24722477
}
24732478
case "text":
24742479
if (typeof message.text === "string" && message.text.length > 0) {
@@ -2724,8 +2729,9 @@ const make = Effect.gen(function* () {
27242729
: {}),
27252730
});
27262731
} else {
2727-
const dispatch = (input: Record<string, unknown>) =>
2728-
dispatchViewerInput(tab, session, viewer, input);
2732+
const dispatch = async (input: Record<string, unknown>) => {
2733+
await dispatchViewerInput(tab, session, viewer, input);
2734+
};
27292735
if (
27302736
message.type === "mouse" &&
27312737
message.action === "move" &&
@@ -2752,7 +2758,10 @@ const make = Effect.gen(function* () {
27522758
}),
27532759
);
27542760
} else {
2755-
await tab.control.human(viewer.id, () => dispatch(message));
2761+
const copyRead = await tab.control.human(viewer.id, () =>
2762+
dispatchViewerInput(tab, session, viewer, message),
2763+
);
2764+
await copyRead?.();
27562765
}
27572766
}
27582767
} catch {

0 commit comments

Comments
 (0)