6
6
/* eslint-disable no-console */
7
7
/* eslint-disable no-param-reassign */
8
8
9
- // ~~ I dont like the fact that these are global variables ~~ - Zack
10
9
let copyInstances = 0 ; // Tells you if we have already made a copy of current tree??
11
- const circularComponentTable = new Set < Tree > ( ) ; // Keeps track of the nodes added to the tree
10
+ const circularComponentTable = new Set < Tree > ( ) ; // Keeps track of the nodes added to the tree and allows you make sure there isnt circular state
12
11
let componentNames = { } ; // {componentName: frequency of use} => component name as a key and it's frequency of use as its value
13
12
14
13
// Functions dont serialize properly so we need to scrub for that
@@ -19,12 +18,13 @@ function scrubUnserializableMembers(tree: Tree): Tree {
19
18
return tree ;
20
19
}
21
20
22
- // Making a deep clone of an object
21
+ // Making a deep clone of state becuase we want to make a copy
23
22
function serializeState ( state ) {
24
23
try {
25
24
// makes a deep clone, but this way can be very slow
26
25
return JSON . parse ( JSON . stringify ( state ) ) ;
27
26
} catch ( e ) {
27
+ // if there is an error, that means there is circular state i.e state that depends on itself
28
28
return 'circularState' ;
29
29
}
30
30
}
@@ -37,6 +37,9 @@ function serializeState(state) {
37
37
* @param componentData - {props: {}} - Data in the component tree
38
38
* @param chilren - {(Tree | string)[]} - An array of children nodes
39
39
* @param parent - {Tree} - the parent node
40
+ * @param isExpanded - {boolean}
41
+ * @param rtid - {any}
42
+ * @param route -
40
43
* @parent generates a new tree (recursive call)
41
44
*/
42
45
class Tree {
@@ -106,7 +109,14 @@ class Tree {
106
109
// return name
107
110
return name ;
108
111
}
109
-
112
+ /**
113
+ *
114
+ * @param state - string if root, serialized state otherwise
115
+ * @param name - name of child
116
+ * @param componentData - props
117
+ * @param rtid - ??
118
+ * @returns - return new tree instance that is child
119
+ */
110
120
addChild ( state : string | { } , name : string , componentData : { } , rtid : any ) : Tree {
111
121
// gets unique name by calling checkForDuplicates method
112
122
const uniqueName = this . checkForDuplicates ( name ) ;
@@ -119,13 +129,20 @@ class Tree {
119
129
// return newChild
120
130
return newChild ;
121
131
}
122
-
132
+ /**
133
+ *
134
+ * @param state - string if root, serialized state otherwise
135
+ * @param name - name of child
136
+ * @param componentData - props
137
+ * @param rtid - ??
138
+ * @returns - return new tree instance that is child
139
+ */
123
140
addSibling ( state : string | { } , name : string , componentData : { } , rtid : any ) : Tree {
124
141
// gets unique name by calling checkForDuplicates method
125
142
const uniqueName = this . checkForDuplicates ( name ) ;
126
143
// instantiate new sibilng tree with state, uniqueName, componentName and rtid
127
144
const newSibling : Tree = new Tree ( state , uniqueName , componentData , rtid ) ;
128
- // updating newSibling parent to be the parent of "this" which refers to sibling node
145
+ // updating newSibling's parent to be the parent of "this" which refers to sibling node
129
146
newSibling . parent = this . parent ;
130
147
// adds newSibling to children array
131
148
this . parent . children . push ( newSibling ) ;
@@ -143,27 +160,31 @@ class Tree {
143
160
*/
144
161
// if we havent made a copy of the tree, increment copyInstances and clear cicularComponentTable set
145
162
if ( copyInstances === 0 ) {
163
+ // increment copyInstances
146
164
copyInstances ++ ;
165
+ // clear circularComponentTable
147
166
circularComponentTable . clear ( ) ;
148
167
}
149
168
// creates copy of present node
150
169
let copy : Tree = new Tree ( this . state , this . name , this . componentData , this . rtid ) ;
151
- // you want to get rid of the parentNode?? not sure why
170
+ // you want to get rid of the parentNode becuase right now copy and "this" have the same parent and you dont want that
152
171
delete copy . parent ;
153
172
// add to circularComponentTable
154
173
circularComponentTable . add ( this ) ;
155
- //
174
+ // remove unserializable Trees
156
175
copy = scrubUnserializableMembers ( copy ) ;
157
176
158
- // creates copy of each child of the present node
177
+ // creates copy of each child of the present node and assigns it to children property of the new copy Tree
159
178
copy . children = this . children . map ( ( child : Tree ) : Tree | string => {
179
+ // if child isnt in circularComponent table, return recursive call of cleanTreeCopy() on child. We need to do this to fully build out the tree
160
180
if ( ! circularComponentTable . has ( child ) ) {
161
181
return child . cleanTreeCopy ( ) ;
162
182
}
163
183
return 'circular' ;
164
184
} ) ;
165
-
185
+ // reset copyInstances back to zero becuase we are done making a copy of the tree
166
186
copyInstances -- ;
187
+ // return the copy
167
188
return copy ;
168
189
}
169
190
}
0 commit comments