Skip to content

Commit f044270

Browse files
Yu Jin KangYu Jin Kang
authored andcommitted
updates
2 parents 7db39fd + c3d0126 commit f044270

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

package/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ window.addEventListener('message', ({ data: { action, payload } }) => {
2525
}
2626
});
2727

28-
module.exports = linkFiber;
28+
module.exports = linkFiber;

package/linkFiber.js

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ module.exports = (snap, mode) => {
1212
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
1313
if (mode.jumping || mode.paused) return;
1414
const payload = snap.tree.getCopy();
15+
console.log('Here is the payload:', payload);
1516
// console.log('payload', payload);
1617
window.postMessage({
1718
action: 'recordSnap',
1819
payload,
1920
});
2021
}
2122

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
23+
// DEV: This is how we know when a change has happened
24+
// (by injecting an event listener to every component's setState functionality).
25+
// Will need to create a separate one for useState components
2326
function changeSetState(component) {
2427
// check that setState hasn't been changed yet
2528
if (component.setState.linkFiberChanged) return;
@@ -43,33 +46,32 @@ module.exports = (snap, mode) => {
4346
component.setState.linkFiberChanged = true;
4447
}
4548

49+
// Helper function to
50+
4651
// Helper function to traverse through the memoized state
4752
function traverseHooks(memoizedState) {
4853
// 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+
let index = 0;
55+
const memoizedObj = {};
56+
// while memoizedState is truthy, save the value to the object
57+
while (memoizedState) {
58+
// Increment the index by 1
59+
memoizedObj[`useState${index += 1}`] = memoizedState.memoizedState;
5460
// Reassign memoizedState to its next value
55-
memoizedState = memoizedState.next;
61+
memoizedState = memoizedState.next;
5662
}
57-
return memoizedObj;
63+
return memoizedObj;
5864
}
5965

6066
function createTree(currentFiber, tree = new Tree('root')) {
6167
if (!currentFiber) return tree;
62-
// We have to figure out which properties to destructure from currentFiber
63-
// To support hooks and Context API
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-
}
68+
69+
const {
70+
sibling,
71+
stateNode,
72+
child,
73+
memoizedState,
74+
} = currentFiber;
7375

7476
let nextTree = tree;
7577
// check if stateful component
@@ -79,31 +81,38 @@ module.exports = (snap, mode) => {
7981
// change setState functionality
8082
changeSetState(stateNode);
8183
}
82-
// Need to check if functional component AND uses hooks
83-
84+
// Check if the component uses hooks
85+
// TODO: Refactor the conditionals - think about the edge case where a stateful
86+
// component might have a key called 'baseState' in the state
87+
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
88+
// console.log('The memoizedState is: ', memoizedState)
89+
90+
const result = traverseHooks(memoizedState);
91+
nextTree = tree.appendChild(result);
92+
}
93+
8494
// iterate through siblings
8595
createTree(sibling, tree);
8696
// iterate through children
8797
createTree(child, nextTree);
88-
98+
console.log('this is the tree after being traversed', tree)
8999
return tree;
90100
}
91101

92102
function updateSnapShotTree() {
93-
// console.log('fiberRoot', fiberRoot);
103+
94104
const { current } = fiberRoot;
95-
// console.log('current', current);
96105
snap.tree = createTree(current);
97106
}
98107

99108
return container => {
100-
// console.log('Container', container);
101109
const {
102110
_reactRootContainer: { _internalRoot },
103111
_reactRootContainer,
104112
} = container;
105113
// only assign internal root if it actually exists
106114
fiberRoot = _internalRoot || _reactRootContainer;
115+
console.log('here is the fiberRoot', fiberRoot)
107116
updateSnapShotTree();
108117

109118
// send the initial snapshot once the content script has started up

package/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"react devtool"
2323
],
2424
"author": "Bryan Lee, Josh Kim, Ryan Dang, Sierra Swaby",
25-
"license": "MIT"
25+
"license": "MIT",
26+
"dependencies": {}
2627
}

package/tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Tree {
2828
getCopy(copy = new Tree('root', true)) {
2929
// copy state of children
3030
copy.children = this.children.map(
31-
child => new Tree(child.component.state, true, child.component.constructor.name),
31+
child => new Tree(child.component.state || child.component, true, child.component.constructor.name),
3232
);
3333

3434
// copy children's children recursively

0 commit comments

Comments
 (0)