Skip to content

Commit cd6f0dc

Browse files
committed
Refactor object utility functions
1 parent bc4a844 commit cd6f0dc

File tree

21 files changed

+191
-367
lines changed

21 files changed

+191
-367
lines changed

config/tsconfig.base.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,11 @@
33
"compilerOptions": {
44
"declaration": true,
55
"importHelpers": true,
6-
"lib": [
7-
"es2015",
8-
"dom"
9-
],
6+
"lib": ["es2017", "dom"],
107
"module": "ES2015",
118
"moduleResolution": "node",
129
"sourceMap": true,
1310
"target": "es5",
14-
"typeRoots": [
15-
"../node_modules/@types"
16-
]
11+
"typeRoots": ["../node_modules/@types"]
1712
}
1813
}

packages/database/src/core/PersistentConnection.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -948,8 +948,8 @@ 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: string, queries: Object) => {
952-
forEach(queries, (key: string, listenSpec: ListenSpec) => {
951+
forEach(this.listens_, (_pathString, queries) => {
952+
forEach(queries, (_key, listenSpec) => {
953953
this.sendListen_(listenSpec);
954954
});
955955
});

packages/database/src/core/SparseSnapshotTree.ts

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -17,41 +17,30 @@
1717

1818
import { Path } from './util/Path';
1919
import { PRIORITY_INDEX } from './snap/indexes/PriorityIndex';
20-
import { CountedSet } from './util/CountedSet';
2120
import { Node } from './snap/Node';
2221

2322
/**
2423
* Helper class to store a sparse set of snapshots.
25-
*
26-
* @constructor
2724
*/
2825
export class SparseSnapshotTree {
29-
/**
30-
* @private
31-
* @type {Node}
32-
*/
33-
private value_: Node | null = null;
26+
private value: Node | null = null;
3427

35-
/**
36-
* @private
37-
* @type {CountedSet}
38-
*/
39-
private children_: CountedSet<string, SparseSnapshotTree> | null = null;
28+
private readonly children: Map<string, SparseSnapshotTree> = new Map();
4029

4130
/**
4231
* Gets the node stored at the given path if one exists.
4332
*
44-
* @param {!Path} path Path to look up snapshot for.
45-
* @return {?Node} The retrieved node, or null.
33+
* @param path Path to look up snapshot for.
34+
* @return The retrieved node, or null.
4635
*/
4736
find(path: Path): Node | null {
48-
if (this.value_ != null) {
49-
return this.value_.getChild(path);
50-
} else if (!path.isEmpty() && this.children_ != null) {
37+
if (this.value != null) {
38+
return this.value.getChild(path);
39+
} else if (!path.isEmpty() && this.children.size > 0) {
5140
const childKey = path.getFront();
5241
path = path.popFront();
53-
if (this.children_.contains(childKey)) {
54-
const childTree = this.children_.get(childKey) as SparseSnapshotTree;
42+
if (this.children.has(childKey)) {
43+
const childTree = this.children.get(childKey);
5544
return childTree.find(path);
5645
} else {
5746
return null;
@@ -65,26 +54,22 @@ export class SparseSnapshotTree {
6554
* Stores the given node at the specified path. If there is already a node
6655
* at a shallower path, it merges the new data into that snapshot node.
6756
*
68-
* @param {!Path} path Path to look up snapshot for.
69-
* @param {!Node} data The new data, or null.
57+
* @param path Path to look up snapshot for.
58+
* @param data The new data, or null.
7059
*/
7160
remember(path: Path, data: Node) {
7261
if (path.isEmpty()) {
73-
this.value_ = data;
74-
this.children_ = null;
75-
} else if (this.value_ !== null) {
76-
this.value_ = this.value_.updateChild(path, data);
62+
this.value = data;
63+
this.children.clear();
64+
} else if (this.value !== null) {
65+
this.value = this.value.updateChild(path, data);
7766
} else {
78-
if (this.children_ == null) {
79-
this.children_ = new CountedSet<string, SparseSnapshotTree>();
80-
}
81-
8267
const childKey = path.getFront();
83-
if (!this.children_.contains(childKey)) {
84-
this.children_.add(childKey, new SparseSnapshotTree());
68+
if (!this.children.has(childKey)) {
69+
this.children.set(childKey, new SparseSnapshotTree());
8570
}
8671

87-
const child = this.children_.get(childKey) as SparseSnapshotTree;
72+
const child = this.children.get(childKey);
8873
path = path.popFront();
8974
child.remember(path, data);
9075
}
@@ -93,22 +78,22 @@ export class SparseSnapshotTree {
9378
/**
9479
* Purge the data at path from the cache.
9580
*
96-
* @param {!Path} path Path to look up snapshot for.
97-
* @return {boolean} True if this node should now be removed.
81+
* @param path Path to look up snapshot for.
82+
* @return True if this node should now be removed.
9883
*/
9984
forget(path: Path): boolean {
10085
if (path.isEmpty()) {
101-
this.value_ = null;
102-
this.children_ = null;
86+
this.value = null;
87+
this.children.clear();
10388
return true;
10489
} else {
105-
if (this.value_ !== null) {
106-
if (this.value_.isLeafNode()) {
90+
if (this.value !== null) {
91+
if (this.value.isLeafNode()) {
10792
// We're trying to forget a node that doesn't exist
10893
return false;
10994
} else {
110-
const value = this.value_;
111-
this.value_ = null;
95+
const value = this.value;
96+
this.value = null;
11297

11398
const self = this;
11499
value.forEachChild(PRIORITY_INDEX, function(key, tree) {
@@ -117,24 +102,17 @@ export class SparseSnapshotTree {
117102

118103
return this.forget(path);
119104
}
120-
} else if (this.children_ !== null) {
105+
} else if (this.children.size > 0) {
121106
const childKey = path.getFront();
122107
path = path.popFront();
123-
if (this.children_.contains(childKey)) {
124-
const safeToRemove = (this.children_.get(
125-
childKey
126-
) as SparseSnapshotTree).forget(path);
108+
if (this.children.has(childKey)) {
109+
const safeToRemove = this.children.get(childKey).forget(path);
127110
if (safeToRemove) {
128-
this.children_.remove(childKey);
111+
this.children.delete(childKey);
129112
}
130113
}
131114

132-
if (this.children_.isEmpty()) {
133-
this.children_ = null;
134-
return true;
135-
} else {
136-
return false;
137-
}
115+
return this.children.size === 0;
138116
} else {
139117
return true;
140118
}
@@ -145,12 +123,12 @@ export class SparseSnapshotTree {
145123
* Recursively iterates through all of the stored tree and calls the
146124
* callback on each one.
147125
*
148-
* @param {!Path} prefixPath Path to look up node for.
149-
* @param {!Function} func The function to invoke for each tree.
126+
* @param prefixPath Path to look up node for.
127+
* @param func The function to invoke for each tree.
150128
*/
151129
forEachTree(prefixPath: Path, func: (a: Path, b: Node) => any) {
152-
if (this.value_ !== null) {
153-
func(prefixPath, this.value_);
130+
if (this.value !== null) {
131+
func(prefixPath, this.value);
154132
} else {
155133
this.forEachChild((key, tree) => {
156134
const path = new Path(prefixPath.toString() + '/' + key);
@@ -162,13 +140,11 @@ export class SparseSnapshotTree {
162140
/**
163141
* Iterates through each immediate child and triggers the callback.
164142
*
165-
* @param {!Function} func The function to invoke for each child.
143+
* @param func The function to invoke for each child.
166144
*/
167145
forEachChild(func: (a: string, b: SparseSnapshotTree) => void) {
168-
if (this.children_ !== null) {
169-
this.children_.each((key, tree) => {
170-
func(key, tree);
171-
});
172-
}
146+
this.children.forEach((tree, key) => {
147+
func(key, tree);
148+
});
173149
}
174150
}

packages/database/src/core/snap/IndexMap.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ export class IndexMap {
7070
const sortedMap = safeGet(this.indexes_, indexKey);
7171
if (!sortedMap) throw new Error('No index defined for ' + indexKey);
7272

73-
if (sortedMap === fallbackObject) {
73+
if (sortedMap instanceof SortedMap) {
74+
return sortedMap;
75+
} else {
7476
// The index exists, but it falls back to just name comparison. Return null so that the calling code uses the
7577
// regular child map
7678
return null;
77-
} else {
78-
return sortedMap;
7979
}
8080
}
8181

packages/database/src/core/snap/Node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ export interface Node {
116116
forEachChild(index: Index, action: (a: string, b: Node) => void): any;
117117

118118
/**
119-
* @param {boolean=} exportFormat True for export format (also wire protocol format).
120-
* @return {*} Value of this node as JSON.
119+
* @param exportFormat True for export format (also wire protocol format).
120+
* @return Value of this node as JSON.
121121
*/
122-
val(exportFormat?: boolean): Object;
122+
val(exportFormat?: boolean): any;
123123

124124
/**
125125
* @return {string} hash representing the node contents.

packages/database/src/core/snap/nodeFromJSON.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,8 @@ export function nodeFromJSON(
113113
}
114114
} else {
115115
let node: Node = ChildrenNode.EMPTY_NODE;
116-
const jsonObj = json as object;
117-
forEach(jsonObj, (key: string, childData: any) => {
118-
if (contains(jsonObj, key)) {
116+
forEach(json, (key, childData) => {
117+
if (contains(json, key)) {
119118
if (key.substring(0, 1) !== '.') {
120119
// ignore metadata nodes.
121120
const childNode = nodeFromJSON(childData);

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

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)