Skip to content

Commit 17f9aa3

Browse files
authored
Remove some object utility functions (#1816)
1 parent 0c4594a commit 17f9aa3

25 files changed

+216
-561
lines changed

config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"moduleResolution": "node",
1111
"sourceMap": true,
1212
"target": "es5",
13+
"downlevelIteration": true,
1314
"typeRoots": ["../node_modules/@types"]
1415
}
1516
}

packages/database/src/api/test_access.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ export const queryIdentifier = function(query: Query) {
8383
return query.queryIdentifier();
8484
};
8585

86-
/**
87-
* @param {!Query} firebaseRef
88-
* @return {!Object}
89-
*/
90-
export const listens = function(firebaseRef: Query) {
91-
return (firebaseRef.repo.persistentConnection_ as any).listens_;
92-
};
93-
9486
/**
9587
* Forces the RepoManager to create Repos that use ReadonlyRestClient instead of PersistentConnection.
9688
*

packages/database/src/core/CompoundWrite.ts

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

1818
import { ImmutableTree } from './util/ImmutableTree';
1919
import { Path } from './util/Path';
20-
import { forEach } from '@firebase/util';
2120
import { Node, NamedNode } from './snap/Node';
2221
import { PRIORITY_INDEX } from './snap/indexes/PriorityIndex';
2322
import { assert } from '@firebase/util';
@@ -70,9 +69,9 @@ export class CompoundWrite {
7069
*/
7170
addWrites(path: Path, updates: { [name: string]: Node }): CompoundWrite {
7271
let newWrite = this as CompoundWrite;
73-
forEach(updates, function(childKey: string, node: Node) {
72+
for (const [childKey, node] of Object.entries(updates)) {
7473
newWrite = newWrite.addWrite(path.child(childKey), node);
75-
});
74+
}
7675
return newWrite;
7776
}
7877

packages/database/src/core/PersistentConnection.ts

Lines changed: 21 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717

