Skip to content

Commit f52b3e0

Browse files
committed
Fix the snapshot tree structure
1 parent b593800 commit f52b3e0

File tree

4 files changed

+40
-83
lines changed

4 files changed

+40
-83
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ function Increment() {
1010
<button className='increment' onClick={() => setCount(count + 1)}>
1111
You clicked me {count} times.
1212
</button>
13-
<div>
13+
{/* <div> */}
1414
<Box
1515
value={value}
1616
column={3}
1717
row={2}
1818
handleBoxClick={() => setValue((value: BoardText) => (value == 'X' ? 'O' : 'X'))}
1919
/>
20-
</div>
20+
{/* </div> */}
2121
</div>
2222
);
2323
}

src/backend/controllers/createTree/createTree.ts

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ export default function createTree(
8181
tree: Tree = new Tree('root', 'root'),
8282
fromSibling = false,
8383
): Tree {
84-
// Base case: child or sibling pointed to null
85-
// if (!currentFiberNode) return tree; //TO BE DELETE SINCE THIS FUNCTION CAN ONLY BE RECURSIVE CALLED IF THE FIBERNODE HAS A CHILD/SIBILING
86-
// if (!tree) return tree; //TO BE DELETE: WE HAVE A DEFAULT PARAMETER TREE, THIS WILL NEVER BE TRUE
87-
// These have the newest state. We update state and then
88-
// called updateSnapshotTree()
89-
90-
// Base Case: if has visited, return
84+
// Base Case: if has visited the component, return
9185
if (circularComponentTable.has(currentFiberNode)) {
9286
return;
9387
} else {
@@ -132,30 +126,6 @@ export default function createTree(
132126
// // _debugHookTypes,
133127
// });
134128

135-
// TODO: Understand this if statement
136-
if (tag === HostComponent) {
137-
try {
138-
// Ensure parent component has memoizedProps property
139-
if (
140-
memoizedProps.children &&
141-
memoizedProps.children[0]?._owner?.memoizedProps !== undefined
142-
) {
143-
// Access the memoizedProps of the parent component
144-
const propsData = memoizedProps.children[0]._owner.memoizedProps;
145-
const newPropData = filterAndFormatData(
146-
propsData,
147-
tree.componentData.props ? tree.componentData.props : null,
148-
);
149-
tree.componentData = {
150-
...tree.componentData,
151-
props: newPropData,
152-
};
153-
}
154-
} catch (error) {
155-
console.log(error);
156-
}
157-
}
158-
159129
// -----------------INITIALIZE OBJECT TO CONTAIN COMPONENT DATA---------------
160130
let newState: any | { hooksState?: any[] } = {};
161131
let componentData: {
@@ -183,18 +153,6 @@ export default function createTree(
183153
*/
184154
let newNode: Tree;
185155

186-
// console.log('LinkFiber', {
187-
// currentFiberNode,
188-
// // tag,
189-
// // elementType,
190-
// componentName,
191-
// // memoizedProps,
192-
// // memoizedState,
193-
// // stateNode,
194-
// // dependencies,
195-
// // _debugHookTypes,
196-
// });
197-
198156
// ----------------APPEND PROP DATA FROM REACT DEV TOOL-----------------------
199157
// check to see if the parent component has any state/props
200158
if (
@@ -273,7 +231,6 @@ export default function createTree(
273231
) {
274232
// Save component's state and setState() function to our record for future
275233
// time-travel state changing. Add record index to snapshot so we can retrieve.
276-
console.log('Class Component: ', stateNode);
277234
componentData.index = componentActionsRecord.saveNew(stateNode);
278235
// Save state information in componentData.
279236
componentData.state = stateNode.state;
@@ -314,11 +271,9 @@ export default function createTree(
314271
});
315272
isStatefulComponent = true;
316273
} catch (err) {
317-
console.log('Failed Element', { component: elementType?.name });
318-
// if (!nextJSDefaultComponent.has(elementType?.name)) {
319-
// throw new Error(err);
320-
// } else {
321-
// console.log('We are good');
274+
console.log('ERROR: Failed Element during JSX parsing', {
275+
componentName: elementType?.name,
276+
});
322277
}
323278
}
324279
}
@@ -378,7 +333,14 @@ export default function createTree(
378333
if (child) createTree(child, circularComponentTable, newNode);
379334

380335
// If currentFiberNode has siblings, recurse on siblings
381-
if (sibling) createTree(sibling, circularComponentTable, newNode, true);
336+
if (sibling) {
337+
if (
338+
(isStatefulComponent || newState === 'stateless') &&
339+
!nextJSDefaultComponent.has(componentName)
340+
)
341+
createTree(sibling, circularComponentTable, newNode, true);
342+
else createTree(sibling, circularComponentTable, newNode, fromSibling);
343+
}
382344

383345
// -------------RETURN THE TREE OUTPUT & PASS TO FRONTEND FOR RENDERING-------
384346
return tree;

src/backend/models/tree.ts

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function serializeState(state) {
3434
return JSON.parse(JSON.stringify(state));
3535
} catch (e) {
3636
// if there is an error, that means there is circular state i.e state that depends on itself
37+
console.log('circular');
3738
return 'circularState';
3839
}
3940
}
@@ -51,7 +52,7 @@ export function serializeState(state) {
5152
* @param route - an object representing the route associated with the node.
5253
*/
5354
class Tree {
54-
state: string | {};
55+
state: string | {}; // TODO: should change this to stateless || statefull
5556

5657
name: string;
5758

@@ -77,16 +78,10 @@ class Tree {
7778
// If not, create the new component and also a new key: value pair in 'componentNames' with the component's name as the key and 0 as its value
7879
// EXAMPLE OF COMPONENTNAMES OBJECT: {editableInput: 1, Provider: 0, etc}
7980

80-
constructor(
81-
state: string | {},
82-
name = 'nameless',
83-
componentData: {} = {},
84-
rtid: any = null,
85-
string: any = null,
86-
) {
81+
constructor(state: string | {}, name = 'nameless', componentData: {} = {}, rtid: any = null) {
8782
this.state = state === 'root' ? 'root' : serializeState(state);
8883
this.name = name;
89-
this.componentData = componentData ? { ...JSON.parse(JSON.stringify(componentData)) } : {};
84+
this.componentData = JSON.parse(JSON.stringify(componentData));
9085
this.children = [];
9186
this.parent = null; // ref to parent so we can add siblings
9287
this.isExpanded = true;
@@ -100,26 +95,24 @@ class Tree {
10095
* @returns
10196
*/
10297
checkForDuplicates(name: string): string {
98+
/**
99+
* The condition for check empty name does not seem to ever be reached
100+
* Commented and did not remove for potential future use
101+
*/
103102
// check for empty name
104-
if (name === '' && typeof this.rtid === 'string') {
105-
name = this.rtid.replace('fromLinkFiber', '');
106-
}
107-
// if we are at root, then make sure componentNames is an empty object
108-
if (this.state === 'root') componentNames = {};
109-
// check for duplicate
110-
else if (componentNames[name] !== undefined) {
111-
// if name exists in componentNames object, grab count and add 1
112-
const count: number = componentNames[name] + 1;
113-
// declare a new name variable
114-
const newName: string = name + count;
115-
// update name count in object
116-
componentNames[name] = count;
117-
// return newName
118-
return newName;
119-
}
120-
// add name in componentsName with value of 0
121-
componentNames[name] = 0;
122-
// return name
103+
// if (name === '' && typeof this.rtid === 'string') {
104+
// name = this.rtid.replace('fromLinkFiber', '');
105+
// }
106+
// console.log('Tree', { name, parentName: this.name, this: this });
107+
// if parent node is root, initialize the componentNames object
108+
if (this.name === 'root') componentNames = {};
109+
110+
// Numerize the component name if found duplicate
111+
// Ex: A board has 3 rows => Row, Row1, Row2
112+
// Ex: Each row has 3 boxes => Box, Box1, Box2, ..., Box8
113+
componentNames[name] = componentNames[name] + 1 || 0;
114+
name += componentNames[name] ? componentNames[name] : '';
115+
123116
return name;
124117
}
125118
/**
@@ -131,9 +124,10 @@ class Tree {
131124
* @returns - return new tree instance that is child
132125
*/
133126
addChild(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
134-
// gets unique name by calling checkForDuplicates method
127+
// Get unique name by invoking checkForDuplicates method
135128
const uniqueName = this.checkForDuplicates(name);
136-
// instantiates new child Tree with state, uniqueName, componentData and rtid
129+
console.log('CHILD', { name: uniqueName, this: this });
130+
// Instantiates new child Tree with state, uniqueName, componentData and rtid
137131
const newChild: Tree = new Tree(state, uniqueName, componentData, rtid);
138132
// updating newChild parent to "this"
139133
newChild.parent = this;
@@ -153,6 +147,7 @@ class Tree {
153147
addSibling(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
154148
// gets unique name by calling checkForDuplicates method
155149
const uniqueName = this.checkForDuplicates(name);
150+
console.log('SIBILING', { name: uniqueName, this: this });
156151
// instantiate new sibilng tree with state, uniqueName, componentName and rtid
157152
const newSibling: Tree = new Tree(state, uniqueName, componentData, rtid);
158153
// updating newSibling's parent to be the parent of "this" which refers to sibling node

src/backend/routers/snapShot.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default function updateAndSendSnapShotTree(snapshot: Snapshot, fiberRoot:
2424
const payload = snapshot.tree.cleanTreeCopy();
2525
// Save the current window url to route
2626
payload.route = routes.addRoute(window.location.href);
27-
27+
console.log('send snapshot', { payload });
2828
// method safely enables cross-origin communication between Window objects;
2929
// e.g., between a page and a pop-up that it spawned, or between a page
3030
// and an iframe embedded within it.

0 commit comments

Comments
 (0)