@@ -34,6 +34,7 @@ export class CollaborationGateway
3434 private readonly logger = new Logger ( CollaborationGateway . name ) ;
3535 private flushTimeouts = new Map < string , NodeJS . Timeout > ( ) ;
3636 private joiningClients = new Set < string > ( ) ; // join 처리 중인 clientId
37+ private pendingUpdates = new Map < string , { cardsetId : string ; update : number [ ] } [ ] > ( ) ; // join 완료 전 수신된 update 버퍼
3738
3839 constructor (
3940 private readonly yjsDocumentService : YjsDocumentService ,
@@ -84,6 +85,29 @@ export class CollaborationGateway
8485
8586 this . joiningClients . delete ( client . id ) ;
8687 this . logger . log ( `User ${ user . userId } joined cardset ${ cardsetId } ` ) ;
88+
89+ const buffered = this . pendingUpdates . get ( client . id ) ;
90+ if ( buffered && buffered . length > 0 ) {
91+ this . logger . log (
92+ `[버퍼 처리] join 완료 후 밀린 update 처리 - clientId=${ client . id } , count=${ buffered . length } ` ,
93+ ) ;
94+ this . pendingUpdates . delete ( client . id ) ;
95+ for ( const pending of buffered ) {
96+ const updateBuffer = new Uint8Array ( pending . update ) ;
97+ let pendingDoc = await this . yjsDocumentService . loadDocument ( pending . cardsetId ) ;
98+ if ( ! pendingDoc ) pendingDoc = new Y . Doc ( ) ;
99+ Y . applyUpdate ( pendingDoc , updateBuffer ) ;
100+ await this . yjsDocumentService . saveUpdate ( pending . cardsetId , updateBuffer ) ;
101+ const newState = Y . encodeStateAsUpdate ( pendingDoc ) ;
102+ this . server . to ( `cardset:${ pending . cardsetId } ` ) . emit ( 'sync' , {
103+ cardsetId : pending . cardsetId ,
104+ update : newState ,
105+ } ) ;
106+ this . logger . log (
107+ `[버퍼 처리 완료] cardsetId=${ pending . cardsetId } , clientId=${ client . id } ` ,
108+ ) ;
109+ }
110+ }
87111 } catch ( error ) {
88112 this . logger . error ( 'Error joining cardset:' , error ) ;
89113 this . logger . error ( 'Error details:' , {
@@ -94,6 +118,7 @@ export class CollaborationGateway
94118 } ) ;
95119
96120 this . joiningClients . delete ( client . id ) ;
121+ this . pendingUpdates . delete ( client . id ) ;
97122 try {
98123 const emptyDoc = new Y . Doc ( ) ;
99124 const state = Y . encodeStateAsUpdate ( emptyDoc ) ;
@@ -163,9 +188,14 @@ export class CollaborationGateway
163188 ) ;
164189
165190 if ( this . joiningClients . has ( client . id ) ) {
166- this . logger . warn (
167- `[update 무시] join 처리 중 - clientId=${ client . id } , cardsetId=${ cardsetId } ` ,
168- ) ;
191+ if ( update ) {
192+ const buffer = this . pendingUpdates . get ( client . id ) ?? [ ] ;
193+ buffer . push ( { cardsetId, update } ) ;
194+ this . pendingUpdates . set ( client . id , buffer ) ;
195+ this . logger . log (
196+ `[update 버퍼링] join 완료 후 처리 예정 - clientId=${ client . id } , cardsetId=${ cardsetId } , 버퍼 크기=${ buffer . length } ` ,
197+ ) ;
198+ }
169199 return ;
170200 }
171201
0 commit comments