Skip to content

Commit a1508be

Browse files
committed
Remove some object utility functions
1 parent 407a59a commit a1508be

28 files changed

+190
-484
lines changed

config/tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"moduleResolution": "node",
99
"sourceMap": true,
1010
"target": "es5",
11+
"downlevelIteration": true,
1112
"typeRoots": ["../node_modules/@types"]
1213
}
1314
}

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: 6 additions & 6 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';
@@ -901,7 +901,7 @@ export class PersistentConnection extends ServerActions {
901901
if (this.listens_[normalizedPathString] !== undefined) {
902902
listen = this.listens_[normalizedPathString][queryId];
903903
delete this.listens_[normalizedPathString][queryId];
904-
if (getCount(this.listens_[normalizedPathString]) === 0) {
904+
if (isEmpty(this.listens_[normalizedPathString])) {
905905
delete this.listens_[normalizedPathString];
906906
}
907907
} else {
@@ -948,11 +948,11 @@ export class PersistentConnection extends ServerActions {
948948

949949
// Puts depend on having received the corresponding data update from the server before they complete, so we must
950950
// make sure to send listens before puts.
951-
forEach(this.listens_, (_pathString, queries) => {
952-
forEach(queries, (_key, listenSpec) => {
951+
for (const queries of Object.values(this.listens_)) {
952+
for (const listenSpec of Object.values(queries)) {
953953
this.sendListen_(listenSpec);
954-
});
955-
});
954+
}
955+
}
956956

957957
for (let i = 0; i < this.outstandingPuts_.length; i++) {
958958
if (this.outstandingPuts_[i]) this.sendPut_(i);

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) {

packages/database/src/core/RepoInfo.ts

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

1818
import { assert } from '@firebase/util';
19-
import { forEach } from '@firebase/util';
2019
import { PersistentStorage } from './storage/storage';
2120
import { LONG_POLLING, WEBSOCKET } from '../realtime/Constants';
2221

@@ -102,9 +101,9 @@ export class RepoInfo {
102101

103102
const pairs: string[] = [];
104103

105-
forEach(params, (key: string, value: string) => {
104+
for (const [key, value] of Object.entries(params)) {
106105
pairs.push(key + '=' + value);
107-
});
106+
}
108107

109108
return connURL + pairs.join('&');
110109
}

packages/database/src/core/SyncPoint.ts

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import { CacheNode } from './view/CacheNode';
1919
import { ChildrenNode } from './snap/ChildrenNode';
2020
import { assert } from '@firebase/util';
21-
import { isEmpty, forEach, findValue, safeGet } from '@firebase/util';
2221
import { ViewCache } from './view/ViewCache';
2322
import { View } from './view/View';
2423
import { Operation } from './operation/Operation';
@@ -61,34 +60,21 @@ export class SyncPoint {
6160
* queryId and the value is the View for that query.
6261
*
6362
* NOTE: This list will be quite small (usually 1, but perhaps 2 or 3; any more is an odd use case).
64-
*
65-
* @type {!Object.<!string, !View>}
66-
* @private
6763
*/
68-
private views_: { [k: string]: View } = {};
64+
private readonly views: Map<string, View> = new Map();
6965

70-
/**
71-
* @return {boolean}
72-
*/
7366
isEmpty(): boolean {
74-
return isEmpty(this.views_);
67+
return this.views.size === 0;
7568
}
7669

77-
/**
78-
*
79-
* @param {!Operation} operation
80-
* @param {!WriteTreeRef} writesCache
81-
* @param {?Node} optCompleteServerCache
82-
* @return {!Array.<!Event>}
83-
*/
8470
applyOperation(
8571
operation: Operation,
8672
writesCache: WriteTreeRef,
8773
optCompleteServerCache: Node | null
8874
): Event[] {
8975
const queryId = operation.source.queryId;
9076
if (queryId !== null) {
91-
const view = safeGet(this.views_, queryId);
77+
const view = this.views.get(queryId);
9278
assert(view != null, 'SyncTree gave us an op for an invalid query.');
9379
return view.applyOperation(
9480
operation,
@@ -98,11 +84,11 @@ export class SyncPoint {
9884
} else {
9985
let events: Event[] = [];
10086

101-
forEach(this.views_, function(key: string, view: View) {
87+
for (const view of this.views.values()) {
10288
events = events.concat(
10389
view.applyOperation(operation, writesCache, optCompleteServerCache)
10490
);
105-
});
91+
}
10692

10793
return events;
10894
}
@@ -126,7 +112,7 @@ export class SyncPoint {
126112
serverCacheComplete: boolean
127113
): Event[] {
128114
const queryId = query.queryIdentifier();
129-
let view = safeGet(this.views_, queryId);
115+
let view = this.views.get(queryId);
130116
if (!view) {
131117
// TODO: make writesCache take flag for complete server node
132118
let eventCache = writesCache.calcCompleteEventCache(
@@ -155,7 +141,7 @@ export class SyncPoint {
155141
)
156142
);
157143
view = new View(query, viewCache);
158-
this.views_[queryId] = view;
144+
this.views.set(queryId, view);
159145
}
160146

161147
// This is guaranteed to exist now, we just created anything that was missing
@@ -185,13 +171,12 @@ export class SyncPoint {
185171
const hadCompleteView = this.hasCompleteView();
186172
if (queryId === 'default') {
187173
// When you do ref.off(...), we search all views for the registration to remove.
188-
const self = this;
189-
forEach(this.views_, function(viewQueryId: string, view: View) {
174+
for (const [viewQueryId, view] of this.views.entries()) {
190175
cancelEvents = cancelEvents.concat(
191176
view.removeEventRegistration(eventRegistration, cancelError)
192177
);
193178
if (view.isEmpty()) {
194-
delete self.views_[viewQueryId];
179+
this.views.delete(viewQueryId);
195180

196181
// We'll deal with complete views later.
197182
if (
@@ -203,16 +188,16 @@ export class SyncPoint {
203188
removed.push(view.getQuery());
204189
}
205190
}
206-
});
191+
}
207192
} else {
208193
// remove the callback from the specific view.
209-
const view = safeGet(this.views_, queryId);
194+
const view = this.views.get(queryId);
210195
if (view) {
211196
cancelEvents = cancelEvents.concat(
212197
view.removeEventRegistration(eventRegistration, cancelError)
213198
);
214199
if (view.isEmpty()) {
215-
delete this.views_[queryId];
200+
this.views.delete(queryId);
216201

217202
// We'll deal with complete views later.
218203
if (
@@ -237,66 +222,48 @@ export class SyncPoint {
237222
return { removed: removed, events: cancelEvents };
238223
}
239224

240-
/**
241-
* @return {!Array.<!View>}
242-
*/
243225
getQueryViews(): View[] {
244-
const values = Object.keys(this.views_).map(key => this.views_[key]);
245-
return values.filter(function(view) {
246-
return !view
247-
.getQuery()
248-
.getQueryParams()
249-
.loadsAllData();
250-
});
226+
return Array.from(this.views.values()).filter(
227+
view =>
228+
!view
229+
.getQuery()
230+
.getQueryParams()
231+
.loadsAllData()
232+
);
251233
}
252234

253235
/**
254-
*
255-
* @param {!Path} path The path to the desired complete snapshot
256-
* @return {?Node} A complete cache, if it exists
236+
* @param path The path to the desired complete snapshot
237+
* @return A complete cache, if it exists
257238
*/
258239
getCompleteServerCache(path: Path): Node | null {
259240
let serverCache: Node | null = null;
260-
forEach(this.views_, (key: string, view: View) => {
241+
for (const view of this.views.values()) {
261242
serverCache = serverCache || view.getCompleteServerCache(path);
262-
});
243+
}
263244
return serverCache;
264245
}
265246

266-
/**
267-
* @param {!Query} query
268-
* @return {?View}
269-
*/
270247
viewForQuery(query: Query): View | null {
271248
const params = query.getQueryParams();
272249
if (params.loadsAllData()) {
273250
return this.getCompleteView();
274251
} else {
275252
const queryId = query.queryIdentifier();
276-
return safeGet(this.views_, queryId);
253+
return this.views.get(queryId);
277254
}
278255
}
279256

280-
/**
281-
* @param {!Query} query
282-
* @return {boolean}
283-
*/
284257
viewExistsForQuery(query: Query): boolean {
285258
return this.viewForQuery(query) != null;
286259
}
287260

288-
/**
289-
* @return {boolean}
290-
*/
291261
hasCompleteView(): boolean {
292262
return this.getCompleteView() != null;
293263
}
294264

295-
/**
296-
* @return {?View}
297-
*/
298265
getCompleteView(): View | null {
299-
const completeView = findValue(this.views_, (view: View) =>
266+
const completeView = Array.from(this.views.values()).find((view: View) =>
300267
view
301268
.getQuery()
302269
.getQueryParams()

0 commit comments

Comments
 (0)