@@ -27,8 +27,7 @@ import {
27
27
firestoreClientGetDocumentsFromLocalCache ,
28
28
firestoreClientGetDocumentsViaSnapshotListener ,
29
29
firestoreClientListen ,
30
- firestoreClientTransaction ,
31
- firestoreClientWrite
30
+ firestoreClientTransaction
32
31
} from '../core/firestore_client' ;
33
32
import {
34
33
Bound ,
@@ -59,7 +58,6 @@ import { Transaction as InternalTransaction } from '../core/transaction';
59
58
import { ChangeType , ViewSnapshot } from '../core/view_snapshot' ;
60
59
import { Document , MaybeDocument , NoDocument } from '../model/document' ;
61
60
import { DocumentKey } from '../model/document_key' ;
62
- import { DeleteMutation , Mutation , Precondition } from '../model/mutation' ;
63
61
import { FieldPath , ResourcePath } from '../model/path' ;
64
62
import { isServerTimestamp } from '../model/server_timestamps' ;
65
63
import { refValue } from '../model/values' ;
@@ -118,11 +116,13 @@ import {
118
116
getDocFromCache ,
119
117
getDocFromServer ,
120
118
getDoc ,
121
- onSnapshot
119
+ onSnapshot ,
120
+ executeWrite
122
121
} from '../../exp/src/api/reference' ;
123
122
import { DocumentSnapshot as ExpDocumentSnapshot } from '../../exp/src/api/snapshot' ;
124
123
import { LRU_COLLECTION_DISABLED } from '../local/lru_garbage_collector' ;
125
124
import { Compat } from '../compat/compat' ;
125
+ import { WriteBatch as ExpWriteBatch } from '../../exp/src/api/write_batch' ;
126
126
127
127
import {
128
128
CollectionReference as PublicCollectionReference ,
@@ -399,7 +399,11 @@ export class Firestore
399
399
400
400
batch ( ) : PublicWriteBatch {
401
401
ensureFirestoreConfigured ( this . _delegate ) ;
402
- return new WriteBatch ( this ) ;
402
+ return new WriteBatch (
403
+ new ExpWriteBatch ( this . _delegate , mutations =>
404
+ executeWrite ( this . _delegate , mutations )
405
+ )
406
+ ) ;
403
407
}
404
408
}
405
409
@@ -590,15 +594,9 @@ export class Transaction implements PublicTransaction {
590
594
}
591
595
}
592
596
593
- export class WriteBatch implements PublicWriteBatch {
594
- private _mutations = [ ] as Mutation [ ] ;
595
- private _committed = false ;
596
- private _dataReader : UserDataReader ;
597
-
598
- constructor ( private _firestore : Firestore ) {
599
- this . _dataReader = newUserDataReader ( this . _firestore . _delegate ) ;
600
- }
601
-
597
+ export class WriteBatch
598
+ extends Compat < ExpWriteBatch >
599
+ implements PublicWriteBatch {
602
600
set < T > (
603
601
documentRef : DocumentReference < T > ,
604
602
data : Partial < T > ,
@@ -607,38 +605,22 @@ export class WriteBatch implements PublicWriteBatch {
607
605
set < T > ( documentRef : DocumentReference < T > , data : T ) : WriteBatch ;
608
606
set < T > (
609
607
documentRef : PublicDocumentReference < T > ,
610
- value : T | Partial < T > ,
608
+ data : T | Partial < T > ,
611
609
options ?: PublicSetOptions
612
610
) : WriteBatch {
613
- this . verifyNotCommitted ( ) ;
614
- const ref = validateReference (
615
- 'WriteBatch.set' ,
616
- documentRef ,
617
- this . _firestore
618
- ) ;
619
- options = validateSetOptions ( 'WriteBatch.set' , options ) ;
620
- const convertedValue = applyFirestoreDataConverter (
621
- ref . _converter ,
622
- value ,
623
- options
624
- ) ;
625
- const parsed = parseSetData (
626
- this . _dataReader ,
627
- 'WriteBatch.set' ,
628
- ref . _key ,
629
- convertedValue ,
630
- ref . _converter !== null ,
631
- options
632
- ) ;
633
- this . _mutations = this . _mutations . concat (
634
- parsed . toMutations ( ref . _key , Precondition . none ( ) )
635
- ) ;
611
+ const ref = castReference ( documentRef ) ;
612
+ if ( options ) {
613
+ validateSetOptions ( 'WriteBatch.set' , options ) ;
614
+ this . _delegate . set ( ref , data , options ) ;
615
+ } else {
616
+ this . _delegate . set ( ref , data ) ;
617
+ }
636
618
return this ;
637
619
}
638
620
639
621
update (
640
622
documentRef : PublicDocumentReference < unknown > ,
641
- value : PublicUpdateData
623
+ data : PublicUpdateData
642
624
) : WriteBatch ;
643
625
update (
644
626
documentRef : PublicDocumentReference < unknown > ,
@@ -648,83 +630,32 @@ export class WriteBatch implements PublicWriteBatch {
648
630
) : WriteBatch ;
649
631
update (
650
632
documentRef : PublicDocumentReference < unknown > ,
651
- fieldOrUpdateData : string | PublicFieldPath | PublicUpdateData ,
633
+ dataOrField : string | PublicFieldPath | PublicUpdateData ,
652
634
value ?: unknown ,
653
635
...moreFieldsAndValues : unknown [ ]
654
636
) : WriteBatch {
655
- this . verifyNotCommitted ( ) ;
656
- const ref = validateReference (
657
- 'WriteBatch.update' ,
658
- documentRef ,
659
- this . _firestore
660
- ) ;
661
-
662
- // For Compat types, we have to "extract" the underlying types before
663
- // performing validation.
664
- if ( fieldOrUpdateData instanceof Compat ) {
665
- fieldOrUpdateData = ( fieldOrUpdateData as Compat < ExpFieldPath > ) . _delegate ;
666
- }
667
-
668
- let parsed ;
669
- if (
670
- typeof fieldOrUpdateData === 'string' ||
671
- fieldOrUpdateData instanceof ExpFieldPath
672
- ) {
673
- parsed = parseUpdateVarargs (
674
- this . _dataReader ,
675
- 'WriteBatch.update' ,
676
- ref . _key ,
677
- fieldOrUpdateData ,
678
- value ,
679
- moreFieldsAndValues
680
- ) ;
637
+ const ref = castReference ( documentRef ) ;
638
+ if ( arguments . length === 2 ) {
639
+ this . _delegate . update ( ref , dataOrField as PublicUpdateData ) ;
681
640
} else {
682
- parsed = parseUpdateData (
683
- this . _dataReader ,
684
- 'WriteBatch.update' ,
685
- ref . _key ,
686
- fieldOrUpdateData
641
+ this . _delegate . update (
642
+ ref ,
643
+ dataOrField as string | ExpFieldPath ,
644
+ value ,
645
+ ... moreFieldsAndValues
687
646
) ;
688
647
}
689
-
690
- this . _mutations = this . _mutations . concat (
691
- parsed . toMutations ( ref . _key , Precondition . exists ( true ) )
692
- ) ;
693
648
return this ;
694
649
}
695
650
696
651
delete ( documentRef : PublicDocumentReference < unknown > ) : WriteBatch {
697
- this . verifyNotCommitted ( ) ;
698
- const ref = validateReference (
699
- 'WriteBatch.delete' ,
700
- documentRef ,
701
- this . _firestore
702
- ) ;
703
- this . _mutations = this . _mutations . concat (
704
- new DeleteMutation ( ref . _key , Precondition . none ( ) )
705
- ) ;
652
+ const ref = castReference ( documentRef ) ;
653
+ this . _delegate . delete ( ref ) ;
706
654
return this ;
707
655
}
708
656
709
657
commit ( ) : Promise < void > {
710
- this . verifyNotCommitted ( ) ;
711
- this . _committed = true ;
712
- if ( this . _mutations . length > 0 ) {
713
- const client = ensureFirestoreConfigured ( this . _firestore . _delegate ) ;
714
- return firestoreClientWrite ( client , this . _mutations ) ;
715
- }
716
-
717
- return Promise . resolve ( ) ;
718
- }
719
-
720
- private verifyNotCommitted ( ) : void {
721
- if ( this . _committed ) {
722
- throw new FirestoreError (
723
- Code . FAILED_PRECONDITION ,
724
- 'A write batch can no longer be used after commit() ' +
725
- 'has been called.'
726
- ) ;
727
- }
658
+ return this . _delegate . commit ( ) ;
728
659
}
729
660
}
730
661
@@ -2030,6 +1961,15 @@ export class CollectionReference<T = PublicDocumentData>
2030
1961
}
2031
1962
}
2032
1963
1964
+ function castReference < T > (
1965
+ documentRef : PublicDocumentReference < T >
1966
+ ) : ExpDocumentReference < T > {
1967
+ if ( documentRef instanceof Compat ) {
1968
+ documentRef = documentRef . _delegate ;
1969
+ }
1970
+ return cast < ExpDocumentReference < T > > ( documentRef , ExpDocumentReference ) ;
1971
+ }
1972
+
2033
1973
function validateReference < T > (
2034
1974
methodName : string ,
2035
1975
documentRef : PublicDocumentReference < T > ,
0 commit comments