@@ -297,6 +297,8 @@ interface ServerTab {
297297 readonly frameSessions : Map < Frame , CDPSession > ;
298298 /** Copy reads run in key order, outside the input queue. */
299299 copyReads : Promise < void > ;
300+ /** When the last copy or cut key was acknowledged; the next one starts after it. */
301+ copyKeyedUntil : number ;
300302 readonly createdAt : number ;
301303 usedAt : number ;
302304 readonly viewers : Set < ViewerState > ;
@@ -464,10 +466,10 @@ const CLIPBOARD_SCRIPT = `(() => {
464466 addEventListener(type, snapshot, true);
465467 addEventListener(type, replace);
466468 }
467- // Copies from keys dispatched before since are stale.
468- globalThis.takeCopy = (since) => {
469+ // A key's copy was keyed between its dispatch and acknowledgement; older copies are stale.
470+ globalThis.takeCopy = (since, until ) => {
469471 while (copies.length > 0 && copies[0].at < since) copies.shift();
470- return copies. shift()? .text ?? "";
472+ return copies[0] && copies[0].at <= until ? copies. shift().text : "";
471473 };
472474})();` ;
473475
@@ -872,6 +874,7 @@ const make = Effect.gen(function* () {
872874 cdp,
873875 frameSessions : new Map ( ) ,
874876 copyReads : Promise . resolve ( ) ,
877+ copyKeyedUntil : 0 ,
875878 createdAt : Date . now ( ) ,
876879 usedAt : Date . now ( ) ,
877880 viewers : new Set ( ) ,
@@ -1315,7 +1318,7 @@ const make = Effect.gen(function* () {
13151318 await recordCopies ( cdp ) . catch ( constVoid ) ;
13161319 } ;
13171320
1318- const takeSessionCopies = async ( cdp : CDPSession , since : number ) => {
1321+ const takeSessionCopies = async ( cdp : CDPSession , since : number , until : number ) => {
13191322 const tree = await cdp . send ( "Page.getFrameTree" ) . catch ( ( ) => null ) ;
13201323 if ( ! tree ) return [ ] ;
13211324 const frameIds = ( node : typeof tree . frameTree ) : Array < string > => [
@@ -1329,7 +1332,7 @@ const make = Effect.gen(function* () {
13291332 . then ( ( { executionContextId } ) =>
13301333 cdp . send ( "Runtime.evaluate" , {
13311334 contextId : executionContextId ,
1332- expression : `globalThis.takeCopy?.(${ since } )` ,
1335+ expression : `globalThis.takeCopy?.(${ since } , ${ until } )` ,
13331336 returnByValue : true ,
13341337 } ) ,
13351338 )
@@ -1341,9 +1344,9 @@ const make = Effect.gen(function* () {
13411344 ) ;
13421345 } ;
13431346
1344- const takeCopiedText = async ( tab : ServerTab , since : number ) => {
1347+ const takeCopiedText = async ( tab : ServerTab , since : number , until : number ) => {
13451348 const copies = await Promise . all (
1346- [ tab . cdp , ...tab . frameSessions . values ( ) ] . map ( ( cdp ) => takeSessionCopies ( cdp , since ) ) ,
1349+ [ tab . cdp , ...tab . frameSessions . values ( ) ] . map ( ( cdp ) => takeSessionCopies ( cdp , since , until ) ) ,
13471350 ) ;
13481351 return copies . flat ( ) . find ( ( text ) => text . length > 0 ) ;
13491352 } ;
@@ -2452,6 +2455,9 @@ const make = Effect.gen(function* () {
24522455 const copying =
24532456 ! tab . desktop && ( command ?. commands [ 0 ] === "copy" || command ?. commands [ 0 ] === "cut" ) ;
24542457
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 ) ;
24552461 const since = Date . now ( ) ;
24562462 await session . send ( "Input.dispatchKeyEvent" , {
24572463 type : text ? "keyDown" : "rawKeyDown" ,
@@ -2467,9 +2473,11 @@ const make = Effect.gen(function* () {
24672473 } ) ;
24682474 viewer . pressedKeys . set ( code || key , { key, code } ) ;
24692475 if ( ! copying ) return ;
2476+ const until = Date . now ( ) ;
2477+ tab . copyKeyedUntil = until ;
24702478 // A busy frame answers late; its read must not hold later input.
24712479 const read = tab . copyReads . then ( async ( ) => {
2472- const copied = await takeCopiedText ( tab , since ) ;
2480+ const copied = await takeCopiedText ( tab , since , until ) ;
24732481 if ( copied ) viewer . push ( { _tag : "clipboard" , text : copied } ) ;
24742482 } ) ;
24752483 tab . copyReads = read ;
0 commit comments