@@ -295,6 +295,10 @@ 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 > ;
300+ /** When the last copy or cut key was acknowledged; the next one starts after it. */
301+ copyKeyedUntil : number ;
298302 readonly createdAt : number ;
299303 usedAt : number ;
300304 readonly viewers : Set < ViewerState > ;
@@ -427,14 +431,15 @@ const CLIPBOARD_WORLD = "t3-preview-clipboard";
427431// Chromium shares its clipboard across contexts; capture real copy/cut keys in a page-inaccessible world instead.
428432const CLIPBOARD_SCRIPT = `(() => {
429433 let keying = false;
430- let copied = "";
434+ // One entry per copy or cut key, oldest first, so a later key cannot overwrite an unread copy.
435+ const copies = [];
431436 // The same Ctrl or Cmd + C or X that editingCommand sends as copy or cut.
432437 addEventListener("keydown", (event) => {
433438 if (!event.isTrusted || event.altKey || event.ctrlKey === event.metaKey) return;
434439 const key = event.key.toLowerCase();
435440 if (key !== "c" && key !== "x") return;
436441 keying = true;
437- copied = "" ;
442+ copies.push({ at: Date.now(), text: "" }) ;
438443 setTimeout(() => { keying = false; });
439444 }, true);
440445 const selection = () => {
@@ -445,7 +450,8 @@ const CLIPBOARD_SCRIPT = `(() => {
445450 return String(document.getSelection() ?? "");
446451 };
447452 const record = (text) => {
448- copied = String(text ?? "").slice(0, ${ CLIPBOARD_TEXT_LIMIT } );
453+ const copy = copies.at(-1);
454+ if (copy) copy.text = String(text ?? "").slice(0, ${ CLIPBOARD_TEXT_LIMIT } );
449455 };
450456 // Capturing at window runs before page handlers can stop the event, and
451457 // before cut removes the selection.
@@ -460,10 +466,10 @@ const CLIPBOARD_SCRIPT = `(() => {
460466 addEventListener(type, snapshot, true);
461467 addEventListener(type, replace);
462468 }
463- globalThis.takeCopy = () => {
464- const text = copied;
465- copied = "" ;
466- return text;
469+ // A key's copy was keyed between its dispatch and acknowledgement; older copies are stale.
470+ globalThis.takeCopy = (since, until) => {
471+ while (copies.length > 0 && copies[0].at < since) copies.shift() ;
472+ return copies[0] && copies[0].at <= until ? copies.shift(). text : "" ;
467473 };
468474})();` ;
469475
@@ -867,6 +873,8 @@ const make = Effect.gen(function* () {
867873 page,
868874 cdp,
869875 frameSessions : new Map ( ) ,
876+ copyReads : Promise . resolve ( ) ,
877+ copyKeyedUntil : 0 ,
870878 createdAt : Date . now ( ) ,
871879 usedAt : Date . now ( ) ,
872880 viewers : new Set ( ) ,
@@ -1310,7 +1318,7 @@ const make = Effect.gen(function* () {
13101318 await recordCopies ( cdp ) . catch ( constVoid ) ;
13111319 } ;
13121320
1313- const takeSessionCopies = async ( cdp : CDPSession ) => {
1321+ const takeSessionCopies = async ( cdp : CDPSession , since : number , until : number ) => {
13141322 const tree = await cdp . send ( "Page.getFrameTree" ) . catch ( ( ) => null ) ;
13151323 if ( ! tree ) return [ ] ;
13161324 const frameIds = ( node : typeof tree . frameTree ) : Array < string > => [
@@ -1324,7 +1332,7 @@ const make = Effect.gen(function* () {
13241332 . then ( ( { executionContextId } ) =>
13251333 cdp . send ( "Runtime.evaluate" , {
13261334 contextId : executionContextId ,
1327- expression : " globalThis.takeCopy?.()" ,
1335+ expression : ` globalThis.takeCopy?.(${ since } , ${ until } )` ,
13281336 returnByValue : true ,
13291337 } ) ,
13301338 )
@@ -1336,9 +1344,9 @@ const make = Effect.gen(function* () {
13361344 ) ;
13371345 } ;
13381346
1339- const takeCopiedText = async ( tab : ServerTab ) => {
1347+ const takeCopiedText = async ( tab : ServerTab , since : number , until : number ) => {
13401348 const copies = await Promise . all (
1341- [ tab . cdp , ...tab . frameSessions . values ( ) ] . map ( takeSessionCopies ) ,
1349+ [ tab . cdp , ...tab . frameSessions . values ( ) ] . map ( ( cdp ) => takeSessionCopies ( cdp , since , until ) ) ,
13421350 ) ;
13431351 return copies . flat ( ) . find ( ( text ) => text . length > 0 ) ;
13441352 } ;
@@ -2447,7 +2455,10 @@ const make = Effect.gen(function* () {
24472455 const copying =
24482456 ! tab . desktop && ( command ?. commands [ 0 ] === "copy" || command ?. commands [ 0 ] === "cut" ) ;
24492457
2450- if ( copying ) await takeCopiedText ( tab ) ;
2458+ // Copy keys get disjoint millisecond windows so each frame's copy belongs to one key.
2459+ if ( copying )
2460+ while ( Date . now ( ) <= tab . copyKeyedUntil ) await sleepUntil ( tab . copyKeyedUntil + 1 ) ;
2461+ const since = Date . now ( ) ;
24512462 await session . send ( "Input.dispatchKeyEvent" , {
24522463 type : text ? "keyDown" : "rawKeyDown" ,
24532464 key,
@@ -2461,11 +2472,16 @@ const make = Effect.gen(function* () {
24612472 ) ,
24622473 } ) ;
24632474 viewer . pressedKeys . set ( code || key , { key, code } ) ;
2464- if ( copying ) {
2465- const copied = await takeCopiedText ( tab ) ;
2475+ if ( ! copying ) return ;
2476+ const until = Date . now ( ) ;
2477+ tab . copyKeyedUntil = until ;
2478+ // A busy frame answers late; its read must not hold later input.
2479+ const read = tab . copyReads . then ( async ( ) => {
2480+ const copied = await takeCopiedText ( tab , since , until ) ;
24662481 if ( copied ) viewer . push ( { _tag : "clipboard" , text : copied } ) ;
2467- }
2468- return ;
2482+ } ) ;
2483+ tab . copyReads = read ;
2484+ return ( ) => read ;
24692485 }
24702486 case "text" :
24712487 if ( typeof message . text === "string" && message . text . length > 0 ) {
@@ -2721,8 +2737,9 @@ const make = Effect.gen(function* () {
27212737 : { } ) ,
27222738 } ) ;
27232739 } else {
2724- const dispatch = ( input : Record < string , unknown > ) =>
2725- dispatchViewerInput ( tab , session , viewer , input ) ;
2740+ const dispatch = async ( input : Record < string , unknown > ) => {
2741+ await dispatchViewerInput ( tab , session , viewer , input ) ;
2742+ } ;
27262743 if (
27272744 message . type === "mouse" &&
27282745 message . action === "move" &&
@@ -2749,7 +2766,10 @@ const make = Effect.gen(function* () {
27492766 } ) ,
27502767 ) ;
27512768 } else {
2752- await tab . control . human ( viewer . id , ( ) => dispatch ( message ) ) ;
2769+ const copyRead = await tab . control . human ( viewer . id , ( ) =>
2770+ dispatchViewerInput ( tab , session , viewer , message ) ,
2771+ ) ;
2772+ await copyRead ?.( ) ;
27532773 }
27542774 }
27552775 } catch {
0 commit comments