@@ -731,41 +731,40 @@ export class LocalStore {
731
731
* Notify local store of the changed views to locally pin documents.
732
732
*/
733
733
notifyLocalViewChanges ( viewChanges : LocalViewChanges [ ] ) : Promise < void > {
734
+ for ( const viewChange of viewChanges ) {
735
+ const targetId = viewChange . targetId ;
736
+
737
+ this . localViewReferences . addReferences ( viewChange . addedKeys , targetId ) ;
738
+ this . localViewReferences . removeReferences (
739
+ viewChange . removedKeys ,
740
+ targetId
741
+ ) ;
742
+
743
+ if ( ! viewChange . fromCache ) {
744
+ const queryData = this . queryDataByTarget . get ( targetId ) ;
745
+ assert (
746
+ queryData !== null ,
747
+ `Can't set limbo-free snapshot version for unknown target: ${ targetId } `
748
+ ) ;
749
+
750
+ // Advance the last limbo free snapshot version
751
+ const lastLimboFreeSnapshotVersion = queryData ! . snapshotVersion ;
752
+ const updatedQueryData = queryData ! . withLastLimboFreeSnapshotVersion (
753
+ lastLimboFreeSnapshotVersion
754
+ ) ;
755
+ this . queryDataByTarget = this . queryDataByTarget . insert (
756
+ targetId ,
757
+ updatedQueryData
758
+ ) ;
759
+ }
760
+ }
734
761
return this . persistence . runTransaction (
735
762
'notifyLocalViewChanges' ,
736
- 'readwrite' ,
763
+ 'readwrite-idempotent ' ,
737
764
txn => {
738
765
return PersistencePromise . forEach (
739
766
viewChanges ,
740
767
( viewChange : LocalViewChanges ) => {
741
- const targetId = viewChange . targetId ;
742
-
743
- this . localViewReferences . addReferences (
744
- viewChange . addedKeys ,
745
- targetId
746
- ) ;
747
- this . localViewReferences . removeReferences (
748
- viewChange . removedKeys ,
749
- targetId
750
- ) ;
751
-
752
- if ( ! viewChange . fromCache ) {
753
- const queryData = this . queryDataByTarget . get ( targetId ) ;
754
- assert (
755
- queryData !== null ,
756
- `Can't set limbo-free snapshot version for unknown target: ${ targetId } `
757
- ) ;
758
-
759
- // Advance the last limbo free snapshot version
760
- const lastLimboFreeSnapshotVersion = queryData ! . snapshotVersion ;
761
- const updatedQueryData = queryData ! . withLastLimboFreeSnapshotVersion (
762
- lastLimboFreeSnapshotVersion
763
- ) ;
764
- this . queryDataByTarget = this . queryDataByTarget . insert (
765
- targetId ,
766
- updatedQueryData
767
- ) ;
768
- }
769
768
return PersistencePromise . forEach (
770
769
viewChange . removedKeys ,
771
770
( key : DocumentKey ) =>
@@ -819,10 +818,8 @@ export class LocalStore {
819
818
* the store can be used to manage its view.
820
819
*/
821
820
allocateQuery ( query : Query ) : Promise < QueryData > {
822
- return this . persistence . runTransaction (
823
- 'Allocate query' ,
824
- 'readwrite' ,
825
- txn => {
821
+ return this . persistence
822
+ . runTransaction ( 'Allocate query' , 'readwrite-idempotent' , txn => {
826
823
let queryData : QueryData ;
827
824
return this . queryCache
828
825
. getQueryData ( txn , query )
@@ -832,7 +829,7 @@ export class LocalStore {
832
829
// previous targetID.
833
830
// TODO(mcg): freshen last accessed date?
834
831
queryData = cached ;
835
- return PersistencePromise . resolve ( ) ;
832
+ return PersistencePromise . resolve ( queryData ) ;
836
833
} else {
837
834
return this . queryCache . allocateTargetId ( txn ) . next ( targetId => {
838
835
queryData = new QueryData (
@@ -841,24 +838,25 @@ export class LocalStore {
841
838
QueryPurpose . Listen ,
842
839
txn . currentSequenceNumber
843
840
) ;
844
- return this . queryCache . addQueryData ( txn , queryData ) ;
841
+ return this . queryCache
842
+ . addQueryData ( txn , queryData )
843
+ . next ( ( ) => queryData ) ;
845
844
} ) ;
846
845
}
847
- } )
848
- . next ( ( ) => {
849
- assert (
850
- this . queryDataByTarget . get ( queryData . targetId ) === null ,
851
- 'Tried to allocate an already allocated query: ' + query
852
- ) ;
853
- this . queryDataByTarget = this . queryDataByTarget . insert (
854
- queryData . targetId ,
855
- queryData
856
- ) ;
857
- this . targetIdByQuery . set ( query , queryData . targetId ) ;
858
- return queryData ;
859
846
} ) ;
860
- }
861
- ) ;
847
+ } )
848
+ . then ( queryData => {
849
+ assert (
850
+ this . queryDataByTarget . get ( queryData . targetId ) === null ,
851
+ 'Tried to allocate an already allocated query: ' + query
852
+ ) ;
853
+ this . queryDataByTarget = this . queryDataByTarget . insert (
854
+ queryData . targetId ,
855
+ queryData
856
+ ) ;
857
+ this . targetIdByQuery . set ( query , queryData . targetId ) ;
858
+ return queryData ;
859
+ } ) ;
862
860
}
863
861
864
862
/**
0 commit comments