Skip to content

Make MaybeDocument an abstract class #1153

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
Aug 21, 2018
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
5 changes: 4 additions & 1 deletion packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,11 @@ export class Transaction implements firestore.Transaction {
const doc = docs[0];
if (doc instanceof NoDocument) {
return new DocumentSnapshot(this._firestore, ref._key, null, false);
} else if (doc instanceof Document) {
return new DocumentSnapshot(this._firestore, ref._key, doc, false);
} else {
fail('MaybeDocument is neither Document nor NoDocument');
}
return new DocumentSnapshot(this._firestore, ref._key, doc, false);
});
}

Expand Down
43 changes: 24 additions & 19 deletions packages/firestore/src/model/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,32 @@ export interface DocumentOptions {
hasLocalMutations: boolean;
}

export class Document {
/**
* The result of a lookup for a given path may be an existing document or a
* marker that this document does not exist at a given version.
*/
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);
}
}

/**
* Represents a document in Firestore with a key, version, data and whether the
* data has local mutations applied to it.
*/
export class Document extends MaybeDocument {
readonly hasLocalMutations: boolean;

constructor(
readonly key: DocumentKey,
readonly version: SnapshotVersion,
key: DocumentKey,
version: SnapshotVersion,
readonly data: ObjectValue,
options: DocumentOptions
) {
super(key, version);
this.hasLocalMutations = options.hasLocalMutations;
}

Expand Down Expand Up @@ -68,10 +85,6 @@ export class Document {
);
}

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

static compareByField(field: FieldPath, d1: Document, d2: Document): number {
const v1 = d1.field(field);
const v2 = d2.field(field);
Expand All @@ -88,8 +101,10 @@ export class Document {
* Version is set to 0 if we don't point to any specific time, otherwise it
* denotes time we know it didn't exist at.
*/
export class NoDocument {
constructor(readonly key: DocumentKey, readonly version: SnapshotVersion) {}
export class NoDocument extends MaybeDocument {
constructor(key: DocumentKey, version: SnapshotVersion) {
super(key, version);
}

toString(): string {
return `NoDocument(${this.key}, ${this.version})`;
Expand All @@ -102,14 +117,4 @@ export class NoDocument {
other.key.isEqual(this.key)
);
}

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

/**
* A union type representing either a full document or a deleted document.
* The NoDocument is used when it doesn't exist on the server.
*/
export type MaybeDocument = Document | NoDocument;
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/watch_change.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DocumentWatchChange {
* The new document or NoDocument if it was deleted. Is null if the
* document went out of view without the server sending a new document.
*/
public newDoc: Document | NoDocument | null
public newDoc: MaybeDocument | null
) {}
}

Expand Down
5 changes: 1 addition & 4 deletions packages/firestore/test/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,7 @@ export function updateMapping(
modifiedDocuments = modifiedDocuments.add(k);
});
removed.forEach(docOrKey => {
const k =
docOrKey instanceof Document || docOrKey instanceof NoDocument
? docOrKey.key
: key(docOrKey);
const k = docOrKey instanceof MaybeDocument ? docOrKey.key : key(docOrKey);
removedDocuments = removedDocuments.add(k);
});

Expand Down