@@ -12,14 +12,17 @@ module.exports = (snap, mode) => {
12
12
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
13
13
if ( mode . jumping || mode . paused ) return ;
14
14
const payload = snap . tree . getCopy ( ) ;
15
+ console . log ( 'Here is the payload:' , payload ) ;
15
16
// console.log('payload', payload);
16
17
window . postMessage ( {
17
18
action : 'recordSnap' ,
18
19
payload,
19
20
} ) ;
20
21
}
21
22
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
23
26
function changeSetState ( component ) {
24
27
// check that setState hasn't been changed yet
25
28
if ( component . setState . linkFiberChanged ) return ;
@@ -43,33 +46,32 @@ module.exports = (snap, mode) => {
43
46
component . setState . linkFiberChanged = true ;
44
47
}
45
48
49
+ // Helper function to
50
+
46
51
// Helper function to traverse through the memoized state
47
52
function traverseHooks ( memoizedState ) {
48
53
// 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 ;
54
60
// Reassign memoizedState to its next value
55
- memoizedState = memoizedState . next ;
61
+ memoizedState = memoizedState . next ;
56
62
}
57
- return memoizedObj ;
63
+ return memoizedObj ;
58
64
}
59
65
60
66
function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
61
67
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 ;
73
75
74
76
let nextTree = tree ;
75
77
// check if stateful component
@@ -79,31 +81,38 @@ module.exports = (snap, mode) => {
79
81
// change setState functionality
80
82
changeSetState ( stateNode ) ;
81
83
}
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
+
84
94
// iterate through siblings
85
95
createTree ( sibling , tree ) ;
86
96
// iterate through children
87
97
createTree ( child , nextTree ) ;
88
-
98
+ console . log ( 'this is the tree after being traversed' , tree )
89
99
return tree ;
90
100
}
91
101
92
102
function updateSnapShotTree ( ) {
93
- // console.log('fiberRoot', fiberRoot);
103
+
94
104
const { current } = fiberRoot ;
95
- // console.log('current', current);
96
105
snap . tree = createTree ( current ) ;
97
106
}
98
107
99
108
return container => {
100
- // console.log('Container', container);
101
109
const {
102
110
_reactRootContainer : { _internalRoot } ,
103
111
_reactRootContainer,
104
112
} = container ;
105
113
// only assign internal root if it actually exists
106
114
fiberRoot = _internalRoot || _reactRootContainer ;
115
+ console . log ( 'here is the fiberRoot' , fiberRoot )
107
116
updateSnapShotTree ( ) ;
108
117
109
118
// send the initial snapshot once the content script has started up
0 commit comments