@@ -4,6 +4,7 @@ import { forEach } from "../../utils/obj";
4
4
import { Node , NamedNode } from "./snap/Node" ;
5
5
import { PRIORITY_INDEX } from "./snap/indexes/PriorityIndex" ;
6
6
import { assert } from "../../utils/assert" ;
7
+ import { ChildrenNode } from './snap/ChildrenNode' ;
7
8
8
9
/**
9
10
* 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 {
20
21
* @type {!CompoundWrite }
21
22
*/
22
23
static Empty = new CompoundWrite ( new ImmutableTree ( null ) ) ;
23
-
24
+
24
25
/**
25
26
* @param {!Path } path
26
27
* @param {!Node } node
@@ -30,33 +31,33 @@ export class CompoundWrite {
30
31
if ( path . isEmpty ( ) ) {
31
32
return new CompoundWrite ( new ImmutableTree ( node ) ) ;
32
33
} else {
33
- var rootmost = this . writeTree_ . findRootMostValueAndPath ( path ) ;
34
+ const rootmost = this . writeTree_ . findRootMostValueAndPath ( path ) ;
34
35
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 ) ;
38
39
value = value . updateChild ( relativePath , node ) ;
39
40
return new CompoundWrite ( this . writeTree_ . set ( rootMostPath , value ) ) ;
40
41
} 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 ) ;
43
44
return new CompoundWrite ( newWriteTree ) ;
44
45
}
45
46
}
46
- } ;
47
+ }
47
48
48
49
/**
49
50
* @param {!Path } path
50
51
* @param {!Object.<string, !Node> } updates
51
52
* @return {!CompoundWrite }
52
53
*/
53
54
addWrites ( path : Path , updates : { [ name : string ] : Node } ) : CompoundWrite {
54
- var newWrite = < CompoundWrite > this ;
55
+ let newWrite = < CompoundWrite > this ;
55
56
forEach ( updates , function ( childKey , node ) {
56
57
newWrite = newWrite . addWrite ( path . child ( childKey ) , node ) ;
57
58
} ) ;
58
59
return newWrite ;
59
- } ;
60
+ }
60
61
61
62
/**
62
63
* 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 {
69
70
if ( path . isEmpty ( ) ) {
70
71
return CompoundWrite . Empty ;
71
72
} else {
72
- var newWriteTree = this . writeTree_ . setTree ( path , ImmutableTree . Empty ) ;
73
+ const newWriteTree = this . writeTree_ . setTree ( path , ImmutableTree . Empty ) ;
73
74
return new CompoundWrite ( newWriteTree ) ;
74
75
}
75
- } ;
76
+ }
76
77
77
78
/**
78
79
* Returns whether this CompoundWrite will fully overwrite a node at a given location and can therefore be
@@ -83,7 +84,7 @@ export class CompoundWrite {
83
84
*/
84
85
hasCompleteWrite ( path : Path ) : boolean {
85
86
return this . getCompleteNode ( path ) != null ;
86
- } ;
87
+ }
87
88
88
89
/**
89
90
* 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 {
93
94
* @return {?Node } The node if complete at that path, or null otherwise.
94
95
*/
95
96
getCompleteNode ( path : Path ) : Node {
96
- var rootmost = this . writeTree_ . findRootMostValueAndPath ( path ) ;
97
+ const rootmost = this . writeTree_ . findRootMostValueAndPath ( path ) ;
97
98
if ( rootmost != null ) {
98
99
return this . writeTree_ . get ( rootmost . path ) . getChild ( Path . relativePath ( rootmost . path , path ) ) ;
99
100
} else {
100
101
return null ;
101
102
}
102
- } ;
103
+ }
103
104
104
105
/**
105
106
* Returns all children that are guaranteed to be a complete overwrite.
106
107
*
107
108
* @return {!Array.<NamedNode> } A list of all complete children.
108
109
*/
109
110
getCompleteChildren ( ) : Array < NamedNode > {
110
- var children = [ ] ;
111
- var node = this . writeTree_ . value ;
111
+ const children = [ ] ;
112
+ let node = this . writeTree_ . value ;
112
113
if ( node != null ) {
113
114
// If it's a leaf node, it has no children; so nothing to do.
114
115
if ( ! node . isLeafNode ( ) ) {
115
- node = /** @type { ! ChildrenNode} */ ( node ) ;
116
+ node = < ChildrenNode > node ;
116
117
node . forEachChild ( PRIORITY_INDEX , function ( childName , childNode ) {
117
118
children . push ( new NamedNode ( childName , childNode ) ) ;
118
119
} ) ;
@@ -125,42 +126,42 @@ export class CompoundWrite {
125
126
} ) ;
126
127
}
127
128
return children ;
128
- } ;
129
+ }
129
130
130
131
/**
131
132
* @param {!Path } path
132
133
* @return {!CompoundWrite }
133
134
*/
134
- childCompoundWrite ( path : Path ) {
135
+ childCompoundWrite ( path : Path ) : CompoundWrite {
135
136
if ( path . isEmpty ( ) ) {
136
137
return this ;
137
138
} else {
138
- var shadowingNode = this . getCompleteNode ( path ) ;
139
+ const shadowingNode = this . getCompleteNode ( path ) ;
139
140
if ( shadowingNode != null ) {
140
141
return new CompoundWrite ( new ImmutableTree ( shadowingNode ) ) ;
141
142
} else {
142
143
return new CompoundWrite ( this . writeTree_ . subtree ( path ) ) ;
143
144
}
144
145
}
145
- } ;
146
+ }
146
147
147
148
/**
148
149
* Returns true if this CompoundWrite is empty and therefore does not modify any nodes.
149
150
* @return {boolean } Whether this CompoundWrite is empty
150
151
*/
151
- isEmpty ( ) {
152
+ isEmpty ( ) : boolean {
152
153
return this . writeTree_ . isEmpty ( ) ;
153
- } ;
154
+ }
154
155
155
156
/**
156
157
* Applies this CompoundWrite to a node. The node is returned with all writes from this CompoundWrite applied to the
157
158
* node
158
159
* @param {!Node } node The node to apply this CompoundWrite to
159
160
* @return {!Node } The node with all writes applied
160
161
*/
161
- apply ( node : Node ) {
162
+ apply ( node : Node ) : Node {
162
163
return CompoundWrite . applySubtreeWrite_ ( Path . Empty , this . writeTree_ , node ) ;
163
- } ;
164
+ }
164
165
165
166
/**
166
167
* @param {!Path } relativePath
@@ -169,12 +170,12 @@ export class CompoundWrite {
169
170
* @return {!Node }
170
171
* @private
171
172
*/
172
- static applySubtreeWrite_ = function ( relativePath : Path , writeTree : ImmutableTree , node : Node ) {
173
+ private static applySubtreeWrite_ = function ( relativePath : Path , writeTree : ImmutableTree , node : Node ) : Node {
173
174
if ( writeTree . value != null ) {
174
175
// Since there a write is always a leaf, we're done here
175
176
return node . updateChild ( relativePath , writeTree . value ) ;
176
177
} else {
177
- var priorityWrite = null ;
178
+ let priorityWrite = null ;
178
179
writeTree . children . inorderTraversal ( function ( childKey , childTree ) {
179
180
if ( childKey === '.priority' ) {
180
181
// Apply priorities at the end so we don't update priorities for either empty nodes or forget
@@ -191,6 +192,6 @@ export class CompoundWrite {
191
192
}
192
193
return node ;
193
194
}
194
- } ;
195
+ }
195
196
}
196
197
0 commit comments