Skip to content

Commit 71bdf7e

Browse files
Remove cyclic references
1 parent f23120e commit 71bdf7e

36 files changed

+161
-198
lines changed

packages/firestore/src/api/database.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ export class WriteBatch implements firestore.WriteBatch {
836836
convertedValue
837837
);
838838
this._mutations = this._mutations.concat(
839-
parsed.toMutations(ref._key, Precondition.NONE)
839+
parsed.toMutations(ref._key, Precondition.none())
840840
);
841841
return this;
842842
}
@@ -906,7 +906,7 @@ export class WriteBatch implements firestore.WriteBatch {
906906
this._firestore
907907
);
908908
this._mutations = this._mutations.concat(
909-
new DeleteMutation(ref._key, Precondition.NONE)
909+
new DeleteMutation(ref._key, Precondition.none())
910910
);
911911
return this;
912912
}
@@ -1031,7 +1031,7 @@ export class DocumentReference<T = firestore.DocumentData>
10311031
)
10321032
: this.firestore._dataReader.parseSetData(functionName, convertedValue);
10331033
return this._firestoreClient.write(
1034-
parsed.toMutations(this._key, Precondition.NONE)
1034+
parsed.toMutations(this._key, Precondition.none())
10351035
);
10361036
}
10371037

@@ -1075,7 +1075,7 @@ export class DocumentReference<T = firestore.DocumentData>
10751075
delete(): Promise<void> {
10761076
validateExactNumberOfArgs('DocumentReference.delete', arguments, 0);
10771077
return this._firestoreClient.write([
1078-
new DeleteMutation(this._key, Precondition.NONE)
1078+
new DeleteMutation(this._key, Precondition.none())
10791079
]);
10801080
}
10811081

@@ -1460,7 +1460,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14601460

14611461
let fieldValue: api.Value;
14621462
const fieldPath = fieldPathFromArgument('Query.where', field);
1463-
const operator = Operator.fromString(opStr);
1463+
const operator = opStr as Operator;
14641464
if (fieldPath.isKeyField()) {
14651465
if (
14661466
operator === Operator.ARRAY_CONTAINS ||
@@ -1492,7 +1492,7 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
14921492
'Query.where',
14931493
value,
14941494
// We only allow nested arrays for IN queries.
1495-
/** allowArrays = */ operator === Operator.IN ? true : false
1495+
/** allowArrays = */ operator === Operator.IN
14961496
);
14971497
}
14981498
const filter = FieldFilter.create(fieldPath, operator, fieldValue);

packages/firestore/src/core/query.ts

