Skip to content

Commit 21a0902

Browse files
committed
WIP: flesh out Node interface, implement interface on classes
1 parent ea43b0c commit 21a0902

File tree

4 files changed

+115
-126
lines changed

4 files changed

+115
-126
lines changed

src/database/core/snap/ChildrenNode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
MIN_NAME
66
} from "../util/util";
77
import { SortedMap } from "../util/SortedMap";
8-
import { NamedNode } from "./Node";
8+
import { Node, NamedNode } from "./Node";
99
import {
1010
validatePriorityNode,
1111
priorityHashText,
@@ -30,7 +30,7 @@ let EMPTY_NODE;
3030
* @param {?Node} priorityNode The priority of this node (as a snapshot node).
3131
* @param {!IndexMap} indexMap
3232
*/
33-
export class ChildrenNode {
33+
export class ChildrenNode implements Node {
3434
children_;
3535
priorityNode_;
3636
indexMap_;
@@ -491,7 +491,7 @@ export class MaxNode extends ChildrenNode {
491491

492492

493493
getImmediateChild(childName) {
494-
ChildrenNode.EMPTY_NODE;
494+
return ChildrenNode.EMPTY_NODE;
495495
};
496496

497497

src/database/core/snap/LeafNode.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import {
88
validatePriorityNode
99
} from "./snap";
1010
import { ChildrenNode } from "./ChildrenNode";
11+
import { Node } from "./Node";
1112

1213

1314
/**
1415
* LeafNode is a class for storing leaf nodes in a DataSnapshot. It
1516
* implements Node and stores the value of the node (a string,
1617
* number, or boolean) accessible via getValue().
1718
*/
18-
export class LeafNode {
19+
export class LeafNode implements Node {
1920
value_;
2021
priorityNode_;
2122
lazyHash_;
@@ -128,9 +129,7 @@ export class LeafNode {
128129
assert(front !== '.priority' || path.getLength() === 1,
129130
'.priority must be the last token in a path');
130131

131-
return this.updateImmediateChild(front,
132-
ChildrenNode.EMPTY_NODE.updateChild(path.popFront(),
133-
newChildNode));
132+
return this.updateImmediateChild(front, ChildrenNode.EMPTY_NODE.updateChild(path.popFront(), newChildNode));
134133
}
135134
}
136135

src/database/core/snap/MaxNode.ts

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

src/database/core/snap/Node.ts

Lines changed: 109 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,142 +1,142 @@
1+
import { Path } from "../util/path";
2+
import { Index } from "./indexes/Index";
3+
14
/**
25
* Node is an interface defining the common functionality for nodes in
36
* a DataSnapshot.
47
*
58
* @interface
69
*/
7-
export const Node = function() { };
8-
9-
10-
/**
11-
* Whether this node is a leaf node.
12-
* @return {boolean} Whether this is a leaf node.
13-
*/
14-
Node.prototype.isLeafNode;
15-
16-
17-
/**
18-
* Gets the priority of the node.
19-
* @return {!Node} The priority of the node.
20-
*/
21-
Node.prototype.getPriority;
10+
export interface Node {
11+
/**
12+
* Whether this node is a leaf node.
13+
* @return {boolean} Whether this is a leaf node.
14+
*/
15+
isLeafNode(): boolean;
2216

2317

24-
/**
25-
* Returns a duplicate node with the new priority.
26-
* @param {!Node} newPriorityNode New priority to set for the node.
27-
* @return {!Node} Node with new priority.
28-
*/
29-
Node.prototype.updatePriority;
18+
/**
19+
* Gets the priority of the node.
20+
* @return {!Node} The priority of the node.
21+
*/
22+
getPriority(): Node;
3023

3124

32-
/**
33-
* Returns the specified immediate child, or null if it doesn't exist.
34-
* @param {string} childName The name of the child to retrieve.
35-
* @return {!Node} The retrieved child, or an empty node.
36-
*/
37-
Node.prototype.getImmediateChild;
25+
/**
26+
* Returns a duplicate node with the new priority.
27+
* @param {!Node} newPriorityNode New priority to set for the node.
28+
* @return {!Node} Node with new priority.
29+
*/
30+
updatePriority(newPriorityNode: Node): Node;
3831

3932

40-
/**
41-
* Returns a child by path, or null if it doesn't exist.
42-
* @param {!fb.core.util.Path} path The path of the child to retrieve.
43-
* @return {!Node} The retrieved child or an empty node.
44-
*/
45-
Node.prototype.getChild;
33+
/**
34+
* Returns the specified immediate child, or null if it doesn't exist.
35+
* @param {string} childName The name of the child to retrieve.
36+
* @return {!Node} The retrieved child, or an empty node.
37+
*/
38+
getImmediateChild(childName: string): Node;
4639

4740

48-
/**
49-
* Returns the name of the child immediately prior to the specified childNode, or null.
50-
* @param {!string} childName The name of the child to find the predecessor of.
51-
* @param {!Node} childNode The node to find the predecessor of.
52-
* @param {!fb.core.snap.Index} index The index to use to determine the predecessor
53-
* @return {?string} The name of the predecessor child, or null if childNode is the first child.
54-
*/
55-
Node.prototype.getPredecessorChildName;
41+
/**
42+
* Returns a child by path, or null if it doesn't exist.
43+
* @param {!Path} path The path of the child to retrieve.
44+
* @return {!Node} The retrieved child or an empty node.
45+
*/
46+
getChild(path: Path): Node;
5647

57-
/**
58-
* Returns a duplicate node, with the specified immediate child updated.
59-
* Any value in the node will be removed.
60-
* @param {string} childName The name of the child to update.
61-
* @param {!Node} newChildNode The new child node
62-
* @return {!Node} The updated node.
63-
*/
64-
Node.prototype.updateImmediateChild;
6548

49+
/**
50+
* Returns the name of the child immediately prior to the specified childNode, or null.
51+
* @param {!string} childName The name of the child to find the predecessor of.
52+
* @param {!Node} childNode The node to find the predecessor of.
53+
* @param {!Index} index The index to use to determine the predecessor
54+
* @return {?string} The name of the predecessor child, or null if childNode is the first child.
55+
*/
56+
getPredecessorChildName(childName: String, childNode: Node, index: Index): string;
6657

67-
/**
68-
* Returns a duplicate node, with the specified child updated. Any value will
69-
* be removed.
70-
* @param {!fb.core.util.Path} path The path of the child to update.
71-
* @param {!Node} newChildNode The new child node, which may be an empty node
72-
* @return {!Node} The updated node.
73-
*/
74-
Node.prototype.updateChild;
58+
/**
59+
* Returns a duplicate node, with the specified immediate child updated.
60+
* Any value in the node will be removed.
61+
* @param {string} childName The name of the child to update.
62+
* @param {!Node} newChildNode The new child node
63+
* @return {!Node} The updated node.
64+
*/
65+
updateImmediateChild(childName: string, newChildNode: Node): Node;
7566

76-
/**
77-
* True if the immediate child specified exists
78-
* @param {!string} childName
79-
* @return {boolean}
80-
*/
81-
Node.prototype.hasChild;
8267

83-
/**
84-
* @return {boolean} True if this node has no value or children.
85-
*/
86-
Node.prototype.isEmpty;
68+
/**
69+
* Returns a duplicate node, with the specified child updated. Any value will
70+
* be removed.
71+
* @param {!Path} path The path of the child to update.
72+
* @param {!Node} newChildNode The new child node, which may be an empty node
73+
* @return {!Node} The updated node.
74+
*/
75+
updateChild(path: Path, newChildNode: Node): Node;
8776

77+
/**
78+
* True if the immediate child specified exists
79+
* @param {!string} childName
80+
* @return {boolean}
81+
*/
82+
hasChild(childName: string): boolean;
8883

89-
/**
90-
* @return {number} The number of children of this node.
91-
*/
92-
Node.prototype.numChildren;
84+
/**
85+
* @return {boolean} True if this node has no value or children.
86+
*/
87+
isEmpty(): boolean;
9388

9489

95-
/**
96-
* Calls action for each child.
97-
* @param {!fb.core.snap.Index} index
98-
* @param {function(string, !Node)} action Action to be called for
99-
* each child. It's passed the child name and the child node.
100-
* @return {*} The first truthy value return by action, or the last falsey one
101-
*/
102-
Node.prototype.forEachChild;
90+
/**
91+
* @return {number} The number of children of this node.
92+
*/
93+
numChildren(): number;
10394

10495

105-
/**
106-
* @param {boolean=} opt_exportFormat True for export format (also wire protocol format).
107-
* @return {*} Value of this node as JSON.
108-
*/
109-
Node.prototype.val;
96+
/**
97+
* Calls action for each child.
98+
* @param {!Index} index
99+
* @param {function(string, !Node)} action Action to be called for
100+
* each child. It's passed the child name and the child node.
101+
* @return {*} The first truthy value return by action, or the last falsey one
102+
*/
103+
forEachChild(index: Index, action: (string, node) => any): any;
110104

105+
/**
106+
* @param {boolean=} opt_exportFormat True for export format (also wire protocol format).
107+
* @return {*} Value of this node as JSON.
108+
*/
109+
val(exportFormat?: boolean): Object;
111110

112-
/**
113-
* @return {string} hash representing the node contents.
114-
*/
115-
Node.prototype.hash;
111+
/**
112+
* @return {string} hash representing the node contents.
113+
*/
114+
hash(): string;
116115

117-
/**
118-
* @param {!Node} other Another node
119-
* @return {!number} -1 for less than, 0 for equal, 1 for greater than other
120-
*/
121-
Node.prototype.compareTo;
116+
/**
117+
* @param {!Node} other Another node
118+
* @return {!number} -1 for less than, 0 for equal, 1 for greater than other
119+
*/
120+
compareTo(other: Node): number;
122121

123-
/**
124-
* @param {!Node} other
125-
* @return {boolean} Whether or not this snapshot equals other
126-
*/
127-
Node.prototype.equals;
122+
/**
123+
* @param {!Node} other
124+
* @return {boolean} Whether or not this snapshot equals other
125+
*/
126+
equals(other: Node): boolean;
128127

129-
/**
130-
* @param {!fb.core.snap.Index} indexDefinition
131-
* @return {!Node} This node, with the specified index now available
132-
*/
133-
Node.prototype.withIndex;
128+
/**
129+
* @param {!Index} indexDefinition
130+
* @return {!Node} This node, with the specified index now available
131+
*/
132+
withIndex(indexDefinition: Index): Node;
134133

135-
/**
136-
* @param {!fb.core.snap.Index} indexDefinition
137-
* @return {boolean}
138-
*/
139-
Node.prototype.isIndexed;
134+
/**
135+
* @param {!Index} indexDefinition
136+
* @return {boolean}
137+
*/
138+
isIndexed(indexDefinition: Index): boolean;
139+
}
140140

141141
/**
142142
*
@@ -146,8 +146,7 @@ Node.prototype.isIndexed;
146146
* @struct
147147
*/
148148
export class NamedNode {
149-
name;
150-
node;
149+
constructor(public name: string, public node: Node) {}
151150

152151
/**
153152
*
@@ -158,10 +157,5 @@ export class NamedNode {
158157
static Wrap(name: string, node: Node) {
159158
return new NamedNode(name, node);
160159
}
161-
162-
constructor(name, node) {
163-
this.name = name;
164-
this.node = node;
165-
}
166160
}
167161

0 commit comments

Comments
 (0)