@@ -277,8 +277,10 @@ export class Api {
277
277
const awareness = new awarenessProtocol . Awareness ( ydoc )
278
278
awareness . setLocalState ( null ) // we don't want to propagate awareness state
279
279
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 } )
280
283
ydoc . transact ( ( ) => {
281
- if ( docstate ) { Y . applyUpdateV2 ( ydoc , docstate . doc ) }
282
284
docMessages ?. messages . forEach ( m => {
283
285
const decoder = decoding . createDecoder ( m )
284
286
switch ( decoding . readVarUint ( decoder ) ) {
@@ -296,7 +298,13 @@ export class Api {
296
298
} )
297
299
} )
298
300
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
+ }
300
308
}
301
309
302
310
/**
@@ -339,9 +347,12 @@ export class Api {
339
347
} else {
340
348
reclaimCounts ++
341
349
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 )
343
351
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` )
345
356
await promise . all ( [
346
357
storeReferences ? this . store . deleteReferences ( room , docid , storeReferences ) : promise . resolve ( ) ,
347
358
this . redis . multi ( )
0 commit comments