Skip to content

Commit 81fbb08

Browse files
Merge 9d6d28d into 8877803
2 parents 8877803 + 9d6d28d commit 81fbb08

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

packages/firestore/src/core/query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import * as api from '../protos/firestore_proto_api';
1919

20-
import { Document } from '../model/document';
20+
import { compareDocumentsByField, Document } from '../model/document';
2121
import { DocumentKey } from '../model/document_key';
2222
import {
2323
canonicalId,
@@ -808,8 +808,8 @@ export class OrderBy {
808808

809809
compare(d1: Document, d2: Document): number {
810810
const comparison = this.isKeyOrderBy
811-
? Document.compareByKey(d1, d2)
812-
: Document.compareByField(this.field, d1, d2);
811+
? DocumentKey.comparator(d1.key, d2.key)
812+
: compareDocumentsByField(this.field, d1, d2);
813813
switch (this.dir) {
814814
case Direction.ASCENDING:
815815
return comparison;

packages/firestore/src/model/document.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ export interface DocumentOptions {
3737
export abstract class MaybeDocument {
3838
constructor(readonly key: DocumentKey, readonly version: SnapshotVersion) {}
3939

40-
static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
41-
return DocumentKey.comparator(d1.key, d2.key);
42-
}
43-
4440
/**
4541
* Whether this document had a local mutation applied that has not yet been
4642
* acknowledged by Watch.
@@ -107,15 +103,23 @@ export class Document extends MaybeDocument {
107103
get hasPendingWrites(): boolean {
108104
return this.hasLocalMutations || this.hasCommittedMutations;
109105
}
106+
}
110107

111-
static compareByField(field: FieldPath, d1: Document, d2: Document): number {
112-
const v1 = d1.field(field);
113-
const v2 = d2.field(field);
114-
if (v1 !== null && v2 !== null) {
115-
return valueCompare(v1, v2);
116-
} else {
117-
return fail("Trying to compare documents on fields that don't exist");
118-
}
108+
/**
109+
* Compares the value for field `field` in the provided documents. Throws if
110+
* the field does not exist in both documents.
111+
*/
112+
export function compareDocumentsByField(
113+
field: FieldPath,
114+
d1: Document,
115+
d2: Document
116+
): number {
117+
const v1 = d1.field(field);
118+
const v2 = d2.field(field);
119+
if (v1 !== null && v2 !== null) {
120+
return valueCompare(v1, v2);
121+
} else {
122+
return fail("Trying to compare documents on fields that don't exist");
119123
}
120124
}
121125

packages/firestore/test/unit/model/document_set.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ describe('DocumentSet', () => {
5454
});
5555

5656
it('adds and deletes elements', () => {
57-
const set = new DocumentSet(Document.compareByKey)
57+
const set = new DocumentSet() // Compares by key by default
5858
.add(d1)
5959
.add(d2)
6060
.add(d3)

packages/firestore/test/util/helpers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
maybeDocumentMap
5252
} from '../../src/model/collections';
5353
import {
54+
compareDocumentsByField,
5455
Document,
5556
DocumentOptions,
5657
MaybeDocument,
@@ -640,7 +641,7 @@ export function documentSetAsArray(docs: DocumentSet): Document[] {
640641
export class DocComparator {
641642
static byField(...fields: string[]): DocumentComparator {
642643
const path = new FieldPath(fields);
643-
return Document.compareByField.bind(this, path);
644+
return (doc1, doc2) => compareDocumentsByField(path, doc1, doc2);
644645
}
645646
}
646647

0 commit comments

Comments
 (0)