Skip to content

Commit 857b0fe

Browse files
authored
Refactor and remove object utility functions (#1811)
1 parent 19c3842 commit 857b0fe

34 files changed

+379
-987
lines changed

config/tsconfig.base.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,23 @@
66
"strictNullChecks": true,
77
"noImplicitAny": true,
88
"lib": [
9-
"es2015",
10-
"dom"
9+
"es5",
10+
"dom",
11+
"es2015.promise",
12+
"es2015.symbol",
13+
"es2015.iterable",
14+
"es2015.collection",
15+
"es2015.symbol.wellknown",
16+
"es2015.core",
17+
"es2017.object",
1118
],
1219
"module": "ES2015",
1320
"moduleResolution": "node",
1421
"sourceMap": true,
1522
"target": "es5",
23+
"downlevelIteration": true,
1624
"typeRoots": [
1725
"../node_modules/@types"
1826
]
1927
}
20-
}
28+
}

packages/database/src/api/test_access.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,6 @@ export const queryIdentifier = function(query: Query) {
8383
return query.queryIdentifier();
8484
};
8585

86-
/**
87-
* @param {!Query} firebaseRef
88-
* @return {!Object}
89-
*/
90-
export const listens = function(firebaseRef: Query) {
91-
return (firebaseRef.repo.persistentConnection_ as any).listens_;
92-
};
93-
9486
/**
9587
* Forces the RepoManager to create Repos that use ReadonlyRestClient instead of PersistentConnection.
9688
*

packages/database/src/core/CompoundWrite.ts

Lines changed: 42 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,23 @@
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';
2423
import { ChildrenNode } from './snap/ChildrenNode';
24+
import { each } from './util/util';
2525

2626
/**
2727
* This class holds a collection of writes that can be applied to nodes in unison. It abstracts away the logic with
2828
* dealing with priority writes and multiple nested writes. At any given path there is only allowed to be one write
2929
* modifying that path. Any write to an existing path or shadowing an existing path will modify that existing write
3030
* to reflect the write added.
31-
*
32-
* @constructor
33-
* @param {!ImmutableTree.<!Node>} writeTree
3431
*/
3532
export class CompoundWrite {
3633
constructor(private writeTree_: ImmutableTree<Node>) {}
37-
/**
38-
* @type {!CompoundWrite}
39-
*/
34+
4035
static Empty = new CompoundWrite(new ImmutableTree(null));
4136

42-
/**
43-
* @param {!Path} path
44-
* @param {!Node} node
45-
* @return {!CompoundWrite}
46-
*/
4737
addWrite(path: Path, node: Node): CompoundWrite {
4838
if (path.isEmpty()) {
4939
return new CompoundWrite(new ImmutableTree(node));
@@ -63,14 +53,9 @@ export class CompoundWrite {
6353
}
6454
}
6555

66-
/**
67-
* @param {!Path} path
68-
* @param {!Object.<string, !Node>} updates
69-
* @return {!CompoundWrite}
70-
*/
7156
addWrites(path: Path, updates: { [name: string]: Node }): CompoundWrite {
7257
let newWrite = this as CompoundWrite;
73-
forEach(updates, function(childKey: string, node: Node) {
58+
each(updates, function(childKey: string, node: Node) {
7459
newWrite = newWrite.addWrite(path.child(childKey), node);
7560
});
7661
return newWrite;
@@ -80,7 +65,7 @@ export class CompoundWrite {
8065
* Will remove a write at the given path and deeper paths. This will <em>not</em> modify a write at a higher
8166
* location, which must be removed by calling this method with that path.
8267
*
83-
* @param {!Path} path The path at which a write and all deeper writes should be removed
68+
* @param path The path at which a write and all deeper writes should be removed
8469
* @return {!CompoundWrite} The new CompoundWrite with the removed path
8570
*/
8671
removeWrite(path: Path): CompoundWrite {
@@ -96,8 +81,8 @@ export class CompoundWrite {
9681
* Returns whether this CompoundWrite will fully overwrite a node at a given location and can therefore be
9782
* considered "complete".
9883
*
99-
* @param {!Path} path The path to check for
100-
* @return {boolean} Whether there is a complete write at that path
84+
* @param path The path to check for
85+
* @return Whether there is a complete write at that path
10186
*/
10287
hasCompleteWrite(path: Path): boolean {
10388
return this.getCompleteNode(path) != null;
@@ -107,8 +92,8 @@ export class CompoundWrite {
10792
* Returns a node for a path if and only if the node is a "complete" overwrite at that path. This will not aggregate
10893
* writes from deeper paths, but will return child nodes from a more shallow path.
10994
*
110-
* @param {!Path} path The path to get a complete write
111-
* @return {?Node} The node if complete at that path, or null otherwise.
95+
* @param path The path to get a complete write
96+
* @return The node if complete at that path, or null otherwise.
11297
*/
11398
getCompleteNode(path: Path): Node | null {
11499
const rootmost = this.writeTree_.findRootMostValueAndPath(path);
@@ -124,9 +109,9 @@ export class CompoundWrite {
124109
/**
125110
* Returns all children that are guaranteed to be a complete overwrite.
126111
*
127-
* @return {!Array.<NamedNode>} A list of all complete children.
112+
* @return A list of all complete children.
128113
*/
129-
getCompleteChildren(): Array<NamedNode> {
114+
getCompleteChildren(): NamedNode[] {
130115
const children: NamedNode[] = [];
131116
let node = this.writeTree_.value;
132117
if (node != null) {
@@ -149,10 +134,6 @@ export class CompoundWrite {
149134
return children;
150135
}
151136

152-
/**
153-
* @param {!Path} path
154-
* @return {!CompoundWrite}
155-
*/
156137
childCompoundWrite(path: Path): CompoundWrite {
157138
if (path.isEmpty()) {
158139
return this;
@@ -168,7 +149,7 @@ export class CompoundWrite {
168149

169150
/**
170151
* Returns true if this CompoundWrite is empty and therefore does not modify any nodes.
171-
* @return {boolean} Whether this CompoundWrite is empty
152+
* @return Whether this CompoundWrite is empty
172153
*/
173154
isEmpty(): boolean {
174155
return this.writeTree_.isEmpty();
@@ -177,52 +158,41 @@ export class CompoundWrite {
177158
/**
178159
* Applies this CompoundWrite to a node. The node is returned with all writes from this CompoundWrite applied to the
179160
* node
180-
* @param {!Node} node The node to apply this CompoundWrite to
181-
* @return {!Node} The node with all writes applied
161+
* @param node The node to apply this CompoundWrite to
162+
* @return The node with all writes applied
182163
*/
183164
apply(node: Node): Node {
184-
return CompoundWrite.applySubtreeWrite_(Path.Empty, this.writeTree_, node);
165+
return applySubtreeWrite(Path.Empty, this.writeTree_, node);
185166
}
167+
}
186168

187-
/**
188-
* @param {!Path} relativePath
189-
* @param {!ImmutableTree.<!Node>} writeTree
190-
* @param {!Node} node
191-
* @return {!Node}
192-
* @private
193-
*/
194-
private static applySubtreeWrite_ = function(
195-
relativePath: Path,
196-
writeTree: ImmutableTree<Node>,
197-
node: Node
198-
): Node {
199-
if (writeTree.value != null) {
200-
// Since there a write is always a leaf, we're done here
201-
return node.updateChild(relativePath, writeTree.value);
202-
} else {
203-
let priorityWrite = null;
204-
writeTree.children.inorderTraversal(function(childKey, childTree) {
205-
if (childKey === '.priority') {
206-
// Apply priorities at the end so we don't update priorities for either empty nodes or forget
207-
// to apply priorities to empty nodes that are later filled
208-
assert(
209-
childTree.value !== null,
210-
'Priority writes must always be leaf nodes'
211-
);
212-
priorityWrite = childTree.value;
213-
} else {
214-
node = CompoundWrite.applySubtreeWrite_(
215-
relativePath.child(childKey),
216-
childTree,
217-
node
218-
);
219-
}
220-
});
221-
// If there was a priority write, we only apply it if the node is not empty
222-
if (!node.getChild(relativePath).isEmpty() && priorityWrite !== null) {
223-
node = node.updateChild(relativePath.child('.priority'), priorityWrite);
169+
function applySubtreeWrite(
170+
relativePath: Path,
171+
writeTree: ImmutableTree<Node>,
172+
node: Node
173+
): Node {
174+
if (writeTree.value != null) {
175+
// Since there a write is always a leaf, we're done here
176+
return node.updateChild(relativePath, writeTree.value);
177+
} else {
178+
let priorityWrite = null;
179+
writeTree.children.inorderTraversal(function(childKey, childTree) {
180+
if (childKey === '.priority') {
181+
// Apply priorities at the end so we don't update priorities for either empty nodes or forget
182+
// to apply priorities to empty nodes that are later filled
183+
assert(
184+
childTree.value !== null,
185+
'Priority writes must always be leaf nodes'
186+
);
187+
priorityWrite = childTree.value;
188+
} else {
189+
node = applySubtreeWrite(relativePath.child(childKey), childTree, node);
224190
}
225-
return node;
191+
});
192+
// If there was a priority write, we only apply it if the node is not empty
193+
if (!node.getChild(relativePath).isEmpty() && priorityWrite !== null) {
194+
node = node.updateChild(relativePath.child('.priority'), priorityWrite);
226195
}
227-
};
196+
return node;
197+
}
228198
}

0 commit comments

Comments
 (0)