Skip to content

Commit 8e70e4a

Browse files
Remove namespace import for objUtils (#2807)
1 parent 50de3fb commit 8e70e4a

31 files changed

+350
-401
lines changed

packages/firestore/src/api/database.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ import {
7070
} from '../util/input_validation';
7171
import { logError, setLogLevel, LogLevel, getLogLevel } from '../util/log';
7272
import { AutoId } from '../util/misc';
73-
import * as objUtils from '../util/obj';
7473
import { Deferred, Rejecter, Resolver } from '../util/promise';
7574
import { FieldPath as ExternalFieldPath } from './field_path';
7675

@@ -166,7 +165,7 @@ class FirestoreSettings {
166165
this.host = settings.host;
167166

168167
validateNamedOptionalType('settings', 'boolean', 'ssl', settings.ssl);
169-
this.ssl = objUtils.defaulted(settings.ssl, DEFAULT_SSL);
168+
this.ssl = settings.ssl ?? DEFAULT_SSL;
170169
}
171170
validateOptionNames('settings', settings, [
172171
'host',
@@ -222,10 +221,8 @@ class FirestoreSettings {
222221
Please audit all existing usages of Date when you enable the new
223222
behavior.`);
224223
}
225-
this.timestampsInSnapshots = objUtils.defaulted(
226-
settings.timestampsInSnapshots,
227-
DEFAULT_TIMESTAMPS_IN_SNAPSHOTS
228-
);
224+
this.timestampsInSnapshots =
225+
settings.timestampsInSnapshots ?? DEFAULT_TIMESTAMPS_IN_SNAPSHOTS;
229226

230227
validateNamedOptionalType(
231228
'settings',
@@ -341,9 +338,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
341338
validateExactNumberOfArgs('Firestore.settings', arguments, 1);
342339
validateArgType('Firestore.settings', 'object', 1, settingsLiteral);
343340

344-
if (
345-
objUtils.contains(settingsLiteral as objUtils.Dict<{}>, 'persistence')
346-
) {
341+
if (contains(settingsLiteral, 'persistence')) {
347342
throw new FirestoreError(
348343
Code.INVALID_ARGUMENT,
349344
'"persistence" is now specified with a separate call to ' +
@@ -398,12 +393,10 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
398393
"firestore.enablePersistence() call to use 'synchronizeTabs'."
399394
);
400395
}
401-
synchronizeTabs = objUtils.defaulted(
402-
settings.synchronizeTabs !== undefined
403-
? settings.synchronizeTabs
404-
: settings.experimentalTabSynchronization,
405-
DEFAULT_SYNCHRONIZE_TABS
406-
);
396+
synchronizeTabs =
397+
settings.synchronizeTabs ??
398+
settings.experimentalTabSynchronization ??
399+
DEFAULT_SYNCHRONIZE_TABS;
407400
}
408401

409402
return this.configureClient(this._persistenceProvider, {
@@ -558,15 +551,14 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
558551
}
559552

560553
private static databaseIdFromApp(app: FirebaseApp): DatabaseId {
561-
const options = app.options as objUtils.Dict<{}>;
562-
if (!objUtils.contains(options, 'projectId')) {
554+
if (!contains(app.options, 'projectId')) {
563555
throw new FirestoreError(
564556
Code.INVALID_ARGUMENT,
565557
'"projectId" not provided in firebase.initializeApp.'
566558
);
567559
}
568560

569-
const projectId = options['projectId'];
561+
const projectId = app.options.projectId;
570562
if (!projectId || typeof projectId !== 'string') {
571563
throw new FirestoreError(
572564
Code.INVALID_ARGUMENT,
@@ -2628,6 +2620,10 @@ function applyFirestoreDataConverter<T>(
26282620
return [convertedValue, functionName];
26292621
}
26302622

2623+
function contains(obj: object, key: string): obj is { key : unknown } {
2624+
return Object.prototype.hasOwnProperty.call(obj, key);
2625+
}
2626+
26312627
// Export the classes with a private constructor (it will fail if invoked
26322628
// at runtime). Note that this still allows instanceof checks.
26332629

packages/firestore/src/core/sync_engine.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import {
4545
QueryTargetState,
4646
SharedClientStateSyncer
4747
} from '../local/shared_client_state_syncer';
48-
import * as objUtils from '../util/obj';
4948
import { SortedSet } from '../util/sorted_set';
5049
import { ListenSequence } from './listen_sequence';
5150
import { Query, LimitType } from './query';
@@ -147,7 +146,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
147146
private queryViewsByQuery = new ObjectMap<Query, QueryView>(q =>
148147
q.canonicalId()
149148
);
150-
private queriesByTarget: { [targetId: number]: Query[] } = {};
149+
private queriesByTarget = new Map<TargetId, Query[]>();
151150
/**
152151
* The keys of documents that are in limbo for which we haven't yet started a
153152
* limbo resolution query.
@@ -164,9 +163,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
164163
* Keeps track of the information about an active limbo resolution for each
165164
* active target ID that was started for the purpose of limbo resolution.
166165
*/
167-
private activeLimboResolutionsByTarget: {
168-
[targetId: number]: LimboResolution;
169-
} = {};
166+
private activeLimboResolutionsByTarget = new Map<TargetId, LimboResolution>();
170167
private limboDocumentRefs = new ReferenceSet();
171168
/** Stores user completion handlers, indexed by User and BatchId. */
172169
private mutationUserCallbacks = {} as {
@@ -285,10 +282,11 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
285282

286283
const data = new QueryView(query, targetId, view);
287284
this.queryViewsByQuery.set(query, data);
288-
if (!this.queriesByTarget[targetId]) {
289-
this.queriesByTarget[targetId] = [];
285+
if (this.queriesByTarget.has(targetId)) {
286+
this.queriesByTarget.get(targetId)!.push(query);
287+
} else {
288+
this.queriesByTarget.set(targetId, [query]);
290289
}
291-
this.queriesByTarget[targetId].push(query);
292290
return viewChange.snapshot!;
293291
}
294292

@@ -322,10 +320,11 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
322320

323321
// Only clean up the query view and target if this is the only query mapped
324322
// to the target.
325-
const queries = this.queriesByTarget[queryView.targetId];
323+
const queries = this.queriesByTarget.get(queryView.targetId)!;
326324
if (queries.length > 1) {
327-
this.queriesByTarget[queryView.targetId] = queries.filter(
328-
q => !q.isEqual(query)
325+
this.queriesByTarget.set(
326+
queryView.targetId,
327+
queries.filter(q => !q.isEqual(query))
329328
);
330329
this.queryViewsByQuery.delete(query);
331330
return;
@@ -413,10 +412,10 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
413412
try {
414413
const changes = await this.localStore.applyRemoteEvent(remoteEvent);
415414
// Update `receivedDocument` as appropriate for any limbo targets.
416-
objUtils.forEach(remoteEvent.targetChanges, (targetId, targetChange) => {
417-
const limboResolution = this.activeLimboResolutionsByTarget[
418-
Number(targetId)
419-
];
415+
remoteEvent.targetChanges.forEach((targetChange, targetId) => {
416+
const limboResolution = this.activeLimboResolutionsByTarget.get(
417+
targetId
418+
);
420419
if (limboResolution) {
421420
// Since this is a limbo resolution lookup, it's for a single document
422421
// and it could be added, modified, or removed, but not a combination.
@@ -495,15 +494,15 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
495494
// PORTING NOTE: Multi-tab only.
496495
this.sharedClientState.updateQueryState(targetId, 'rejected', err);
497496

498-
const limboResolution = this.activeLimboResolutionsByTarget[targetId];
497+
const limboResolution = this.activeLimboResolutionsByTarget.get(targetId);
499498
const limboKey = limboResolution && limboResolution.key;
500499
if (limboKey) {
501500
// Since this query failed, we won't want to manually unlisten to it.
502501
// So go ahead and remove it from bookkeeping.
503502
this.activeLimboTargetsByKey = this.activeLimboTargetsByKey.remove(
504503
limboKey
505504
);
506-
delete this.activeLimboResolutionsByTarget[targetId];
505+
this.activeLimboResolutionsByTarget.delete(targetId);
507506
this.pumpEnqueuedLimboResolutions();
508507

509508
// TODO(klimt): We really only should do the following on permission
@@ -523,7 +522,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
523522
const resolvedLimboDocuments = documentKeySet().add(limboKey);
524523
const event = new RemoteEvent(
525524
SnapshotVersion.MIN,
526-
/* targetChanges= */ {},
525+
/* targetChanges= */ new Map<TargetId, TargetChange>(),
527526
/* targetMismatches= */ new SortedSet<TargetId>(primitiveComparator),
528527
documentUpdates,
529528
resolvedLimboDocuments
@@ -720,19 +719,19 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
720719
this.sharedClientState.removeLocalQueryTarget(targetId);
721720

722721
assert(
723-
this.queriesByTarget[targetId] &&
724-
this.queriesByTarget[targetId].length !== 0,
722+
this.queriesByTarget.has(targetId) &&
723+
this.queriesByTarget.get(targetId)!.length !== 0,
725724
`There are no queries mapped to target id ${targetId}`
726725
);
727726

728-
for (const query of this.queriesByTarget[targetId]) {
727+
for (const query of this.queriesByTarget.get(targetId)!) {
729728
this.queryViewsByQuery.delete(query);
730729
if (error) {
731730
this.syncEngineListener!.onWatchError(query, error);
732731
}
733732
}
734733

735-
delete this.queriesByTarget[targetId];
734+
this.queriesByTarget.delete(targetId);
736735

737736
if (this.isPrimary) {
738737
const limboKeys = this.limboDocumentRefs.referencesForId(targetId);
@@ -758,7 +757,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
758757

759758
this.remoteStore.unlisten(limboTargetId);
760759
this.activeLimboTargetsByKey = this.activeLimboTargetsByKey.remove(key);
761-
delete this.activeLimboResolutionsByTarget[limboTargetId];
760+
this.activeLimboResolutionsByTarget.delete(limboTargetId);
762761
this.pumpEnqueuedLimboResolutions();
763762
}
764763

@@ -810,8 +809,9 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
810809
) {
811810
const key = this.enqueuedLimboResolutions.shift()!;
812811
const limboTargetId = this.limboTargetIdGenerator.next();
813-
this.activeLimboResolutionsByTarget[limboTargetId] = new LimboResolution(
814-
key
812+
this.activeLimboResolutionsByTarget.set(
813+
limboTargetId,
814+
new LimboResolution(key)
815815
);
816816
this.activeLimboTargetsByKey = this.activeLimboTargetsByKey.insert(
817817
key,
@@ -868,7 +868,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
868868
})
869869
.then((viewDocChanges: ViewDocumentChanges) => {
870870
const targetChange =
871-
remoteEvent && remoteEvent.targetChanges[queryView.targetId];
871+
remoteEvent && remoteEvent.targetChanges.get(queryView.targetId);
872872
const viewChange = queryView.view.applyChanges(
873873
viewDocChanges,
874874
/* updateLimboDocuments= */ this.isPrimary === true,
@@ -957,7 +957,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
957957
const activeTargets: TargetId[] = [];
958958

959959
let p = Promise.resolve();
960-
objUtils.forEachNumber(this.queriesByTarget, (targetId, _) => {
960+
this.queriesByTarget.forEach((_, targetId) => {
961961
if (this.sharedClientState.isLocalQueryTarget(targetId)) {
962962
activeTargets.push(targetId);
963963
} else {
@@ -981,11 +981,11 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
981981

982982
// PORTING NOTE: Multi-tab only.
983983
private resetLimboDocuments(): void {
984-
objUtils.forEachNumber(this.activeLimboResolutionsByTarget, targetId => {
984+
this.activeLimboResolutionsByTarget.forEach((_, targetId) => {
985985
this.remoteStore.unlisten(targetId);
986986
});
987987
this.limboDocumentRefs.removeAllReferences();
988-
this.activeLimboResolutionsByTarget = [];
988+
this.activeLimboResolutionsByTarget = new Map<TargetId, LimboResolution>();
989989
this.activeLimboTargetsByKey = new SortedMap<DocumentKey, TargetId>(
990990
DocumentKey.comparator
991991
);
@@ -1004,7 +1004,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
10041004
const newViewSnapshots: ViewSnapshot[] = [];
10051005
for (const targetId of targets) {
10061006
let targetData: TargetData;
1007-
const queries = this.queriesByTarget[targetId];
1007+
const queries = this.queriesByTarget.get(targetId);
10081008

10091009
if (queries && queries.length !== 0) {
10101010
// For queries that have a local View, we need to update their state
@@ -1096,7 +1096,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
10961096
return;
10971097
}
10981098

1099-
if (this.queriesByTarget[targetId]) {
1099+
if (this.queriesByTarget.has(targetId)) {
11001100
switch (state) {
11011101
case 'current':
11021102
case 'not-current': {
@@ -1136,7 +1136,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
11361136

11371137
for (const targetId of added) {
11381138
assert(
1139-
!this.queriesByTarget[targetId],
1139+
!this.queriesByTarget.has(targetId),
11401140
'Trying to add an already active target'
11411141
);
11421142
const target = await this.localStore.getTarget(targetId);
@@ -1153,7 +1153,7 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
11531153
for (const targetId of removed) {
11541154
// Check that the target is still active since the target might have been
11551155
// removed if it has been rejected by the backend.
1156-
if (!this.queriesByTarget[targetId]) {
1156+
if (!this.queriesByTarget.has(targetId)) {
11571157
continue;
11581158
}
11591159

@@ -1183,12 +1183,12 @@ export class SyncEngine implements RemoteSyncer, SharedClientStateSyncer {
11831183
}
11841184

11851185
getRemoteKeysForTarget(targetId: TargetId): DocumentKeySet {
1186-
const limboResolution = this.activeLimboResolutionsByTarget[targetId];
1186+
const limboResolution = this.activeLimboResolutionsByTarget.get(targetId);
11871187
if (limboResolution && limboResolution.receivedDocument) {
11881188
return documentKeySet().add(limboResolution.key);
11891189
} else {
11901190
let keySet = documentKeySet();
1191-
const queries = this.queriesByTarget[targetId];
1191+
const queries = this.queriesByTarget.get(targetId);
11921192
if (!queries) {
11931193
return keySet;
11941194
}

packages/firestore/src/local/encoded_resource_path.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2017 Google Inc.
3+
* Copyright 2017 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -72,7 +72,7 @@ const encodedEscape = '\u0011';
7272
/**
7373
* Encodes a resource path into a IndexedDb-compatible string form.
7474
*/
75-
export function encode(path: ResourcePath): EncodedResourcePath {
75+
export function encodeResourcePath(path: ResourcePath): EncodedResourcePath {
7676
let result = '';
7777
for (let i = 0; i < path.length; i++) {
7878
if (result.length > 0) {
@@ -114,7 +114,7 @@ function encodeSeparator(result: string): string {
114114
* decoding resource names from the server; those are One Platform format
115115
* strings.
116116
*/
117-
export function decode(path: EncodedResourcePath): ResourcePath {
117+
export function decodeResourcePath(path: EncodedResourcePath): ResourcePath {
118118
// Event the empty path must encode as a path of at least length 2. A path
119119
// with exactly 2 must be the empty path.
120120
const length = path.length;

packages/firestore/src/local/indexeddb_index_manager.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @license
3-
* Copyright 2019 Google Inc.
3+
* Copyright 2019 Google LLC
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -18,7 +18,10 @@
1818
import { ResourcePath } from '../model/path';
1919
import { assert } from '../util/assert';
2020
import { immediateSuccessor } from '../util/misc';
21-
import { decode, encode } from './encoded_resource_path';
21+
import {
22+
decodeResourcePath,
23+
encodeResourcePath
24+
} from './encoded_resource_path';
2225
import { IndexManager } from './index_manager';
2326
import { IndexedDbPersistence } from './indexeddb_persistence';
2427
import { DbCollectionParent, DbCollectionParentKey } from './indexeddb_schema';
@@ -64,7 +67,7 @@ export class IndexedDbIndexManager implements IndexManager {
6467

6568
const collectionParent: DbCollectionParent = {
6669
collectionId,
67-
parent: encode(parentPath)
70+
parent: encodeResourcePath(parentPath)
6871
};
6972
return collectionParentsStore(transaction).put(collectionParent);
7073
}
@@ -93,7 +96,7 @@ export class IndexedDbIndexManager implements IndexManager {
9396
if (entry.collectionId !== collectionId) {
9497
break;
9598
}
96-
parentPaths.push(decode(entry.parent));
99+
parentPaths.push(decodeResourcePath(entry.parent));
97100
}
98101
return parentPaths;
99102
});

0 commit comments

Comments
 (0)