Skip to content

Commit 7c62224

Browse files
committed
Address comments
1 parent ad23ddc commit 7c62224

File tree

4 files changed

+38
-24
lines changed

4 files changed

+38
-24
lines changed

packages/database/src/core/PersistentConnection.ts

Lines changed: 14 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 listens_: Map<string, Map<string, ListenSpec>> = new Map();
8181
private outstandingPuts_: OutstandingPut[] = [];
8282
private outstandingPutCount_ = 0;
8383
private onDisconnectRequestQueue_: OnDisconnectRequest[] = [];
@@ -180,14 +180,14 @@ 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+
this.listens_.set(pathString, this.listens_.get(pathString) || new Map());
184184
assert(
185185
query.getQueryParams().isDefault() ||
186186
!query.getQueryParams().loadsAllData(),
187187
'listen() called for non-default but complete query'
188188
);
189189
assert(
190-
!this.listens_[pathString][queryId],
190+
!this.listens_.get(pathString)!.has(queryId),
191191
'listen() called twice for same path/queryId.'
192192
);
193193
const listenSpec: ListenSpec = {
@@ -196,7 +196,7 @@ export class PersistentConnection extends ServerActions {
196196
query: query,
197197
tag: tag
198198
};
199-
this.listens_[pathString][queryId] = listenSpec;
199+
this.listens_.get(pathString)!.set(queryId, listenSpec);
200200

201201
if (this.connected_) {
202202
this.sendListen_(listenSpec);
@@ -228,7 +228,8 @@ export class PersistentConnection extends ServerActions {
228228
PersistentConnection.warnOnListenWarnings_(payload, query);
229229

230230
const currentListenSpec =
231-
this.listens_[pathString] && this.listens_[pathString][queryId];
231+
this.listens_.get(pathString) &&
232+
this.listens_.get(pathString)!.get(queryId);
232233
// only trigger actions if the listen hasn't been removed and readded
233234
if (currentListenSpec === listenSpec) {
234235
this.log_('listen response', message);
@@ -834,11 +835,12 @@ export class PersistentConnection extends ServerActions {
834835
private removeListen_(pathString: string, queryId: string): ListenSpec {
835836
const normalizedPathString = new Path(pathString).toString(); // normalize path.
836837
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];
838+
if (this.listens_.has(normalizedPathString)) {
839+
const map = this.listens_.get(normalizedPathString)!;
840+
listen = map.get(queryId);
841+
map.delete(queryId);
842+
if (map.size === 0) {
843+
this.listens_.delete(normalizedPathString);
842844
}
843845
} else {
844846
// all listens for this path has already been removed
@@ -884,8 +886,8 @@ export class PersistentConnection extends ServerActions {
884886

885887
// Puts depend on having received the corresponding data update from the server before they complete, so we must
886888
// make sure to send listens before puts.
887-
for (const queries of Object.values(this.listens_)) {
888-
for (const listenSpec of Object.values(queries)) {
889+
for (const queries of this.listens_.values()) {
890+
for (const listenSpec of queries.values()) {
889891
this.sendListen_(listenSpec);
890892
}
891893
}

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
}

0 commit comments

Comments
 (0)