Skip to content

Commit f927db8

Browse files
committed
Some more refactoring
1 parent 6aa3c38 commit f927db8

File tree

1 file changed

+39
-61
lines changed

1 file changed

+39
-61
lines changed

packages/database/src/core/SparseSnapshotTree.ts

Lines changed: 39 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,26 @@ import { Node } from './snap/Node';
2121

2222
/**
2323
* Helper class to store a sparse set of snapshots.
24-
*
25-
* @constructor
2624
*/
2725
export class SparseSnapshotTree {
28-
/**
29-
* @private
30-
* @type {Node}
31-
*/
32-
private value_: Node | null = null;
26+
private value: Node | null = null;
3327

34-
/**
35-
* @private
36-
*/
37-
private children_: Map<string, SparseSnapshotTree> | null = null;
28+
private readonly children: Map<string, SparseSnapshotTree> = new Map();
3829

3930
/**
4031
* Gets the node stored at the given path if one exists.
4132
*
42-
* @param {!Path} path Path to look up snapshot for.
43-
* @return {?Node} The retrieved node, or null.
33+
* @param path Path to look up snapshot for.
34+
* @return The retrieved node, or null.
4435
*/
4536
find(path: Path): Node | null {
46-
if (this.value_ != null) {
47-
return this.value_.getChild(path);
48-
} 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) {
4940
const childKey = path.getFront();
5041
path = path.popFront();
51-
if (this.children_.has(childKey)) {
52-
const childTree = this.children_.get(childKey);
42+
if (this.children.has(childKey)) {
43+
const childTree = this.children.get(childKey);
5344
return childTree.find(path);
5445
} else {
5546
return null;
@@ -63,26 +54,22 @@ export class SparseSnapshotTree {
6354
* Stores the given node at the specified path. If there is already a node
6455
* at a shallower path, it merges the new data into that snapshot node.
6556
*
66-
* @param {!Path} path Path to look up snapshot for.
67-
* @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.
6859
*/
6960
remember(path: Path, data: Node) {
7061
if (path.isEmpty()) {
71-
this.value_ = data;
72-
this.children_ = null;
73-
} else if (this.value_ !== null) {
74-
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);
7566
} else {
76-
if (this.children_ == null) {
77-
this.children_ = new Map<string, SparseSnapshotTree>();
78-
}
79-
8067
const childKey = path.getFront();
81-
if (!this.children_.has(childKey)) {
82-
this.children_.set(childKey, new SparseSnapshotTree());
68+
if (!this.children.has(childKey)) {
69+
this.children.set(childKey, new SparseSnapshotTree());
8370
}
8471

85-
const child = this.children_.get(childKey) as SparseSnapshotTree;
72+
const child = this.children.get(childKey);
8673
path = path.popFront();
8774
child.remember(path, data);
8875
}
@@ -91,22 +78,22 @@ export class SparseSnapshotTree {
9178
/**
9279
* Purge the data at path from the cache.
9380
*
94-
* @param {!Path} path Path to look up snapshot for.
95-
* @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.
9683
*/
9784
forget(path: Path): boolean {
9885
if (path.isEmpty()) {
99-
this.value_ = null;
100-
this.children_ = null;
86+
this.value = null;
87+
this.children.clear();
10188
return true;
10289
} else {
103-
if (this.value_ !== null) {
104-
if (this.value_.isLeafNode()) {
90+
if (this.value !== null) {
91+
if (this.value.isLeafNode()) {
10592
// We're trying to forget a node that doesn't exist
10693
return false;
10794
} else {
108-
const value = this.value_;
109-
this.value_ = null;
95+
const value = this.value;
96+
this.value = null;
11097

11198
const self = this;
11299
value.forEachChild(PRIORITY_INDEX, function(key, tree) {
@@ -115,24 +102,17 @@ export class SparseSnapshotTree {
115102

116103
return this.forget(path);
117104
}
118-
} else if (this.children_ !== null) {
105+
} else if (this.children.size > 0) {
119106
const childKey = path.getFront();
120107
path = path.popFront();
121-
if (this.children_.has(childKey)) {
122-
const safeToRemove = (this.children_.get(
123-
childKey
124-
) as SparseSnapshotTree).forget(path);
108+
if (this.children.has(childKey)) {
109+
const safeToRemove = this.children.get(childKey).forget(path);
125110
if (safeToRemove) {
126-
this.children_.delete(childKey);
111+
this.children.delete(childKey);
127112
}
128113
}
129114

130-
if (this.children_.size === 0) {
131-
this.children_ = null;
132-
return true;
133-
} else {
134-
return false;
135-
}
115+
return this.children.size === 0;
136116
} else {
137117
return true;
138118
}
@@ -143,12 +123,12 @@ export class SparseSnapshotTree {
143123
* Recursively iterates through all of the stored tree and calls the
144124
* callback on each one.
145125
*
146-
* @param {!Path} prefixPath Path to look up node for.
147-
* @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.
148128
*/
149129
forEachTree(prefixPath: Path, func: (a: Path, b: Node) => any) {
150-
if (this.value_ !== null) {
151-
func(prefixPath, this.value_);
130+
if (this.value !== null) {
131+
func(prefixPath, this.value);
152132
} else {
153133
this.forEachChild((key, tree) => {
154134
const path = new Path(prefixPath.toString() + '/' + key);
@@ -160,13 +140,11 @@ export class SparseSnapshotTree {
160140
/**
161141
* Iterates through each immediate child and triggers the callback.
162142
*
163-
* @param {!Function} func The function to invoke for each child.
143+
* @param func The function to invoke for each child.
164144
*/
165145
forEachChild(func: (a: string, b: SparseSnapshotTree) => void) {
166-
if (this.children_ !== null) {
167-
this.children_.forEach((tree, key) => {
168-
func(key, tree);
169-
});
170-
}
146+
this.children.forEach((tree, key) => {
147+
func(key, tree);
148+
});
171149
}
172150
}

0 commit comments

Comments
 (0)