1818
import firebase from '@firebase/app';
19-
import { forEach, contains, isEmpty, getCount, safeGet } from '@firebase/util';
19+
import { contains, isEmpty, safeGet } from '@firebase/util';
2020
import { stringify } from '@firebase/util';
2121
import { assert } from '@firebase/util';
2222
import { error, log, logWrapper, warn, ObjectToUniqueKey } from './util/util';
@@ -76,9 +76,8 @@ export class PersistentConnection extends ServerActions {
7676
id = PersistentConnection.nextPersistentConnectionId_++;
7777
private log_ = logWrapper('p:' + this.id + ':');
7878

79-
/** @private {Object} */
8079
private interruptReasons_: { [reason: string]: boolean } = {};
81-
private listens_: { [path: string]: { [queryId: string]: ListenSpec } } = {};
80+
private readonly listens: Map<string, Map<string, ListenSpec>> = new Map();
8281
private outstandingPuts_: OutstandingPut[] = [];
8382
private outstandingPutCount_ = 0;
8483
private onDisconnectRequestQueue_: OnDisconnectRequest[] = [];
@@ -88,26 +87,19 @@ export class PersistentConnection extends ServerActions {
8887
private securityDebugCallback_: ((a: Object) => void) | null = null;
8988
lastSessionId: string | null = null;
9089

91-
/** @private {number|null} */
9290
private establishConnectionTimer_: number | null = null;
9391

94-
/** @private {boolean} */
9592
private visible_: boolean = false;
9693

9794
// Before we get connected, we keep a queue of pending messages to send.
9895
private requestCBHash_: { [k: number]: (a: any) => void } = {};
9996
private requestNumber_ = 0;
10097

101-
/** @private {?{
102-
* sendRequest(Object),
103-
* close()
104-
* }} */
10598
private realtime_: {
10699
sendRequest(a: Object): void;
107100
close(): void;
108101
} | null = null;
109102

110-
/** @private {string|null} */
111103
private authToken_: string | null = null;
112104
private forceTokenRefresh_ = false;
113105
private invalidAuthTokenCount_ = 0;
@@ -116,26 +108,17 @@ export class PersistentConnection extends ServerActions {
116108
private lastConnectionAttemptTime_: number | null = null;
117109
private lastConnectionEstablishedTime_: number | null = null;
118110

119-
/**
120-
* @private
121-
*/
122111
private static nextPersistentConnectionId_ = 0;
123112

124113
/**
125114
* Counter for number of connections created. Mainly used for tagging in the logs
126-
* @type {number}
127-
* @private
128115
*/
129116
private static nextConnectionId_ = 0;
130117

131118
/**
132119
* @implements {ServerActions}
133-
* @param {!RepoInfo} repoInfo_ Data about the namespace we are connecting to
134-
* @param {function(string, *, boolean, ?number)} onDataUpdate_ A callback for new data from the server
135-
* @param onConnectStatus_
136-
* @param onServerInfoUpdate_
137-
* @param authTokenProvider_
138-
* @param authOverride_
120+
* @param repoInfo_ Data about the namespace we are connecting to
121+
* @param onDataUpdate_ A callback for new data from the server
139122
*/
140123
constructor(
141124
private repoInfo_: RepoInfo,
@@ -166,12 +149,6 @@ export class PersistentConnection extends ServerActions {
166149
}
167150
}
168151

169-
/**
170-
* @param {!string} action
171-
* @param {*} body
172-
* @param {function(*)=} onResponse
173-
* @protected
174-
*/
175152
protected sendRequest(
176153
action: string,
177154
body: any,
@@ -203,14 +180,16 @@ export class PersistentConnection extends ServerActions {
203180
const queryId = query.queryIdentifier();
204181
const pathString = query.path.toString();
205182
this.log_('Listen called for ' + pathString + ' ' + queryId);
206-
this.listens_[pathString] = this.listens_[pathString] || {};
183+
if (!this.listens.has(pathString)) {
184+
this.listens.set(pathString, new Map());
185+
}
207186
assert(
208187
query.getQueryParams().isDefault() ||
209188
!query.getQueryParams().loadsAllData(),
210189
'listen() called for non-default but complete query'
211190
);
212191
assert(
213-
!this.listens_[pathString][queryId],
192+
!this.listens.get(pathString)!.has(queryId),
214193
'listen() called twice for same path/queryId.'
215194
);
216195
const listenSpec: ListenSpec = {
@@ -219,20 +198,13 @@ export class PersistentConnection extends ServerActions {
219198
query: query,
220199
tag: tag
221200
};
222-
this.listens_[pathString][queryId] = listenSpec;
201+
this.listens.get(pathString)!.set(queryId, listenSpec);
223202

224203
if (this.connected_) {
225204
this.sendListen_(listenSpec);
226205
}
227206
}
228207

229-
/**
230-
* @param {!{onComplete(),
231-
* hashFn():!string,
232-
* query: !Query,
233-
* tag: ?number}} listenSpec
234-
* @private
235-
*/
236208
private sendListen_(listenSpec: ListenSpec) {
237209
const query = listenSpec.query;
238210
const pathString = query.path.toString();
@@ -258,7 +230,8 @@ export class PersistentConnection extends ServerActions {
258230
PersistentConnection.warnOnListenWarnings_(payload, query);
259231

260232
const currentListenSpec =
261-
this.listens_[pathString] && this.listens_[pathString][queryId];
233+
this.listens.get(pathString) &&
234+
this.listens.get(pathString)!.get(queryId);
262235
// only trigger actions if the listen hasn't been removed and readded
263236
if (currentListenSpec === listenSpec) {
264237
this.log_('listen response', message);
@@ -274,11 +247,6 @@ export class PersistentConnection extends ServerActions {
274247
});
275248
}
276249

277-
/**
278-
* @param {*} payload
279-
* @param {!Query} query
280-
* @private
281-
*/
282250
private static warnOnListenWarnings_(payload: any, query: Query) {
283251
if (payload && typeof payload === 'object' && contains(payload, 'w')) {
284252
const warnings = safeGet(payload, 'w');
@@ -319,10 +287,6 @@ export class PersistentConnection extends ServerActions {
319287
this.reduceReconnectDelayIfAdminCredential_(token);
320288
}
321289

322-
/**
323-
* @param {!string} credential
324-
* @private
325-
*/
326290
private reduceReconnectDelayIfAdminCredential_(credential: string) {
327291
// NOTE: This isn't intended to be bulletproof (a malicious developer can always just modify the client).
328292
// Additionally, we don't bother resetting the max delay back to the default if auth fails / expires.
@@ -576,10 +540,6 @@ export class PersistentConnection extends ServerActions {
576540
}
577541
}
578542

579-
/**
580-
* @param {*} message
581-
* @private
582-
*/
583543
private onDataMessage_(message: { [k: string]: any }) {
584544
if ('r' in message) {
585545
// this is a response
@@ -663,10 +623,6 @@ export class PersistentConnection extends ServerActions {
663623
}, Math.floor(timeout)) as any;
664624
}
665625

666-
/**
667-
* @param {boolean} visible
668-
* @private
669-
*/
670626
private onVisible_(visible: boolean) {
671627
// NOTE: Tabbing away and back to a window will defeat our reconnect backoff, but I think that's fine.
672628
if (
@@ -819,9 +775,6 @@ export class PersistentConnection extends ServerActions {
819775
}
820776
}
821777

822-
/**
823-
* @param {string} reason
824-
*/
825778
interrupt(reason: string) {
826779
log('Interrupting connection for reason: ' + reason);
827780
this.interruptReasons_[reason] = true;
@@ -838,9 +791,6 @@ export class PersistentConnection extends ServerActions {
838791
}
839792
}
840793

841-
/**
842-
* @param {string} reason
843-
*/
844794
resume(reason: string) {
845795
log('Resuming connection for reason: ' + reason);
846796
delete this.interruptReasons_[reason];
@@ -872,11 +822,6 @@ export class PersistentConnection extends ServerActions {
872822
if (this.outstandingPutCount_ === 0) this.outstandingPuts_ = [];
873823
}
874824

875-
/**
876-
* @param {!string} pathString
877-
* @param {Array.<*>=} query
878-
* @private
879-
*/
880825
private onListenRevoked_(pathString: string, query?: any[]) {
881826
// Remove the listen and manufacture a "permission_denied" error for the failed listen.
882827
let queryId;
@@ -889,20 +834,15 @@ export class PersistentConnection extends ServerActions {
889834
if (listen && listen.onComplete) listen.onComplete('permission_denied');
890835
}
891836

892-
/**
893-
* @param {!string} pathString
894-
* @param {!string} queryId
895-
* @return {{queries:Array.<Query>, onComplete:function(string)}}
896-
* @private
897-
*/
898837
private removeListen_(pathString: string, queryId: string): ListenSpec {
899838
const normalizedPathString = new Path(pathString).toString(); // normalize path.
900839
let listen;
901-
if (this.listens_[normalizedPathString] !== undefined) {
902-
listen = this.listens_[normalizedPathString][queryId];
903-
delete this.listens_[normalizedPathString][queryId];
904-
if (getCount(this.listens_[normalizedPathString]) === 0) {
905-
delete this.listens_[normalizedPathString];
840+
if (this.listens.has(normalizedPathString)) {
841+
const map = this.listens.get(normalizedPathString)!;
842+
listen = map.get(queryId);
843+
map.delete(queryId);
844+
if (map.size === 0) {
845+
this.listens.delete(normalizedPathString);
906846
}
907847
} else {
908848
// all listens for this path has already been removed
@@ -948,11 +888,11 @@ export class PersistentConnection extends ServerActions {
948888

949889
// Puts depend on having received the corresponding data update from the server before they complete, so we must
950890
// make sure to send listens before puts.
951-
forEach(this.listens_, (_pathString, queries) => {
952-
forEach(queries, (_key, listenSpec) => {
891+
for (const queries of this.listens.values()) {
892+
for (const listenSpec of queries.values()) {
953893
this.sendListen_(listenSpec);
954-
});
955-
});
894+
}
895+
}
956896

957897
for (let i = 0; i < this.outstandingPuts_.length; i++) {
958898
if (this.outstandingPuts_[i]) this.sendPut_(i);
@@ -971,7 +911,6 @@ export class PersistentConnection extends ServerActions {
971911

972912
/**
973913
* Sends client stats for first connection
974-
* @private
975914
*/
976915
private sendConnectStats_() {
977916
const stats: { [k: string]: number } = {};
@@ -995,10 +934,6 @@ export class PersistentConnection extends ServerActions {
995934
this.reportStats(stats);
996935
}
997936

998-
/**
999-
* @return {boolean}
1000-
* @private
1001-
*/
1002937
private shouldReconnect_(): boolean {
1003938
const online = OnlineMonitor.getInstance().currentlyOnline();
1004939
return isEmpty(this.interruptReasons_) && online;

packages/database/src/core/Repo.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import { SyncTree } from './SyncTree';
2727
import { SnapshotHolder } from './SnapshotHolder';
2828
import { stringify } from '@firebase/util';
2929
import { beingCrawled, each, exceptionGuard, warn, log } from './util/util';
30-
import { map, forEach, isEmpty } from '@firebase/util';
30+
import { map, isEmpty } from '@firebase/util';
3131
import { AuthTokenProvider } from './AuthTokenProvider';
3232
import { StatsManager } from './stats/StatsManager';
3333
import { StatsReporter } from './stats/StatsReporter';
@@ -403,14 +403,14 @@ export class Repo {
403403
let empty = true;
404404
const serverValues = this.generateServerValues();
405405
const changedChildren: { [k: string]: Node } = {};
406-
forEach(childrenToMerge, (changedKey: string, changedValue: any) => {
406+
for (const [changedKey, changedValue] of Object.entries(childrenToMerge)) {
407407
empty = false;
408408
const newNodeUnresolved = nodeFromJSON(changedValue);
409409
changedChildren[changedKey] = resolveDeferredValueSnapshot(
410410
newNodeUnresolved,
411411
serverValues
412412
);
413-
});
413+
}
414414

415415
if (!empty) {
416416
const writeId = this.getNextWriteId_();
@@ -440,10 +440,10 @@ export class Repo {
440440
}
441441
);
442442

443-
forEach(childrenToMerge, (changedPath: string) => {
443+
for (const changedPath of Object.keys(childrenToMerge)) {
444444
const affectedPath = this.abortTransactions_(path.child(changedPath));
445445
this.rerunTransactions_(affectedPath);
446-
});
446+
}
447447

448448
// We queued the events above, so just flush the queue here
449449
this.eventQueue_.raiseEventsForChangedPath(path, []);
@@ -566,10 +566,12 @@ export class Repo {
566566
childrenToMerge,
567567
(status, errorReason) => {
568568
if (status === 'ok') {
569-
forEach(childrenToMerge, (childName: string, childNode: any) => {
569+
for (const [childName, childNode] of Object.entries(
570+
childrenToMerge
571+
)) {
570572
const newChildNode = nodeFromJSON(childNode);
571573
this.onDisconnect_.remember(path.child(childName), newChildNode);
572-
});
574+
}
573575
}
574576
this.callOnCompleteCallback(onComplete, status, errorReason);
575577
}
@@ -651,11 +653,14 @@ export class Repo {
651653
0
652654
);
653655

654-
forEach(stats, (stat: string, value: any) => {
656+
for (const [stat, value] of Object.entries(stats)) {
657+
let paddedStat = stat;
655658
// pad stat names to be the same length (plus 2 extra spaces).
656-
for (let i = stat.length; i < longestName + 2; i++) stat += ' ';
657-
console.log(stat + value);
658-
});
659+
for (let i = stat.length; i < longestName + 2; i++) {
660+
paddedStat += ' ';
661+
}
662+
console.log(paddedStat + value);
663+
}
659664
}
660665

661666
statsIncrementCounter(metric: string) {

0 commit comments

Comments
 (0)