Skip to content

Commit 06b52a2

Browse files
committed
Reduce complexity of tree & createTree files
1 parent f52b3e0 commit 06b52a2

File tree

3 files changed

+23
-48
lines changed

3 files changed

+23
-48
lines changed

demo-app/src/client/Components/Buttons.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function Buttons() {
1515
hook.
1616
</h4>
1717
{buttons}
18+
{buttons}
1819
</div>
1920
);
2021
}

src/backend/controllers/createTree/createTree.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ export default function createTree(
7979
currentFiberNode: Fiber,
8080
circularComponentTable: Set<Fiber> = new Set(),
8181
tree: Tree = new Tree('root', 'root'),
82-
fromSibling = false,
8382
): Tree {
8483
// Base Case: if has visited the component, return
8584
if (circularComponentTable.has(currentFiberNode)) {
@@ -109,6 +108,7 @@ export default function createTree(
109108
elementType?.render?.name ||
110109
elementType?.name ||
111110
'nameless';
111+
console.log('createTree', { tree });
112112
// console.log('LinkFiber', {
113113
// currentFiberNode,
114114
// // tag,
@@ -148,10 +148,6 @@ export default function createTree(
148148
context: {},
149149
};
150150
let isStatefulComponent = false;
151-
/**
152-
* The updated tree after adding the `componentData` obtained from `currentFiberNode`
153-
*/
154-
let newNode: Tree;
155151

156152
// ----------------APPEND PROP DATA FROM REACT DEV TOOL-----------------------
157153
// check to see if the parent component has any state/props
@@ -295,6 +291,11 @@ export default function createTree(
295291
* `rtid` - The `Root ID` is a unique identifier that is assigned to each React root instance in a React application.
296292
*/
297293
let rtid: string | null = null;
294+
/**
295+
* The updated tree after adding the `componentData` obtained from `currentFiberNode`
296+
*/
297+
let newNode: Tree = tree;
298+
let parentNode: Tree;
298299
// We want to add this fiber node to the snapshot
299300
if (
300301
(isStatefulComponent || newState === 'stateless') &&
@@ -305,7 +306,7 @@ export default function createTree(
305306
rtid = `fromLinkFiber${rtidCounter}`;
306307
// rtid = rtidCounter;
307308
// check if rtid is already present
308-
// remove existing rtid before adding a new one
309+
// remove existing rtid before adding a new one
309310
if (currentFiberNode.child.stateNode.classList.length > 0) {
310311
const lastClass =
311312
currentFiberNode.child.stateNode.classList[
@@ -318,14 +319,9 @@ export default function createTree(
318319
currentFiberNode.child.stateNode.classList.add(rtid);
319320
}
320321
rtidCounter += 1; // I THINK THIS SHOULD BE UP IN THE IF STATEMENT. Still unsure the use of rtid
321-
// checking if tree fromSibling is true
322-
if (fromSibling) {
323-
newNode = tree.addSibling(newState, componentName, componentData, rtid);
324-
} else {
325-
newNode = tree.addChild(newState, componentName, componentData, rtid);
326-
}
327-
} else {
328-
newNode = tree;
322+
323+
// Append the childNode to the tree
324+
[parentNode, newNode] = tree.addChild(newState, componentName, componentData, rtid);
329325
}
330326

331327
// ----------------------TRAVERSE TO NEXT FIBERNODE---------------------------
@@ -334,12 +330,14 @@ export default function createTree(
334330

335331
// If currentFiberNode has siblings, recurse on siblings
336332
if (sibling) {
333+
// If the component was not filtered, add the sibling to the parentNode
337334
if (
338335
(isStatefulComponent || newState === 'stateless') &&
339336
!nextJSDefaultComponent.has(componentName)
340337
)
341-
createTree(sibling, circularComponentTable, newNode, true);
342-
else createTree(sibling, circularComponentTable, newNode, fromSibling);
338+
createTree(sibling, circularComponentTable, parentNode);
339+
340+
else createTree(sibling, circularComponentTable, newNode);
343341
}
344342

345343
// -------------RETURN THE TREE OUTPUT & PASS TO FRONTEND FOR RENDERING-------

src/backend/models/tree.ts

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,17 @@ class Tree {
5656

5757
name: string;
5858

59-
componentData: {
60-
props: {};
61-
};
59+
componentData: {};
6260

6361
children: (Tree | string)[];
6462

6563
parent: Tree;
6664

67-
isExpanded: boolean;
65+
isExpanded: boolean = true;
6866

69-
rtid: any;
67+
rtid: string | null;
7068

71-
route: Route;
69+
route?: Route;
7270

7371
// Duplicate names: add a unique number ID
7472
// Create an object 'componentNames' to store each component name as a key and it's frequency of use as its value
@@ -79,12 +77,11 @@ class Tree {
7977
// EXAMPLE OF COMPONENTNAMES OBJECT: {editableInput: 1, Provider: 0, etc}
8078

8179
constructor(state: string | {}, name = 'nameless', componentData: {} = {}, rtid: any = null) {
80+
this.children = [];
81+
this.componentData = JSON.parse(JSON.stringify(componentData));
8282
this.state = state === 'root' ? 'root' : serializeState(state);
8383
this.name = name;
84-
this.componentData = JSON.parse(JSON.stringify(componentData));
85-
this.children = [];
8684
this.parent = null; // ref to parent so we can add siblings
87-
this.isExpanded = true;
8885
this.rtid = rtid;
8986
}
9087

@@ -123,7 +120,7 @@ class Tree {
123120
* @param rtid - ??
124121
* @returns - return new tree instance that is child
125122
*/
126-
addChild(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
123+
addChild(state: string | {}, name: string, componentData: {}, rtid: any): [Tree, Tree] {
127124
// Get unique name by invoking checkForDuplicates method
128125
const uniqueName = this.checkForDuplicates(name);
129126
console.log('CHILD', { name: uniqueName, this: this });
@@ -134,28 +131,7 @@ class Tree {
134131
// adds newChild to children array
135132
this.children.push(newChild);
136133
// return newChild
137-
return newChild;
138-
}
139-
/**
140-
*
141-
* @param state - string if root, serialized state otherwise
142-
* @param name - name of child
143-
* @param componentData - props
144-
* @param rtid - ??
145-
* @returns - return new tree instance that is child
146-
*/
147-
addSibling(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
148-
// gets unique name by calling checkForDuplicates method
149-
const uniqueName = this.checkForDuplicates(name);
150-
console.log('SIBILING', { name: uniqueName, this: this });
151-
// instantiate new sibilng tree with state, uniqueName, componentName and rtid
152-
const newSibling: Tree = new Tree(state, uniqueName, componentData, rtid);
153-
// updating newSibling's parent to be the parent of "this" which refers to sibling node
154-
newSibling.parent = this.parent;
155-
// adds newSibling to children array
156-
this.parent.children.push(newSibling);
157-
// return newSibling
158-
return newSibling;
134+
return [this, newChild];
159135
}
160136

161137
/**

0 commit comments

Comments
 (0)