Skip to content

Commit 9a21ad8

Browse files
committed
transaction.ts: refactor snapshot version comparison logic
1 parent 1fe7fde commit 9a21ad8

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

packages/firestore/src/core/transaction.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ 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+
3750
/**
3851
* Internal transaction object responsible for accumulating the mutations to
3952
* perform and the base versions for any documents read.
@@ -127,12 +140,8 @@ export class Transaction {
127140
}
128141

129142
const existingVersion = this.readVersions.get(doc.key.toString());
130-
if (existingVersion !== undefined && existingVersion !== docVersion) {
131-
if (
132-
docVersion == null ||
133-
existingVersion == null ||
134-
!docVersion.isEqual(existingVersion)
135-
) {
143+
if (existingVersion !== undefined) {
144+
if (!nullableSnapshotVersionsEqual(existingVersion, docVersion)) {
136145
// This transaction will fail no matter what.
137146
throw new FirestoreError(
138147
Code.ABORTED,

0 commit comments

Comments
 (0)