Skip to content

Commit 24a08fe

Browse files
committed
refactor(database): several classes and utility methods
1 parent 7efa1b0 commit 24a08fe

19 files changed

+1083
-1145
lines changed

src/database/api/TransactionResult.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1+
import { DataSnapshot } from './DataSnapshot';
2+
13
export class TransactionResult {
24
/**
35
* A type for the resolve value of Firebase.transaction.
46
* @constructor
57
* @dict
68
* @param {boolean} committed
7-
* @param {fb.api.DataSnapshot} snapshot
9+
* @param {DataSnapshot} snapshot
810
*/
9-
constructor(committed, snapshot) {
10-
/**
11-
* @type {boolean}
12-
*/
13-
this['committed'] = committed;
14-
/**
15-
* @type {fb.api.DataSnapshot}
16-
*/
17-
this['snapshot'] = snapshot;
11+
constructor(public committed: boolean, public snapshot: DataSnapshot) {
1812
}
1913
}

src/database/api/test_access.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@ import { RepoInfo } from "../core/RepoInfo";
22
import { PersistentConnection } from "../core/PersistentConnection";
33
import { RepoManager } from "../core/RepoManager";
44
import { Connection } from "../realtime/Connection";
5+
import { Query } from './Query';
56

67
export const DataConnection = PersistentConnection;
78

89
/**
910
* @param {!string} pathString
1011
* @param {function(*)} onComplete
1112
*/
12-
(PersistentConnection.prototype as any).simpleListen = function(pathString, onComplete) {
13+
(PersistentConnection.prototype as any).simpleListen = function(pathString: string, onComplete: (a: any) => any) {
1314
this.sendRequest('q', {'p': pathString}, onComplete);
1415
};
1516

1617
/**
1718
* @param {*} data
1819
* @param {function(*)} onEcho
1920
*/
20-
(PersistentConnection.prototype as any).echo = function(data, onEcho) {
21+
(PersistentConnection.prototype as any).echo = function(data: any, onEcho: (a: any) => any) {
2122
this.sendRequest('echo', {'d': data}, onEcho);
2223
};
2324

@@ -28,8 +29,8 @@ export const RealTimeConnection = Connection;
2829
* @param {function(): string} newHash
2930
* @return {function()}
3031
*/
31-
export const hijackHash = function(newHash) {
32-
var oldPut = PersistentConnection.prototype.put;
32+
export const hijackHash = function(newHash: () => string) {
33+
const oldPut = PersistentConnection.prototype.put;
3334
PersistentConnection.prototype.put = function(pathString, data, opt_onComplete, opt_hash) {
3435
if (opt_hash !== undefined) {
3536
opt_hash = newHash();
@@ -42,23 +43,23 @@ export const hijackHash = function(newHash) {
4243
};
4344

4445
/**
45-
* @type {function(new:fb.core.RepoInfo, !string, boolean, !string, boolean): undefined}
46+
* @type {function(new:RepoInfo, !string, boolean, !string, boolean): undefined}
4647
*/
4748
export const ConnectionTarget = RepoInfo;
4849

4950
/**
50-
* @param {!fb.api.Query} query
51+
* @param {!Query} query
5152
* @return {!string}
5253
*/
53-
export const queryIdentifier = function(query) {
54+
export const queryIdentifier = function(query: Query) {
5455
return query.queryIdentifier();
5556
};
5657

5758
/**
58-
* @param {!fb.api.Query} firebaseRef
59+
* @param {!Query} firebaseRef
5960
* @return {!Object}
6061
*/
61-
export const listens = function(firebaseRef) {
62+
export const listens = function(firebaseRef: Query) {
6263
return firebaseRef.repo.persistentConnection_.listens_;
6364
};
6465

@@ -67,6 +68,6 @@ export const listens = function(firebaseRef) {
6768
*
6869
* @param {boolean} forceRestClient
6970
*/
70-
export const forceRestClient = function(forceRestClient) {
71+
export const forceRestClient = function(forceRestClient: boolean) {
7172
RepoManager.getInstance().forceRestClient(forceRestClient);
7273
};

src/database/core/CompoundWrite.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { forEach } from "../../utils/obj";
44
import { Node, NamedNode } from "./snap/Node";
55
import { PRIORITY_INDEX } from "./snap/indexes/PriorityIndex";
66
import { assert } from "../../utils/assert";
7+
import { ChildrenNode } from './snap/ChildrenNode';
78

89
/**
910
* This class holds a collection of writes that can be applied to nodes in unison. It abstracts away the logic with
@@ -20,7 +21,7 @@ export class CompoundWrite {
2021
* @type {!CompoundWrite}
2122
*/
2223
static Empty = new CompoundWrite(new ImmutableTree(null));
23-
24+
2425
/**
2526
* @param {!Path} path
2627
* @param {!Node} node
@@ -30,33 +31,33 @@ export class CompoundWrite {
3031
if (path.isEmpty()) {
3132
return new CompoundWrite(new ImmutableTree(node));
3233
} else {
33-
var rootmost = this.writeTree_.findRootMostValueAndPath(path);
34+
const rootmost = this.writeTree_.findRootMostValueAndPath(path);
3435
if (rootmost != null) {
35-
var rootMostPath = rootmost.path
36-
var value = rootmost.value;
37-
var relativePath = Path.relativePath(rootMostPath, path);
36+
const rootMostPath = rootmost.path;
37+
let value = rootmost.value;
38+
const relativePath = Path.relativePath(rootMostPath, path);
3839
value = value.updateChild(relativePath, node);
3940
return new CompoundWrite(this.writeTree_.set(rootMostPath, value));
4041
} else {
41-
var subtree = new ImmutableTree(node);
42-
var newWriteTree = this.writeTree_.setTree(path, subtree);
42+
const subtree = new ImmutableTree(node);
43+
const newWriteTree = this.writeTree_.setTree(path, subtree);
4344
return new CompoundWrite(newWriteTree);
4445
}
4546
}
46-
};
47+
}
4748

4849
/**
4950
* @param {!Path} path
5051
* @param {!Object.<string, !Node>} updates
5152
* @return {!CompoundWrite}
5253
*/
5354
addWrites(path: Path, updates: { [name: string]: Node }): CompoundWrite {
54-
var newWrite = <CompoundWrite>this;
55+
let newWrite = <CompoundWrite>this;
5556
forEach(updates, function(childKey, node) {
5657
newWrite = newWrite.addWrite(path.child(childKey), node);
5758
});
5859
return newWrite;
59-
};
60+
}
6061

6162
/**
6263
* Will remove a write at the given path and deeper paths. This will <em>not</em> modify a write at a higher
@@ -69,10 +70,10 @@ export class CompoundWrite {
6970
if (path.isEmpty()) {
7071
return CompoundWrite.Empty;
7172
} else {
72-
var newWriteTree = this.writeTree_.setTree(path, ImmutableTree.Empty);
73+
const newWriteTree = this.writeTree_.setTree(path, ImmutableTree.Empty);
7374
return new CompoundWrite(newWriteTree);
7475
}
75-
};
76+
}
7677

7778
/**
7879
* Returns whether this CompoundWrite will fully overwrite a node at a given location and can therefore be
@@ -83,7 +84,7 @@ export class CompoundWrite {
8384
*/
8485
hasCompleteWrite(path: Path): boolean {
8586
return this.getCompleteNode(path) != null;
86-
};
87+
}
8788

8889
/**
8990
* Returns a node for a path if and only if the node is a "complete" overwrite at that path. This will not aggregate
@@ -93,26 +94,26 @@ export class CompoundWrite {
9394
* @return {?Node} The node if complete at that path, or null otherwise.
9495
*/
9596
getCompleteNode(path: Path): Node {
96-
var rootmost = this.writeTree_.findRootMostValueAndPath(path);
97+
const rootmost = this.writeTree_.findRootMostValueAndPath(path);
9798
if (rootmost != null) {
9899
return this.writeTree_.get(rootmost.path).getChild(Path.relativePath(rootmost.path, path));
99100
} else {
100101
return null;
101102
}
102-
};
103+
}
103104

104105
/**
105106
* Returns all children that are guaranteed to be a complete overwrite.
106107
*
107108
* @return {!Array.<NamedNode>} A list of all complete children.
108109
*/
109110
getCompleteChildren(): Array<NamedNode> {
110-
var children = [];
111-
var node = this.writeTree_.value;
111+
const children = [];
112+
let node = this.writeTree_.value;
112113
if (node != null) {
113114
// If it's a leaf node, it has no children; so nothing to do.
114115
if (!node.isLeafNode()) {
115-
node = /** @type {!ChildrenNode} */ (node);
116+
node = <ChildrenNode>node;
116117
node.forEachChild(PRIORITY_INDEX, function(childName, childNode) {
117118
children.push(new NamedNode(childName, childNode));
118119
});
@@ -125,42 +126,42 @@ export class CompoundWrite {
125126
});
126127
}
127128
return children;
128-
};
129+
}
129130

130131
/**
131132
* @param {!Path} path
132133
* @return {!CompoundWrite}
133134
*/
134-
childCompoundWrite(path: Path) {
135+
childCompoundWrite(path: Path): CompoundWrite {
135136
if (path.isEmpty()) {
136137
return this;
137138
} else {
138-
var shadowingNode = this.getCompleteNode(path);
139+
const shadowingNode = this.getCompleteNode(path);
139140
if (shadowingNode != null) {
140141
return new CompoundWrite(new ImmutableTree(shadowingNode));
141142
} else {
142143
return new CompoundWrite(this.writeTree_.subtree(path));
143144
}
144145
}
145-
};
146+
}
146147

147148
/**
148149
* Returns true if this CompoundWrite is empty and therefore does not modify any nodes.
149150
* @return {boolean} Whether this CompoundWrite is empty
150151
*/
151-
isEmpty() {
152+
isEmpty(): boolean {
152153
return this.writeTree_.isEmpty();
153-
};
154+
}
154155

155156
/**
156157
* Applies this CompoundWrite to a node. The node is returned with all writes from this CompoundWrite applied to the
157158
* node
158159
* @param {!Node} node The node to apply this CompoundWrite to
159160
* @return {!Node} The node with all writes applied
160161
*/
161-
apply(node: Node) {
162+
apply(node: Node): Node {
162163
return CompoundWrite.applySubtreeWrite_(Path.Empty, this.writeTree_, node);
163-
};
164+
}
164165

165166
/**
166167
* @param {!Path} relativePath
@@ -169,12 +170,12 @@ export class CompoundWrite {
169170
* @return {!Node}
170171
* @private
171172
*/
172-
static applySubtreeWrite_ = function(relativePath: Path, writeTree: ImmutableTree, node: Node) {
173+
private static applySubtreeWrite_ = function(relativePath: Path, writeTree: ImmutableTree, node: Node): Node {
173174
if (writeTree.value != null) {
174175
// Since there a write is always a leaf, we're done here
175176
return node.updateChild(relativePath, writeTree.value);
176177
} else {
177-
var priorityWrite = null;
178+
let priorityWrite = null;
178179
writeTree.children.inorderTraversal(function(childKey, childTree) {
179180
if (childKey === '.priority') {
180181
// Apply priorities at the end so we don't update priorities for either empty nodes or forget
@@ -191,6 +192,6 @@ export class CompoundWrite {
191192
}
192193
return node;
193194
}
194-
};
195+
}
195196
}
196197

0 commit comments

Comments
 (0)