@@ -121,10 +121,12 @@ import {
121
121
getDoc ,
122
122
onSnapshot ,
123
123
DocumentReference as ExpDocumentReference ,
124
- Query as ExpQuery
124
+ Query as ExpQuery ,
125
+ executeWrite
125
126
} from '../../exp/src/api/reference' ;
126
127
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector' ;
127
128
import { Compat } from '../compat/compat' ;
129
+ import { WriteBatch as ExpWriteBatch } from '../../exp/src/api/write_batch' ;
128
130
129
131
import {
130
132
CollectionReference as PublicCollectionReference ,
@@ -403,7 +405,11 @@ export class Firestore
403
405
404
406
batch ( ) : PublicWriteBatch {
405
407
ensureFirestoreConfigured ( this . _delegate ) ;
406
- return new WriteBatch ( this ) ;
408
+ return new WriteBatch (
409
+ new ExpWriteBatch ( this . _delegate , mutations =>
410
+ executeWrite ( this . _delegate , mutations )
411
+ )
412
+ ) ;
407
413
}
408
414
}
409
415
@@ -637,15 +643,9 @@ export class Transaction implements PublicTransaction {
637
643
}
638
644
}
639
645
640
- export class WriteBatch implements PublicWriteBatch {
641
- private _mutations = [ ] as Mutation [ ] ;
642
- private _committed = false ;
643
- private _dataReader : UserDataReader ;
644
-
645
- constructor ( private _firestore : Firestore ) {
646
- this . _dataReader = newUserDataReader ( this . _firestore . _delegate ) ;
647
- }
648
-
646
+ export class WriteBatch
647
+ extends Compat < ExpWriteBatch >
648
+ implements PublicWriteBatch {
649
649
set < T > (
650
650
documentRef : DocumentReference < T > ,
651
651
data : Partial < T > ,
@@ -654,38 +654,22 @@ export class WriteBatch implements PublicWriteBatch {
654
654
set < T > ( documentRef : DocumentReference < T > , data : T ) : WriteBatch ;
655
655
set < T > (
656
656
documentRef : PublicDocumentReference < T > ,
657
- value : T | Partial < T > ,
657
+ data : T | Partial < T > ,
658
658
options ?: PublicSetOptions
659
659
) : WriteBatch {
660
- this . verifyNotCommitted ( ) ;
661
- const ref = validateReference (
662
- 'WriteBatch.set' ,
663
- documentRef ,
664
- this . _firestore
665
- ) ;
666
- options = validateSetOptions ( 'WriteBatch.set' , options ) ;
667
- const convertedValue = applyFirestoreDataConverter (
668
- ref . _converter ,
669
- value ,
670
- options
671
- ) ;
672
- const parsed = parseSetData (
673
- this . _dataReader ,
674
- 'WriteBatch.set' ,
675
- ref . _key ,
676
- convertedValue ,
677
- ref . _converter !== null ,
678
- options
679
- ) ;
680
- this . _mutations = this . _mutations . concat (
681
- parsed . toMutations ( ref . _key , Precondition . none ( ) )
682
- ) ;
660
+ const ref = castReference ( documentRef ) ;
661
+ if ( options ) {
662
+ validateSetOptions ( 'WriteBatch.set' , options ) ;
663
+ this . _delegate . set ( ref , data , options ) ;
664
+ } else {
665
+ this . _delegate . set ( ref , data ) ;
666
+ }
683
667
return this ;
684
668
}
685
669
686
670
update (
687
671
documentRef : PublicDocumentReference < unknown > ,
688
- value : PublicUpdateData
672
+ data : PublicUpdateData
689
673
) : WriteBatch ;
690
674
update (
691
675
documentRef : PublicDocumentReference < unknown > ,
@@ -695,83 +679,32 @@ export class WriteBatch implements PublicWriteBatch {
695
679
) : WriteBatch ;
696
680
update (
697
681
documentRef : PublicDocumentReference < unknown > ,
698
- fieldOrUpdateData : string | PublicFieldPath | PublicUpdateData ,
682
+ dataOrField : string | PublicFieldPath | PublicUpdateData ,
699
683
value ?: unknown ,
700
684
...moreFieldsAndValues : unknown [ ]
701
685
) : WriteBatch {
702
- this . verifyNotCommitted ( ) ;
703
- const ref = validateReference (
704
- 'WriteBatch.update' ,
705
- documentRef ,
706
- this . _firestore
707
- ) ;
708
-
709
- // For Compat types, we have to "extract" the underlying types before
710
- // performing validation.
711
- if ( fieldOrUpdateData instanceof Compat ) {
712
- fieldOrUpdateData = ( fieldOrUpdateData as Compat < ExpFieldPath > ) . _delegate ;
713
- }
714
-
715
- let parsed ;
716
- if (
717
- typeof fieldOrUpdateData === 'string' ||
718
- fieldOrUpdateData instanceof ExpFieldPath
719
- ) {
720
- parsed = parseUpdateVarargs (
721
- this . _dataReader ,
722
- 'WriteBatch.update' ,
723
- ref . _key ,
724
- fieldOrUpdateData ,
725
- value ,
726
- moreFieldsAndValues
727
- ) ;
686
+ const ref = castReference ( documentRef ) ;
687
+ if ( arguments . length === 2 ) {
688
+ this . _delegate . update ( ref , dataOrField as PublicUpdateData ) ;
728
689
} else {
729
- parsed = parseUpdateData (
730
- this . _dataReader ,
731
- 'WriteBatch.update' ,
732
- ref . _key ,
733
- fieldOrUpdateData
690
+ this . _delegate . update (
691
+ ref ,
692
+ dataOrField as string | ExpFieldPath ,
693
+ value ,
694
+ ... moreFieldsAndValues
734
695
) ;
735
696
}
736
-
737
- this . _mutations = this . _mutations . concat (
738
- parsed . toMutations ( ref . _key , Precondition . exists ( true ) )
739
- ) ;
740
697
return this ;
741
698
}
742
699
743
700
delete ( documentRef : PublicDocumentReference < unknown > ) : WriteBatch {
744
- this . verifyNotCommitted ( ) ;
745
- const ref = validateReference (
746
- 'WriteBatch.delete' ,
747
- documentRef ,
748
- this . _firestore
749
- ) ;
750
- this . _mutations = this . _mutations . concat (
751
- new DeleteMutation ( ref . _key , Precondition . none ( ) )
752
- ) ;
701
+ const ref = castReference ( documentRef ) ;
702
+ this . _delegate . delete ( ref ) ;
753
703
return this ;
754
704
}
755
705
756
706
commit ( ) : Promise < void > {
757
- this . verifyNotCommitted ( ) ;
758
- this . _committed = true ;
759
- if ( this . _mutations . length > 0 ) {
760
- const client = ensureFirestoreConfigured ( this . _firestore . _delegate ) ;
761
- return firestoreClientWrite ( client , this . _mutations ) ;
762
- }
763
-
764
- return Promise . resolve ( ) ;
765
- }
766
-
767
- private verifyNotCommitted ( ) : void {
768
- if ( this . _committed ) {
769
- throw new FirestoreError (
770
- Code . FAILED_PRECONDITION ,
771
- 'A write batch can no longer be used after commit() ' +
772
- 'has been called.'
773
- ) ;
774
- }
707
+ return this . _delegate . commit ( ) ;
775
708
}
776
709
}
777
710
@@ -2016,6 +1949,15 @@ export class CollectionReference<T = PublicDocumentData>
2016
1949
}
2017
1950
}
2018
1951
1952
+ function castReference < T > (
1953
+ documentRef : PublicDocumentReference < T >
1954
+ ) : ExpDocumentReference < T > {
1955
+ if ( documentRef instanceof Compat ) {
1956
+ documentRef = documentRef . _delegate ;
1957
+ }
1958
+ return cast < ExpDocumentReference < T > > ( documentRef , ExpDocumentReference ) ;
1959
+ }
1960
+
2019
1961
function validateReference < T > (
2020
1962
methodName : string ,
2021
1963
documentRef : PublicDocumentReference < T > ,
0 commit comments