@@ -34,26 +34,13 @@ import { Code, FirestoreError } from '../util/error';
34
34
35
35
import { SnapshotVersion } from './snapshot_version' ;
36
36
37
- function nullableSnapshotVersionsEqual (
38
- v1 : SnapshotVersion | null ,
39
- v2 : SnapshotVersion | null
40
- ) : boolean {
41
- if ( v1 === v2 ) {
42
- return true ;
43
- } else if ( v1 === null || v2 === null ) {
44
- return false ;
45
- } else {
46
- return v1 . isEqual ( v2 ) ;
47
- }
48
- }
49
-
50
37
/**
51
38
* Internal transaction object responsible for accumulating the mutations to
52
39
* perform and the base versions for any documents read.
53
40
*/
54
41
export class Transaction {
55
42
// The version of each document that was read during this transaction.
56
- private readVersions = new Map < /* path */ string , SnapshotVersion | null > ( ) ;
43
+ private readVersions = new Map < /* path */ string , SnapshotVersion > ( ) ;
57
44
private mutations : Mutation [ ] = [ ] ;
58
45
private committed = false ;
59
46
@@ -128,20 +115,20 @@ export class Transaction {
128
115
}
129
116
130
117
private recordVersion ( doc : Document ) : void {
131
- let docVersion : SnapshotVersion | null ;
118
+ let docVersion : SnapshotVersion ;
132
119
133
120
if ( doc . isFoundDocument ( ) ) {
134
121
docVersion = doc . version ;
135
122
} else if ( doc . isNoDocument ( ) ) {
136
- // For deleted docs, we must use {exists: false} when we overwrite them .
137
- docVersion = null ;
123
+ // Represent a deleted doc using SnapshotVersion.min() .
124
+ docVersion = SnapshotVersion . min ( ) ;
138
125
} else {
139
126
throw fail ( 'Document in a transaction was a ' + doc . constructor . name ) ;
140
127
}
141
128
142
129
const existingVersion = this . readVersions . get ( doc . key . toString ( ) ) ;
143
- if ( existingVersion !== undefined ) {
144
- if ( ! nullableSnapshotVersionsEqual ( existingVersion , docVersion ) ) {
130
+ if ( existingVersion ) {
131
+ if ( ! docVersion . isEqual ( existingVersion ) ) {
145
132
// This transaction will fail no matter what.
146
133
throw new FirestoreError (
147
134
Code . ABORTED ,
@@ -159,8 +146,8 @@ export class Transaction {
159
146
*/
160
147
private precondition ( key : DocumentKey ) : Precondition {
161
148
const version = this . readVersions . get ( key . toString ( ) ) ;
162
- if ( ! this . writtenDocs . has ( key . toString ( ) ) && version !== undefined ) {
163
- if ( version == null ) {
149
+ if ( ! this . writtenDocs . has ( key . toString ( ) ) && version ) {
150
+ if ( version . isEqual ( SnapshotVersion . min ( ) ) ) {
164
151
return Precondition . exists ( false ) ;
165
152
} else {
166
153
return Precondition . updateTime ( version ) ;
@@ -177,8 +164,8 @@ export class Transaction {
177
164
const version = this . readVersions . get ( key . toString ( ) ) ;
178
165
// The first time a document is written, we want to take into account the
179
166
// read time and existence
180
- if ( ! this . writtenDocs . has ( key . toString ( ) ) && version !== undefined ) {
181
- if ( version === null ) {
167
+ if ( ! this . writtenDocs . has ( key . toString ( ) ) && version ) {
168
+ if ( version . isEqual ( SnapshotVersion . min ( ) ) ) {
182
169
// The document doesn't exist, so fail the transaction.
183
170
184
171
// This has to be validated locally because you can't send a
0 commit comments