@@ -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 ,
@@ -154,6 +154,7 @@ import {
154
154
155
155
import { makeDatabaseInfo } from '../../lite/src/api/database' ;
156
156
import { DEFAULT_HOST } from '../../lite/src/api/components' ;
157
+ import * as exp from '../../exp/index' ;
157
158
158
159
/**
159
160
* Constant used to indicate the LRU garbage collection should be disabled.
@@ -399,7 +400,11 @@ export class Firestore
399
400
400
401
batch ( ) : PublicWriteBatch {
401
402
ensureFirestoreConfigured ( this . _delegate ) ;
402
- return new WriteBatch ( this ) ;
403
+ return new WriteBatch (
404
+ new ExpWriteBatch ( this . _delegate , mutations =>
405
+ executeWrite ( this . _delegate , mutations )
406
+ )
407
+ ) ;
403
408
}
404
409
}
405
410
@@ -590,141 +595,62 @@ export class Transaction implements PublicTransaction {
590
595
}
591
596
}
592
597
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
-
598
+ export class WriteBatch
599
+ extends Compat < exp . WriteBatch >
600
+ implements PublicWriteBatch {
602
601
set < T > (
603
602
documentRef : DocumentReference < T > ,
604
- data : Partial < T > ,
605
- options : PublicSetOptions
606
- ) : WriteBatch ;
607
- set < T > ( documentRef : DocumentReference < T > , data : T ) : WriteBatch ;
608
- set < T > (
609
- documentRef : PublicDocumentReference < T > ,
610
- value : T | Partial < T > ,
603
+ data : T ,
611
604
options ?: PublicSetOptions
612
605
) : 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
- ) ;
606
+ if ( options ) {
607
+ validateSetOptions ( 'WriteBatch.set' , options ) ;
608
+ this . _delegate . set ( documentRef . _delegate , data , options ) ;
609
+ } else {
610
+ this . _delegate . set ( documentRef . _delegate , data ) ;
611
+ }
636
612
return this ;
637
613
}
638
614
639
615
update (
640
- documentRef : PublicDocumentReference < unknown > ,
641
- value : PublicUpdateData
616
+ documentRef : DocumentReference < unknown > ,
617
+ data : PublicUpdateData
642
618
) : WriteBatch ;
643
619
update (
644
- documentRef : PublicDocumentReference < unknown > ,
620
+ documentRef : DocumentReference < unknown > ,
645
621
field : string | PublicFieldPath ,
646
622
value : unknown ,
647
623
...moreFieldsAndValues : unknown [ ]
648
624
) : WriteBatch ;
649
625
update (
650
- documentRef : PublicDocumentReference < unknown > ,
651
- fieldOrUpdateData : string | PublicFieldPath | PublicUpdateData ,
626
+ documentRef : DocumentReference < unknown > ,
627
+ dataOrField : unknown ,
652
628
value ?: unknown ,
653
629
...moreFieldsAndValues : unknown [ ]
654
630
) : 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
631
+ if ( arguments . length === 2 ) {
632
+ this . _delegate . update (
633
+ documentRef . _delegate ,
634
+ dataOrField as PublicUpdateData
680
635
) ;
681
636
} else {
682
- parsed = parseUpdateData (
683
- this . _dataReader ,
684
- 'WriteBatch.update' ,
685
- ref . _key ,
686
- fieldOrUpdateData
637
+ this . _delegate . update (
638
+ documentRef . _delegate ,
639
+ dataOrField as string | ExpFieldPath ,
640
+ value ,
641
+ ... moreFieldsAndValues
687
642
) ;
688
643
}
689
-
690
- this . _mutations = this . _mutations . concat (
691
- parsed . toMutations ( ref . _key , Precondition . exists ( true ) )
692
- ) ;
693
644
return this ;
694
645
}
695
646
696
- 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
- ) ;
647
+ delete ( documentRef : DocumentReference < unknown > ) : WriteBatch {
648
+ this . _delegate . delete ( documentRef . _delegate ) ;
706
649
return this ;
707
650
}
708
651
709
652
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
- }
653
+ return this . _delegate . commit ( ) ;
728
654
}
729
655
}
730
656
0 commit comments