Skip to content

Commit 4c1cba2

Browse files
committed
Address comments
1 parent 22222dc commit 4c1cba2

File tree

5 files changed

+44
-26
lines changed

5 files changed

+44
-26
lines changed

packages/database/src/core/PersistentConnection.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class PersistentConnection extends ServerActions {
7777
private log_ = logWrapper('p:' + this.id + ':');
7878

7979
private interruptReasons_: { [reason: string]: boolean } = {};
80-
private listens_: { [path: string]: { [queryId: string]: ListenSpec } } = {};
80+
private readonly listens: Map<string, Map<string, ListenSpec>> = new Map();
8181
private outstandingPuts_: OutstandingPut[] = [];
8282
private outstandingPutCount_ = 0;
8383
private onDisconnectRequestQueue_: OnDisconnectRequest[] = [];
@@ -180,14 +180,16 @@ export class PersistentConnection extends ServerActions {
180180
const queryId = query.queryIdentifier();
181181
const pathString = query.path.toString();
182182
this.log_('Listen called for ' + pathString + ' ' + queryId);
183-
this.listens_[pathString] = this.listens_[pathString] || {};
183+
if (!this.listens.has(pathString)) {
184+
this.listens.set(pathString, new Map());
185+
}
184186
assert(
185187
query.getQueryParams().isDefault() ||
186188
!query.getQueryParams().loadsAllData(),
187189
'listen() called for non-default but complete query'
188190
);
189191
assert(
190-
!this.listens_[pathString][queryId],
192+
!this.listens.get(pathString)!.has(queryId),
191193
'listen() called twice for same path/queryId.'
192194
);
193195
const listenSpec: ListenSpec = {
@@ -196,7 +198,7 @@ export class PersistentConnection extends ServerActions {
196198
query: query,
197199
tag: tag
198200
};
199-
this.listens_[pathString][queryId] = listenSpec;
201+
this.listens.get(pathString)!.set(queryId, listenSpec);
200202

201203
if (this.connected_) {
202204
this.sendListen_(listenSpec);
@@ -228,7 +230,8 @@ export class PersistentConnection extends ServerActions {
228230
PersistentConnection.warnOnListenWarnings_(payload, query);
229231

230232
const currentListenSpec =
231-
this.listens_[pathString] && this.listens_[pathString][queryId];
233+
this.listens.get(pathString) &&
234+
this.listens.get(pathString)!.get(queryId);
232235
// only trigger actions if the listen hasn't been removed and readded
233236
if (currentListenSpec === listenSpec) {
234237
this.log_('listen response', message);
@@ -834,11 +837,12 @@ export class PersistentConnection extends ServerActions {
834837
private removeListen_(pathString: string, queryId: string): ListenSpec {
835838
const normalizedPathString = new Path(pathString).toString(); // normalize path.
836839
let listen;
837-
if (this.listens_[normalizedPathString] !== undefined) {
838-
listen = this.listens_[normalizedPathString][queryId];
839-
delete this.listens_[normalizedPathString][queryId];
840-
if (isEmpty(this.listens_[normalizedPathString])) {
841-
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);
842846
}
843847
} else {
844848
// all listens for this path has already been removed
@@ -884,8 +888,8 @@ export class PersistentConnection extends ServerActions {
884888

885889
// Puts depend on having received the corresponding data update from the server before they complete, so we must
886890
// make sure to send listens before puts.
887-
for (const queries of Object.values(this.listens_)) {
888-
for (const listenSpec of Object.values(queries)) {
891+
for (const queries of this.listens.values()) {
892+
for (const listenSpec of queries.values()) {
889893
this.sendListen_(listenSpec);
890894
}
891895
}

packages/database/src/core/SyncPoint.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,16 @@ export class SyncPoint {
263263
}
264264

265265
getCompleteView(): View | null {
266-
const completeView = Array.from(this.views.values()).find((view: View) =>
267-
view
268-
.getQuery()
269-
.getQueryParams()
270-
.loadsAllData()
271-
);
272-
return completeView || null;
266+
for (const view of this.views.values()) {
267+
if (
268+
view
269+
.getQuery()
270+
.getQueryParams()
271+
.loadsAllData()
272+
) {
273+
return view;
274+
}
275+
}
276+
return null;
273277
}
274278
}

packages/database/src/core/WriteTree.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,9 +497,15 @@ export class WriteTree {
497497
if (writeRecord.snap) {
498498
return writeRecord.path.contains(path);
499499
} else {
500-
return Object.keys(writeRecord.children).some((childName: string) =>
501-
writeRecord.path.child(childName).contains(path)
502-
);
500+
for (const childName in writeRecord.children) {
501+
if (
502+
writeRecord.children.hasOwnProperty(childName) &&
503+
writeRecord.path.child(childName).contains(path)
504+
) {
505+
return true;
506+
}
507+
}
508+
return false;
503509
}
504510
}
505511

packages/database/src/core/util/util.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,8 +406,10 @@ export function each(obj: object | Array<any>, fn: (v: any, k: any) => void) {
406406
* a single impl that does a key, value callback. So we invert
407407
* to not have to touch the `each` code points
408408
*/
409-
for (const [key, value] of Object.entries(obj)) {
410-
fn(value, key);
409+
for (const key in obj) {
410+
if (obj.hasOwnProperty(key)) {
411+
fn(obj[key], key);
412+
}
411413
}
412414
}
413415
}

packages/util/src/obj.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ export function map<T extends object, V, U extends { [key in keyof T]: V }>(
4848
opt_obj?: unknown
4949
): U {
5050
const res: Partial<U> = {};
51-
for (const [key, value] of Object.entries(obj)) {
52-
res[key as Keys<T>] = fn.call(opt_obj, value, key, obj);
51+
for (const key in obj) {
52+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
53+
res[key] = fn.call(opt_obj, obj[key], key, obj);
54+
}
5355
}
5456
return res as U;
5557
}

0 commit comments

Comments
 (0)