66/* eslint-disable no-console */
77/* eslint-disable no-param-reassign */
88
9- // ~~ I dont like the fact that these are global variables ~~ - Zack
109let 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
1211let componentNames = { } ; // {componentName: frequency of use} => component name as a key and it's frequency of use as its value
1312
1413// Functions dont serialize properly so we need to scrub for that
@@ -19,12 +18,13 @@ function scrubUnserializableMembers(tree: Tree): Tree {
1918 return tree ;
2019}
2120
22- // Making a deep clone of an object
21+ // Making a deep clone of state becuase we want to make a copy
2322function serializeState ( state ) {
2423 try {
2524 // makes a deep clone, but this way can be very slow
2625 return JSON . parse ( JSON . stringify ( state ) ) ;
2726 } catch ( e ) {
27+ // if there is an error, that means there is circular state i.e state that depends on itself
2828 return 'circularState' ;
2929 }
3030}
@@ -37,6 +37,9 @@ function serializeState(state) {
3737 * @param componentData - {props: {}} - Data in the component tree
3838 * @param chilren - {(Tree | string)[]} - An array of children nodes
3939 * @param parent - {Tree} - the parent node
40+ * @param isExpanded - {boolean}
41+ * @param rtid - {any}
42+ * @param route -
4043 * @parent generates a new tree (recursive call)
4144 */
4245class Tree {
@@ -106,7 +109,14 @@ class Tree {
106109 // return name
107110 return name ;
108111 }
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+ */
110120 addChild ( state : string | { } , name : string , componentData : { } , rtid : any ) : Tree {
111121 // gets unique name by calling checkForDuplicates method
112122 const uniqueName = this . checkForDuplicates ( name ) ;
@@ -119,13 +129,20 @@ class Tree {
119129 // return newChild
120130 return newChild ;
121131 }
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+ */
123140 addSibling ( state : string | { } , name : string , componentData : { } , rtid : any ) : Tree {
124141 // gets unique name by calling checkForDuplicates method
125142 const uniqueName = this . checkForDuplicates ( name ) ;
126143 // instantiate new sibilng tree with state, uniqueName, componentName and rtid
127144 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
129146 newSibling . parent = this . parent ;
130147 // adds newSibling to children array
131148 this . parent . children . push ( newSibling ) ;
@@ -143,27 +160,31 @@ class Tree {
143160 */
144161 // if we havent made a copy of the tree, increment copyInstances and clear cicularComponentTable set
145162 if ( copyInstances === 0 ) {
163+ // increment copyInstances
146164 copyInstances ++ ;
165+ // clear circularComponentTable
147166 circularComponentTable . clear ( ) ;
148167 }
149168 // creates copy of present node
150169 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
152171 delete copy . parent ;
153172 // add to circularComponentTable
154173 circularComponentTable . add ( this ) ;
155- //
174+ // remove unserializable Trees
156175 copy = scrubUnserializableMembers ( copy ) ;
157176
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
159178 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
160180 if ( ! circularComponentTable . has ( child ) ) {
161181 return child . cleanTreeCopy ( ) ;
162182 }
163183 return 'circular' ;
164184 } ) ;
165-
185+ // reset copyInstances back to zero becuase we are done making a copy of the tree
166186 copyInstances -- ;
187+ // return the copy
167188 return copy ;
168189 }
169190}
0 commit comments