Skip to content

Commit 7db39fd

Browse files
Yu Jin KangYu Jin Kang
authored andcommitted
updates
2 parents a344149 + 4f5f25e commit 7db39fd

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

package/linkFiber.js

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ module.exports = (snap, mode) => {
99

1010
function sendSnapshot() {
1111
// don't send messages while jumping or while paused
12+
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
1213
if (mode.jumping || mode.paused) return;
1314
const payload = snap.tree.getCopy();
15+
// console.log('payload', payload);
1416
window.postMessage({
1517
action: 'recordSnap',
1618
payload,
1719
});
1820
}
1921

22+
// DEV: This is how we know when a change has happened (by injecting an event listener to every component's setState functionality). Will need to create a separate one for useState components
2023
function changeSetState(component) {
2124
// check that setState hasn't been changed yet
2225
if (component.setState.linkFiberChanged) return;
@@ -40,20 +43,33 @@ module.exports = (snap, mode) => {
4043
component.setState.linkFiberChanged = true;
4144
}
4245

46+
// Helper function to traverse through the memoized state
47+
function traverseHooks(memoizedState) {
48+
// Declare variables and assigned to 0th index and an empty object, respectively
49+
let index = 0;
50+
let memoizedObj = {};
51+
// while memoizedState is truthy, save the value to the object
52+
while (memoizedState) {
53+
memoizedObj[index++] = memoizedState.memoizedState;
54+
// Reassign memoizedState to its next value
55+
memoizedState = memoizedState.next;
56+
}
57+
return memoizedObj;
58+
}
59+
4360
function createTree(currentFiber, tree = new Tree('root')) {
4461
if (!currentFiber) return tree;
4562
// We have to figure out which properties to destructure from currentFiber
4663
// To support hooks and Context API
47-
// Potential properties (pulling from `current`):
48-
// memoizedState (initialized to null and it refers to the state used to create the output)
49-
50-
// updateQueue ? >> refers to a queue of state updates and callbacks
51-
// React adds the callback from setState to the updateQueue and schedules work
52-
53-
54-
55-
56-
const { sibling, stateNode, child } = currentFiber;
64+
const { sibling, stateNode, child, memoizedState } = currentFiber;
65+
console.log('here is the currentFiber', currentFiber)
66+
// TODO: Refactor the conditionals - think about the edge case where a stateful component might have a key called 'baseState' in the state
67+
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
68+
// console.log('The hook element is:', currentFiber);
69+
// console.log('The memoizedState is: ', memoizedState);
70+
traverseHooks(memoizedState);
71+
//console.log('This is the result of calling traverseHooks:', result);
72+
}
5773

5874
let nextTree = tree;
5975
// check if stateful component
@@ -74,11 +90,14 @@ module.exports = (snap, mode) => {
7490
}
7591

7692
function updateSnapShotTree() {
93+
// console.log('fiberRoot', fiberRoot);
7794
const { current } = fiberRoot;
95+
// console.log('current', current);
7896
snap.tree = createTree(current);
7997
}
8098

8199
return container => {
100+
// console.log('Container', container);
82101
const {
83102
_reactRootContainer: { _internalRoot },
84103
_reactRootContainer,

package/tree.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Tree {
1414
this.name = name;
1515
}
1616
this.children = [];
17+
// DEV: Added print() for debugging purposes
18+
// this.print();
1719
}
1820

1921
appendChild(component) {
@@ -35,10 +37,12 @@ class Tree {
3537
}
3638

3739
// print out the tree in the console
40+
// DEV: Process may be different for useState components
3841
// BUG FIX: Don't print the Router as a component
3942
// Change how the children are printed
4043
print() {
4144
const children = ['children: '];
45+
// DEV: What should we push instead for components using hooks (it wouldn't be state)
4246
this.children.forEach(child => {
4347
children.push(child.state || child.component.state);
4448
});

0 commit comments

Comments
 (0)