@@ -544,6 +544,9 @@ export async function pushTargetStateToYjs(target) {
544544 ) ;
545545 await Promise . all ( [ ...costumePromises , ...soundPromises ] ) ;
546546
547+ if ( ! constants . mutableRefs . ydoc || ! constants . mutableRefs . sharedBlocks ) return ;
548+ if ( ! runtime . getTargetById ( targetId ) ) return ;
549+
547550 constants . mutableRefs . ydoc . transact ( ( ) => {
548551 const yTargetBlockMap = new Y . Map ( ) ;
549552 yTargetBlockMap . set ( '__targetName' , targetName ) ;
@@ -582,46 +585,26 @@ export async function pushTargetStateToYjs(target) {
582585}
583586
584587function applyYjsBlocksToTarget ( target , yTargetMap ) {
585- const alreadyHere = [ ] ;
588+ const blocks = target . blocks ;
589+ const live = new Set ( ) ;
586590 yTargetMap . forEach ( ( yBlockMap , blockId ) => {
587591 if ( blockId === '__targetName' ) return ;
592+ live . add ( blockId ) ;
588593 const blockData = deserializeBlockFromYjs ( yBlockMap ) ;
589- if ( target . blocks . getBlock ( blockId ) ) {
590- alreadyHere . push ( [ blockId , blockData ] ) ;
591- return ;
594+ const local = blocks . getBlock ( blockId ) ;
595+ if ( local ) {
596+ Object . assign ( local , blockData ) ;
597+ blocks . updateBlock ( local ) ;
598+ } else {
599+ blocks . createBlock ( blockData ) ;
592600 }
593- target . blocks . createBlock ( blockData ) ;
594601 } ) ;
595- let adopted = 0 ;
596- alreadyHere . forEach ( ( [ blockId , blockData ] ) => {
597- adopted += adoptShadowIdsFromYjs ( target , blockId , blockData ) ;
602+ const stale = Object . keys ( blocks . _blocks ) . filter ( id => ! live . has ( id ) ) ;
603+ stale . forEach ( id => {
604+ blocks . _deleteScript ( id ) ;
605+ delete blocks . _blocks [ id ] ;
598606 } ) ;
599- if ( adopted > 0 ) target . blocks . resetCache ( ) ;
600- }
601-
602- function adoptShadowIdsFromYjs ( target , blockId , remote ) {
603- const local = target . blocks . getBlock ( blockId ) ;
604- if ( ! local || ! local . inputs || ! remote || ! remote . inputs ) return 0 ;
605-
606- let adopted = 0 ;
607- Object . keys ( remote . inputs ) . forEach ( name => {
608- const mine = local . inputs [ name ] ;
609- const theirs = remote . inputs [ name ] ;
610- if ( ! mine || ! theirs ) return ;
611- if ( mine . shadow === theirs . shadow ) return ;
612- if ( ! mine . shadow || mine . block !== mine . shadow ) return ;
613- if ( ! theirs . shadow || theirs . block !== theirs . shadow ) return ;
614-
615- const roomsShadow = target . blocks . getBlock ( theirs . shadow ) ;
616- if ( ! roomsShadow ) return ;
617-
618- const invented = mine . shadow ;
619- local . inputs [ name ] = { name, block : theirs . shadow , shadow : theirs . shadow } ;
620- roomsShadow . parent = blockId ;
621- target . blocks . deleteBlock ( invented ) ;
622- adopted ++ ;
623- } ) ;
624- return adopted ;
607+ if ( stale . length > 0 ) blocks . resetCache ( ) ;
625608}
626609
627610function applyYjsVariablesToTarget ( target , yTargetVarMap ) {
@@ -663,6 +646,13 @@ function applyYjsVariablesToTarget(target, yTargetVarMap) {
663646}
664647
665648function applyYjsCommentsToTarget ( target , yTargetCommentMap ) {
649+ Object . keys ( target . comments ) . forEach ( commentId => {
650+ if ( yTargetCommentMap . has ( commentId ) ) return ;
651+ const comment = target . comments [ commentId ] ;
652+ const owner = comment && comment . blockId ? target . blocks . getBlock ( comment . blockId ) : null ;
653+ if ( owner && owner . comment === commentId ) delete owner . comment ;
654+ delete target . comments [ commentId ] ;
655+ } ) ;
666656 yTargetCommentMap . forEach ( ( yCommentMap , commentId ) => {
667657 if ( target . comments [ commentId ] ) return ;
668658 const commentData = deserializeCommentFromYjs ( yCommentMap ) ;
0 commit comments