@@ -89,36 +89,28 @@ export class YjsDocumentService implements OnModuleInit, OnModuleDestroy {
8989 }
9090 }
9191
92- async saveUpdate ( cardsetId : string , update : Uint8Array ) : Promise < void > {
92+ async saveUpdate ( cardsetId : string , update : Uint8Array ) : Promise < Uint8Array > {
9393 try {
94- if ( ! this . redisClient ) return ;
94+ if ( ! this . redisClient ) return update ;
9595 const key = `yjs:cardset:${ cardsetId } ` ;
9696 const historyKey = `yjs:cardset:${ cardsetId } :updates` ;
97- const updateBuffer = Buffer . from ( update ) ;
9897
98+ const doc = new Y . Doc ( ) ;
9999 const existingData = await this . redisClient . getBuffer ( key ) ;
100100 if ( existingData ) {
101- const doc = new Y . Doc ( ) ;
102101 Y . applyUpdate ( doc , existingData ) ;
103- Y . applyUpdate ( doc , update ) ;
104- const newState = Y . encodeStateAsUpdate ( doc ) ;
105- this . logger . log (
106- `[saveUpdate] Redis 저장 전 doc 내용 - cardsetId=${ cardsetId } , cards=${ JSON . stringify ( doc . getArray ( 'cards' ) . toJSON ( ) ) } ` ,
107- ) ;
108- await this . redisClient . set ( key , Buffer . from ( newState ) ) ;
109- } else {
110- const doc = new Y . Doc ( ) ;
111- Y . applyUpdate ( doc , update ) ;
112- const state = Y . encodeStateAsUpdate ( doc ) ;
113- this . logger . log (
114- `[saveUpdate] Redis 저장 전 doc 내용 (신규) - cardsetId=${ cardsetId } , cards=${ JSON . stringify ( doc . getArray ( 'cards' ) . toJSON ( ) ) } ` ,
115- ) ;
116- await this . redisClient . set ( key , Buffer . from ( state ) ) ;
117102 }
103+ Y . applyUpdate ( doc , update ) ;
104+ const newState = Y . encodeStateAsUpdate ( doc ) ;
105+ this . logger . log (
106+ `[saveUpdate] Redis 저장 전 doc 내용 - cardsetId=${ cardsetId } , cards=${ JSON . stringify ( doc . getArray ( 'cards' ) . toJSON ( ) ) } ` ,
107+ ) ;
108+ await this . redisClient . set ( key , Buffer . from ( newState ) ) ;
118109 await this . redisClient . expire ( key , 86400 * 7 ) ;
119- await this . redisClient . rpush ( historyKey , updateBuffer . toString ( 'base64' ) ) ;
110+ await this . redisClient . rpush ( historyKey , Buffer . from ( update ) . toString ( 'base64' ) ) ;
120111
121112 this . scheduleMySqlPersistence ( cardsetId ) ;
113+ return newState ;
122114 } catch ( error ) {
123115 this . logger . error (
124116 `Failed to save update for cardset ${ cardsetId } :` ,
0 commit comments