Skip to content

Commit 2847792

Browse files
committed
transaction.ts: simplify the logic by going back to using SnapshotVersion.min() to represent a deleted document rather than null
1 parent f76ffb7 commit 2847792

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

packages/firestore/src/core/transaction.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,13 @@ import { Code, FirestoreError } from '../util/error';
3434

3535
import { SnapshotVersion } from './snapshot_version';
3636

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-
5037
/**
5138
* Internal transaction object responsible for accumulating the mutations to
5239
* perform and the base versions for any documents read.
5340
*/
5441
export class Transaction {
5542
// 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>();
5744
private mutations: Mutation[] = [];
5845
private committed = false;
5946

@@ -128,20 +115,20 @@ export class Transaction {
128115
}
129116

130117
private recordVersion(doc: Document): void {
131-
let docVersion: SnapshotVersion | null;
118+
let docVersion: SnapshotVersion;
132119

133120
if (doc.isFoundDocument()) {
134121
docVersion = doc.version;
135122
} 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();
138125
} else {
139126
throw fail('Document in a transaction was a ' + doc.constructor.name);
140127
}
141128

142129
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)) {
145132
// This transaction will fail no matter what.
146133
throw new FirestoreError(
147134
Code.ABORTED,
@@ -159,8 +146,8 @@ export class Transaction {
159146
*/
160147
private precondition(key: DocumentKey): Precondition {
161148
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())) {
164151
return Precondition.exists(false);
165152
} else {
166153
return Precondition.updateTime(version);
@@ -177,8 +164,8 @@ export class Transaction {
177164
const version = this.readVersions.get(key.toString());
178165
// The first time a document is written, we want to take into account the
179166
// 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())) {
182169
// The document doesn't exist, so fail the transaction.
183170

184171
// This has to be validated locally because you can't send a

0 commit comments

Comments
 (0)