@@ -458,6 +458,40 @@ export class LocalStore {
458
458
}
459
459
) ;
460
460
461
+ let changedDocKeys = documentKeySet ( ) ;
462
+ remoteEvent . documentUpdates . forEach ( ( key , doc ) => {
463
+ changedDocKeys = changedDocKeys . add ( key ) ;
464
+ promises . push (
465
+ documentBuffer . getEntry ( txn , key ) . next ( existingDoc => {
466
+ // Make sure we don't apply an old document version to the remote
467
+ // cache, though we make an exception for SnapshotVersion.MIN which
468
+ // can happen for manufactured events (e.g. in the case of a limbo
469
+ // document resolution failing).
470
+ if (
471
+ existingDoc == null ||
472
+ doc . version . isEqual ( SnapshotVersion . MIN ) ||
473
+ doc . version . compareTo ( existingDoc . version ) >= 0
474
+ ) {
475
+ documentBuffer . addEntry ( doc ) ;
476
+ } else {
477
+ log . debug (
478
+ LOG_TAG ,
479
+ 'Ignoring outdated watch update for ' ,
480
+ key ,
481
+ '. Current version:' ,
482
+ existingDoc . version ,
483
+ ' Watch version:' ,
484
+ doc . version
485
+ ) ;
486
+ }
487
+
488
+ // The document might be garbage because it was unreferenced by
489
+ // everything. Make sure to mark it as garbage if it is...
490
+ this . garbageCollector . addPotentialGarbageKey ( key ) ;
491
+ } )
492
+ ) ;
493
+ } ) ;
494
+
461
495
// HACK: The only reason we allow a null snapshot version is so that we
462
496
// can synthesize remote events when we get permission denied errors while
463
497
// trying to resolve the state of a locally cached document that is in
@@ -477,13 +511,6 @@ export class LocalStore {
477
511
) ;
478
512
}
479
513
480
- let changedDocKeys = documentKeySet ( ) ;
481
- remoteEvent . documentUpdates . forEach ( ( key , doc ) => {
482
- changedDocKeys = changedDocKeys . add ( key ) ;
483
- documentBuffer . addEntry ( doc ) ;
484
- this . garbageCollector . addPotentialGarbageKey ( key ) ;
485
- } ) ;
486
-
487
514
let releasedWriteKeys : DocumentKeySet ;
488
515
return PersistencePromise . waitFor ( promises )
489
516
. next ( ( ) => this . releaseHeldBatchResults ( txn , documentBuffer ) )
0 commit comments