Skip to content

Remove dependency on valueCompare from Document #3024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/firestore/src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

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

import { Document } from '../model/document';
import { compareDocumentsByField, Document } from '../model/document';
import { DocumentKey } from '../model/document_key';
import {
canonicalId,
Expand Down Expand Up @@ -808,8 +808,8 @@ export class OrderBy {

compare(d1: Document, d2: Document): number {
const comparison = this.isKeyOrderBy
? Document.compareByKey(d1, d2)
: Document.compareByField(this.field, d1, d2);
? DocumentKey.comparator(d1.key, d2.key)
: compareDocumentsByField(this.field, d1, d2);
switch (this.dir) {
case Direction.ASCENDING:
return comparison;
Expand Down
28 changes: 16 additions & 12 deletions packages/firestore/src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ export interface DocumentOptions {
export abstract class MaybeDocument {
constructor(readonly key: DocumentKey, readonly version: SnapshotVersion) {}

static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
return DocumentKey.comparator(d1.key, d2.key);
}

/**
* Whether this document had a local mutation applied that has not yet been
* acknowledged by Watch.
Expand Down Expand Up @@ -107,15 +103,23 @@ export class Document extends MaybeDocument {
get hasPendingWrites(): boolean {
return this.hasLocalMutations || this.hasCommittedMutations;
}
}

static compareByField(field: FieldPath, d1: Document, d2: Document): number {
const v1 = d1.field(field);
const v2 = d2.field(field);
if (v1 !== null && v2 !== null) {
return valueCompare(v1, v2);
} else {
return fail("Trying to compare documents on fields that don't exist");
}
/**
* Compares the value for field `field` in the provided documents. Throws if
* the field does not exist in both documents.
*/
export function compareDocumentsByField(
field: FieldPath,
d1: Document,
d2: Document
): number {
const v1 = d1.field(field);
const v2 = d2.field(field);
if (v1 !== null && v2 !== null) {
return valueCompare(v1, v2);
} else {
return fail("Trying to compare documents on fields that don't exist");
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/unit/model/document_set.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('DocumentSet', () => {
});

it('adds and deletes elements', () => {
const set = new DocumentSet(Document.compareByKey)
const set = new DocumentSet() // Compares by key by default
.add(d1)
.add(d2)
.add(d3)
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/test/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
maybeDocumentMap
} from '../../src/model/collections';
import {
compareDocumentsByField,
Document,
DocumentOptions,
MaybeDocument,
Expand Down Expand Up @@ -640,7 +641,7 @@ export function documentSetAsArray(docs: DocumentSet): Document[] {
export class DocComparator {
static byField(...fields: string[]): DocumentComparator {
const path = new FieldPath(fields);
return Document.compareByField.bind(this, path);
return (doc1, doc2) => compareDocumentsByField(path, doc1, doc2);
}
}

Expand Down