Lines changed: 21 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -80,28 +80,27 @@ export class Query {
8080

8181
get orderBy(): OrderBy[] {
8282
if (this.memoizedOrderBy === null) {
83+
this.memoizedOrderBy = [];
84+
8385
const inequalityField = this.getInequalityFilterField();
8486
const firstOrderByField = this.getFirstOrderByField();
8587
if (inequalityField !== null && firstOrderByField === null) {
8688
// In order to implicitly add key ordering, we must also add the
8789
// inequality filter field for it to be a valid query.
8890
// Note that the default inequality field and key ordering is ascending.
89-
if (inequalityField.isKeyField()) {
90-
this.memoizedOrderBy = [KEY_ORDERING_ASC];
91-
} else {
92-
this.memoizedOrderBy = [
93-
new OrderBy(inequalityField),
94-
KEY_ORDERING_ASC
95-
];
91+
if (!inequalityField.isKeyField()) {
92+
this.memoizedOrderBy.push(new OrderBy(inequalityField));
9693
}
94+
this.memoizedOrderBy.push(
95+
new OrderBy(FieldPath.keyField(), Direction.ASCENDING)
96+
);
9797
} else {
9898
debugAssert(
9999
inequalityField === null ||
100100
(firstOrderByField !== null &&
101101
inequalityField.isEqual(firstOrderByField)),
102102
'First orderBy should match inequality field.'
103103
);
104-
this.memoizedOrderBy = [];
105104
let foundKeyOrdering = false;
106105
for (const orderBy of this.explicitOrderBy) {
107106
this.memoizedOrderBy.push(orderBy);
@@ -117,9 +116,7 @@ export class Query {
117116
? this.explicitOrderBy[this.explicitOrderBy.length - 1].dir
118117
: Direction.ASCENDING;
119118
this.memoizedOrderBy.push(
120-
lastDirection === Direction.ASCENDING
121-
? KEY_ORDERING_ASC
122-
: KEY_ORDERING_DESC
119+
new OrderBy(FieldPath.keyField(), lastDirection)
123120
);
124121
}
125122
}
@@ -468,48 +465,15 @@ export abstract class Filter {
468465
abstract isEqual(filter: Filter): boolean;
469466
}
470467

471-
export class Operator {
472-
static LESS_THAN = new Operator('<');
473-
static LESS_THAN_OR_EQUAL = new Operator('<=');
474-
static EQUAL = new Operator('==');
475-
static GREATER_THAN = new Operator('>');
476-
static GREATER_THAN_OR_EQUAL = new Operator('>=');
477-
static ARRAY_CONTAINS = new Operator('array-contains');
478-
static IN = new Operator('in');
479-
static ARRAY_CONTAINS_ANY = new Operator('array-contains-any');
480-
481-
static fromString(op: string): Operator {
482-
switch (op) {
483-
case '<':
484-
return Operator.LESS_THAN;
485-
case '<=':
486-
return Operator.LESS_THAN_OR_EQUAL;
487-
case '==':
488-
return Operator.EQUAL;
489-
case '>=':
490-
return Operator.GREATER_THAN_OR_EQUAL;
491-
case '>':
492-
return Operator.GREATER_THAN;
493-
case 'array-contains':
494-
return Operator.ARRAY_CONTAINS;
495-
case 'in':
496-
return Operator.IN;
497-
case 'array-contains-any':
498-
return Operator.ARRAY_CONTAINS_ANY;
499-
default:
500-
return fail('Unknown FieldFilter operator: ' + op);
501-
}
502-
}
503-
504-
constructor(public name: string) {}
505-
506-
toString(): string {
507-
return this.name;
508-
}
509-
510-
isEqual(other: Operator): boolean {
511-
return this.name === other.name;
512-
}
468+
export const enum Operator {
469+
LESS_THAN = '<',
470+
LESS_THAN_OR_EQUAL = '<=',
471+
EQUAL = '==',
472+
GREATER_THAN = '>',
473+
GREATER_THAN_OR_EQUAL = '>=',
474+
ARRAY_CONTAINS = 'array-contains',
475+
IN = 'in',
476+
ARRAY_CONTAINS_ANY = 'array-contains-any'
513477
}
514478

515479
export class FieldFilter extends Filter {
@@ -635,7 +599,7 @@ export class FieldFilter extends Filter {
635599
isEqual(other: Filter): boolean {
636600
if (other instanceof FieldFilter) {
637601
return (
638-
this.op.isEqual(other.op) &&
602+
this.op === other.op &&
639603
this.field.isEqual(other.field) &&
640604
valueEquals(this.value, other.value)
641605
);
@@ -737,15 +701,9 @@ export class ArrayContainsAnyFilter extends FieldFilter {
737701
/**
738702
* The direction of sorting in an order by.
739703
*/
740-
export class Direction {
741-
static ASCENDING = new Direction('asc');
742-
static DESCENDING = new Direction('desc');
743-
744-
private constructor(public name: string) {}
745-
746-
toString(): string {
747-
return this.name;
748-
}
704+
export const enum Direction {
705+
ASCENDING = 'asc',
706+
DESCENDING = 'desc'
749707
}
750708

751709
/**
@@ -875,9 +833,3 @@ export class OrderBy {
875833
return this.dir === other.dir && this.field.isEqual(other.field);
876834
}
877835
}
878-
879-
const KEY_ORDERING_ASC = new OrderBy(FieldPath.keyField(), Direction.ASCENDING);
880-
const KEY_ORDERING_DESC = new OrderBy(
881-
FieldPath.keyField(),
882-
Direction.DESCENDING
883-
);

packages/firestore/src/core/snapshot_version.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ import { Timestamp } from '../api/timestamp';
2222
* timestamp, such as update_time or read_time.
2323
*/
2424
export class SnapshotVersion {
25-
static readonly MIN = new SnapshotVersion(new Timestamp(0, 0));
26-
2725
static fromTimestamp(value: Timestamp): SnapshotVersion {
2826
return new SnapshotVersion(value);
2927
}
3028

31-
static forDeletedDoc(): SnapshotVersion {
32-
return SnapshotVersion.MIN;
29+
static min(): SnapshotVersion {
30+
return new SnapshotVersion(new Timestamp(0, 0));
3331
}
3432

3533
private constructor(private timestamp: Timestamp) {}

packages/firestore/src/core/sync_engine.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
import { MaybeDocument, NoDocument } from '../model/document';
3434
import { DocumentKey } from '../model/document_key';
3535
import { Mutation } from '../model/mutation';
36-
import { MutationBatchResult, BATCHID_UNKNOWN } from '../model/mutation_batch';
36+
import { BATCHID_UNKNOWN, MutationBatchResult } from '../model/mutation_batch';
3737
import { RemoteEvent, TargetChange } from '../remote/remote_event';
3838
import { RemoteStore } from '../remote/remote_store';
3939
import { RemoteSyncer } from '../remote/remote_syncer';
@@ -52,7 +52,7 @@ import {
5252
} from '../local/shared_client_state_syncer';
5353
import { SortedSet } from '../util/sorted_set';
5454
import { ListenSequence } from './listen_sequence';
55-
import { Query, LimitType } from './query';
55+
import { LimitType, Query } from './query';
5656
import { SnapshotVersion } from './snapshot_version';
5757
import { Target } from './target';
5858
import { TargetIdGenerator } from './target_id_generator';
@@ -504,11 +504,11 @@ export class SyncEngine implements RemoteSyncer {
504504
);
505505
documentUpdates = documentUpdates.insert(
506506
limboKey,
507-
new NoDocument(limboKey, SnapshotVersion.forDeletedDoc())
507+
new NoDocument(limboKey, SnapshotVersion.min())
508508
);
509509
const resolvedLimboDocuments = documentKeySet().add(limboKey);
510510
const event = new RemoteEvent(
511-
SnapshotVersion.MIN,
511+
SnapshotVersion.min(),
512512
/* targetChanges= */ new Map<TargetId, TargetChange>(),
513513
/* targetMismatches= */ new SortedSet<TargetId>(primitiveComparator),
514514
documentUpdates,

packages/firestore/src/core/transaction.ts

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

1818
import { ParsedSetData, ParsedUpdateData } from '../api/user_data_reader';
1919
import { documentVersionMap } from '../model/collections';
20-
import { Document, NoDocument, MaybeDocument } from '../model/document';
20+
import { Document, MaybeDocument, NoDocument } from '../model/document';
2121

2222
import { DocumentKey } from '../model/document_key';
2323
import {
@@ -27,7 +27,7 @@ import {
2727
VerifyMutation
2828
} from '../model/mutation';
2929
import { Datastore } from '../remote/datastore';
30-
import { fail, debugAssert } from '../util/assert';
30+
import { debugAssert, fail } from '../util/assert';
3131
import { Code, FirestoreError } from '../util/error';
3232
import { SnapshotVersion } from './snapshot_version';
3333

@@ -123,7 +123,7 @@ export class Transaction {
123123
docVersion = doc.version;
124124
} else if (doc instanceof NoDocument) {
125125
// For deleted docs, we must use baseVersion 0 when we overwrite them.
126-
docVersion = SnapshotVersion.forDeletedDoc();
126+
docVersion = SnapshotVersion.min();
127127
} else {
128128
throw fail('Document in a transaction was a ' + doc.constructor.name);
129129
}
@@ -151,7 +151,7 @@ export class Transaction {
151151
if (!this.writtenDocs.has(key) && version) {
152152
return Precondition.updateTime(version);
153153
} else {
154-
return Precondition.NONE;
154+
return Precondition.none();
155155
}
156156
}
157157

@@ -163,7 +163,7 @@ export class Transaction {
163163
// The first time a document is written, we want to take into account the
164164
// read time and existence
165165
if (!this.writtenDocs.has(key) && version) {
166-
if (version.isEqual(SnapshotVersion.forDeletedDoc())) {
166+
if (version.isEqual(SnapshotVersion.min())) {
167167
// The document doesn't exist, so fail the transaction.
168168

169169
// This has to be validated locally because you can't send a

packages/firestore/src/local/index_free_query_engine.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export class IndexFreeQueryEngine implements QueryEngine {
7878

7979
// Queries that have never seen a snapshot without limbo free documents
8080
// should also be run as a full collection scan.
81-
if (lastLimboFreeSnapshotVersion.isEqual(SnapshotVersion.MIN)) {
81+
if (lastLimboFreeSnapshotVersion.isEqual(SnapshotVersion.min())) {
8282
return this.executeFullCollectionScan(transaction, query);
8383
}
8484

@@ -204,7 +204,7 @@ export class IndexFreeQueryEngine implements QueryEngine {
204204
return this.localDocumentsView!.getDocumentsMatchingQuery(
205205
transaction,
206206
query,
207-
SnapshotVersion.MIN
207+
SnapshotVersion.min()
208208
);
209209
}
210210
}

packages/firestore/src/local/indexeddb_remote_document_cache.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
252252
const immediateChildrenPathLength = query.path.length + 1;
253253

254254
const iterationOptions: IterateOptions = {};
255-
if (sinceReadTime.isEqual(SnapshotVersion.MIN)) {
255+
if (sinceReadTime.isEqual(SnapshotVersion.min())) {
256256
// Documents are ordered by key, so we can use a prefix scan to narrow
257257
// down the documents we need to match the query against.
258258
const startKey = query.path.toArray();
@@ -330,16 +330,16 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
330330

331331
/**
332332
* Returns the read time of the most recently read document in the cache, or
333-
* SnapshotVersion.MIN if not available.
333+
* SnapshotVersion.min() if not available.
334334
*/
335335
// PORTING NOTE: This is only used for multi-tab synchronization.
336336
getLastReadTime(
337337
transaction: PersistenceTransaction
338338
): PersistencePromise<SnapshotVersion> {
339339
const documentsStore = remoteDocumentsStore(transaction);
340340

341-
// If there are no existing entries, we return SnapshotVersion.MIN.
342-
let readTime = SnapshotVersion.MIN;
341+
// If there are no existing entries, we return SnapshotVersion.min().
342+
let readTime = SnapshotVersion.min();
343343

344344
return documentsStore
345345
.iterate(
@@ -396,7 +396,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
396396
const doc = this.serializer.fromDbRemoteDocument(dbRemoteDoc);
397397
if (
398398
doc instanceof NoDocument &&
399-
doc.version.isEqual(SnapshotVersion.forDeletedDoc())
399+
doc.version.isEqual(SnapshotVersion.min())
400400
) {
401401
// The document is a sentinel removal and should only be used in the
402402
// `getNewDocumentChanges()`.
@@ -453,7 +453,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
453453
);
454454
if (maybeDocument) {
455455
debugAssert(
456-
!this.readTime.isEqual(SnapshotVersion.MIN),
456+
!this.readTime.isEqual(SnapshotVersion.min()),
457457
'Cannot add a document with a read time of zero'
458458
);
459459
const doc = this.documentCache.serializer.toDbRemoteDocument(
@@ -473,7 +473,7 @@ export class IndexedDbRemoteDocumentCache implements RemoteDocumentCache {
473473
// with a version of 0 and ignored by `maybeDecodeDocument()` but
474474
// preserved in `getNewDocumentChanges()`.
475475
const deletedDoc = this.documentCache.serializer.toDbRemoteDocument(
476-
new NoDocument(key, SnapshotVersion.forDeletedDoc()),
476+
new NoDocument(key, SnapshotVersion.min()),
477477
this.readTime
478478
);
479479
promises.push(

packages/firestore/src/local/indexeddb_schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ function writeEmptyTargetGlobalEntry(
10111011
const metadata = new DbTargetGlobal(
10121012
/*highestTargetId=*/ 0,
10131013
/*lastListenSequenceNumber=*/ 0,
1014-
SnapshotVersion.MIN.toTimestamp(),
1014+
SnapshotVersion.min().toTimestamp(),
10151015
/*targetCount=*/ 0
10161016
);
10171017
return globalStore.put(DbTargetGlobal.key, metadata);

packages/firestore/src/local/local_documents_view.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class LocalDocumentsView {
134134
docs.forEach((key, maybeDoc) => {
135135
// TODO(http://b/32275378): Don't conflate missing / deleted.
136136
if (!maybeDoc) {
137-
maybeDoc = new NoDocument(key, SnapshotVersion.forDeletedDoc());
137+
maybeDoc = new NoDocument(key, SnapshotVersion.min());
138138
}
139139
results = results.insert(key, maybeDoc);
140140
});
@@ -148,7 +148,7 @@ export class LocalDocumentsView {
148148
*
149149
* @param transaction The persistence transaction.
150150
* @param query The query to match documents against.
151-
* @param sinceReadTime If not set to SnapshotVersion.MIN, return only
151+
* @param sinceReadTime If not set to SnapshotVersion.min(), return only
152152
* documents that have been read since this snapshot version (exclusive).
153153
*/
154154
getDocumentsMatchingQuery(

0 commit comments

Comments
 (0)