Skip to content

Commit 76c2772

Browse files
Make MaybeDocument an abstract class (#1153)
1 parent 4625e84 commit 76c2772

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

packages/firestore/src/api/database.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,11 @@ export class Transaction implements firestore.Transaction {
594594
const doc = docs[0];
595595
if (doc instanceof NoDocument) {
596596
return new DocumentSnapshot(this._firestore, ref._key, null, false);
597+
} else if (doc instanceof Document) {
598+
return new DocumentSnapshot(this._firestore, ref._key, doc, false);
599+
} else {
600+
fail('MaybeDocument is neither Document nor NoDocument');
597601
}
598-
return new DocumentSnapshot(this._firestore, ref._key, doc, false);
599602
});
600603
}
601604

packages/firestore/src/model/document.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,32 @@ export interface DocumentOptions {
2626
hasLocalMutations: boolean;
2727
}
2828

29-
export class Document {
29+
/**
30+
* The result of a lookup for a given path may be an existing document or a
31+
* marker that this document does not exist at a given version.
32+
*/
33+
export abstract class MaybeDocument {
34+
constructor(readonly key: DocumentKey, readonly version: SnapshotVersion) {}
35+
36+
static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
37+
return DocumentKey.comparator(d1.key, d2.key);
38+
}
39+
}
40+
41+
/**
42+
* Represents a document in Firestore with a key, version, data and whether the
43+
* data has local mutations applied to it.
44+
*/
45+
export class Document extends MaybeDocument {
3046
readonly hasLocalMutations: boolean;
3147

3248
constructor(
33-
readonly key: DocumentKey,
34-
readonly version: SnapshotVersion,
49+
key: DocumentKey,
50+
version: SnapshotVersion,
3551
readonly data: ObjectValue,
3652
options: DocumentOptions
3753
) {
54+
super(key, version);
3855
this.hasLocalMutations = options.hasLocalMutations;
3956
}
4057

@@ -68,10 +85,6 @@ export class Document {
6885
);
6986
}
7087

71-
static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
72-
return DocumentKey.comparator(d1.key, d2.key);
73-
}
74-
7588
static compareByField(field: FieldPath, d1: Document, d2: Document): number {
7689
const v1 = d1.field(field);
7790
const v2 = d2.field(field);
@@ -88,8 +101,10 @@ export class Document {
88101
* Version is set to 0 if we don't point to any specific time, otherwise it
89102
* denotes time we know it didn't exist at.
90103
*/
91-
export class NoDocument {
92-
constructor(readonly key: DocumentKey, readonly version: SnapshotVersion) {}
104+
export class NoDocument extends MaybeDocument {
105+
constructor(key: DocumentKey, version: SnapshotVersion) {
106+
super(key, version);
107+
}
93108

94109
toString(): string {
95110
return `NoDocument(${this.key}, ${this.version})`;
@@ -102,14 +117,4 @@ export class NoDocument {
102117
other.key.isEqual(this.key)
103118
);
104119
}
105-
106-
static compareByKey(d1: MaybeDocument, d2: MaybeDocument): number {
107-
return DocumentKey.comparator(d1.key, d2.key);
108-
}
109120
}
110-
111-
/**
112-
* A union type representing either a full document or a deleted document.
113-
* The NoDocument is used when it doesn't exist on the server.
114-
*/
115-
export type MaybeDocument = Document | NoDocument;

packages/firestore/src/remote/watch_change.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class DocumentWatchChange {
6161
* The new document or NoDocument if it was deleted. Is null if the
6262
* document went out of view without the server sending a new document.
6363
*/
64-
public newDoc: Document | NoDocument | null
64+
public newDoc: MaybeDocument | null
6565
) {}
6666
}
6767

packages/firestore/test/util/helpers.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,7 @@ export function updateMapping(
317317
modifiedDocuments = modifiedDocuments.add(k);
318318
});
319319
removed.forEach(docOrKey => {
320-
const k =
321-
docOrKey instanceof Document || docOrKey instanceof NoDocument
322-
? docOrKey.key
323-
: key(docOrKey);
320+
const k = docOrKey instanceof MaybeDocument ? docOrKey.key : key(docOrKey);
324321
removedDocuments = removedDocuments.add(k);
325322
});
326323

0 commit comments

Comments
 (0)