@@ -773,7 +773,7 @@ function populateDocumentChangeBuffer(
773
773
function shouldPersistTargetData (
774
774
oldTargetData : TargetData ,
775
775
newTargetData : TargetData ,
776
- change : TargetChange
776
+ change : TargetChange | null
777
777
) : boolean {
778
778
// Always persist target data if we don't already have a resume token.
779
779
if ( oldTargetData . resumeToken . approximateByteSize ( ) === 0 ) {
@@ -792,11 +792,21 @@ function shouldPersistTargetData(
792
792
return true ;
793
793
}
794
794
795
+ // Update the target cache if sufficient time has passed since the last
796
+ // LastLimboFreeSnapshotVersion
797
+ const limboFreeTimeDelta =
798
+ newTargetData . lastLimboFreeSnapshotVersion . toMicroseconds ( ) -
799
+ oldTargetData . lastLimboFreeSnapshotVersion . toMicroseconds ( ) ;
800
+ if ( limboFreeTimeDelta >= RESUME_TOKEN_MAX_AGE_MICROS ) {
801
+ return true ;
802
+ }
803
+
795
804
// Otherwise if the only thing that has changed about a target is its resume
796
805
// token it's not worth persisting. Note that the RemoteStore keeps an
797
806
// in-memory view of the currently active targets which includes the current
798
807
// resume token, so stream failure or user changes will still use an
799
808
// up-to-date resume token regardless of what we do here.
809
+ if ( change === null ) return false ;
800
810
const changes =
801
811
change . addedDocuments . size +
802
812
change . modifiedDocuments . size +
@@ -828,17 +838,56 @@ export async function localStoreNotifyLocalViewChanges(
828
838
viewChange . targetId ,
829
839
key
830
840
)
831
- ) . next ( ( ) =>
832
- PersistencePromise . forEach (
833
- viewChange . removedKeys ,
834
- ( key : DocumentKey ) =>
835
- localStoreImpl . persistence . referenceDelegate . removeReference (
836
- txn ,
837
- viewChange . targetId ,
838
- key
839
- )
841
+ )
842
+ . next ( ( ) =>
843
+ PersistencePromise . forEach (
844
+ viewChange . removedKeys ,
845
+ ( key : DocumentKey ) =>
846
+ localStoreImpl . persistence . referenceDelegate . removeReference (
847
+ txn ,
848
+ viewChange . targetId ,
849
+ key
850
+ )
851
+ )
840
852
)
841
- ) ;
853
+ . next ( ( ) => {
854
+ const targetId = viewChange . targetId ;
855
+
856
+ if ( ! viewChange . fromCache ) {
857
+ const targetData =
858
+ localStoreImpl . targetDataByTarget . get ( targetId ) ;
859
+ debugAssert (
860
+ targetData !== null ,
861
+ `Can't set limbo-free snapshot version for unknown target: ${ targetId } `
862
+ ) ;
863
+
864
+ // Advance the last limbo free snapshot version
865
+ const lastLimboFreeSnapshotVersion =
866
+ targetData ! . snapshotVersion ;
867
+ const updatedTargetData =
868
+ targetData ! . withLastLimboFreeSnapshotVersion (
869
+ lastLimboFreeSnapshotVersion
870
+ ) ;
871
+ localStoreImpl . targetDataByTarget =
872
+ localStoreImpl . targetDataByTarget . insert (
873
+ targetId ,
874
+ updatedTargetData
875
+ ) ;
876
+
877
+ if (
878
+ shouldPersistTargetData (
879
+ targetData ! ,
880
+ updatedTargetData ,
881
+ null
882
+ )
883
+ ) {
884
+ localStoreImpl . targetCache . updateTargetData (
885
+ txn ,
886
+ updatedTargetData
887
+ ) ;
888
+ }
889
+ }
890
+ } ) ;
842
891
}
843
892
) ;
844
893
}
@@ -854,26 +903,6 @@ export async function localStoreNotifyLocalViewChanges(
854
903
throw e ;
855
904
}
856
905
}
857
-
858
- for ( const viewChange of viewChanges ) {
859
- const targetId = viewChange . targetId ;
860
-
861
- if ( ! viewChange . fromCache ) {
862
- const targetData = localStoreImpl . targetDataByTarget . get ( targetId ) ;
863
- debugAssert (
864
- targetData !== null ,
865
- `Can't set limbo-free snapshot version for unknown target: ${ targetId } `
866
- ) ;
867
-
868
- // Advance the last limbo free snapshot version
869
- const lastLimboFreeSnapshotVersion = targetData . snapshotVersion ;
870
- const updatedTargetData = targetData . withLastLimboFreeSnapshotVersion (
871
- lastLimboFreeSnapshotVersion
872
- ) ;
873
- localStoreImpl . targetDataByTarget =
874
- localStoreImpl . targetDataByTarget . insert ( targetId , updatedTargetData ) ;
875
- }
876
- }
877
906
}
878
907
879
908
/**
0 commit comments