@@ -277,8 +277,10 @@ export class Api {
277277 const awareness = new awarenessProtocol . Awareness ( ydoc )
278278 awareness . setLocalState ( null ) // we don't want to propagate awareness state
279279 const now = performance . now ( )
280+ if ( docstate ) { Y . applyUpdateV2 ( ydoc , docstate . doc ) }
281+ let changed = false
282+ ydoc . once ( 'afterTransaction' , ( tr ) => { changed = tr . changed . size > 0 } )
280283 ydoc . transact ( ( ) => {
281- if ( docstate ) { Y . applyUpdateV2 ( ydoc , docstate . doc ) }
282284 docMessages ?. messages . forEach ( m => {
283285 const decoder = decoding . createDecoder ( m )
284286 switch ( decoding . readVarUint ( decoder ) ) {
@@ -296,7 +298,13 @@ export class Api {
296298 } )
297299 } )
298300 logApi ( `took ${ performance . now ( ) - now } ms to process messages for room: ${ room } ` )
299- return { ydoc, awareness, redisLastId : docMessages ?. lastId . toString ( ) || '0' , storeReferences : docstate ?. references || null }
301+ return {
302+ ydoc,
303+ awareness,
304+ redisLastId : docMessages ?. lastId . toString ( ) || '0' ,
305+ storeReferences : docstate ?. references || null ,
306+ changed
307+ }
300308 }
301309
302310 /**
@@ -339,9 +347,12 @@ export class Api {
339347 } else {
340348 reclaimCounts ++
341349 const { room, docid } = decodeRedisRoomStreamName ( task . stream , this . prefix )
342- const { ydoc, storeReferences, redisLastId } = await this . getDoc ( room , docid )
350+ const { ydoc, storeReferences, redisLastId, changed } = await this . getDoc ( room , docid )
343351 const lastId = math . max ( number . parseInt ( redisLastId . split ( '-' ) [ 0 ] ) , number . parseInt ( task . id . split ( '-' ) [ 0 ] ) )
344- await this . store . persistDoc ( room , docid , ydoc )
352+ if ( changed ) {
353+ logWorker ( `persisting changes in room: ${ room } ` )
354+ await this . store . persistDoc ( room , docid , ydoc )
355+ } else logWorker ( `skip persisting room: ${ room } due to no changes` )
345356 await promise . all ( [
346357 storeReferences ? this . store . deleteReferences ( room , docid , storeReferences ) : promise . resolve ( ) ,
347358 this . redis . multi ( )
0 commit comments