@@ -20,7 +20,9 @@ module.exports = (snap, mode) => {
20
20
} ) ;
21
21
}
22
22
23
- // 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
24
26
function changeSetState ( component ) {
25
27
// check that setState hasn't been changed yet
26
28
if ( component . setState . linkFiberChanged ) return ;
@@ -44,6 +46,8 @@ module.exports = (snap, mode) => {
44
46
component . setState . linkFiberChanged = true ;
45
47
}
46
48
49
+ // Helper function to
50
+
47
51
// Helper function to traverse through the memoized state
48
52
function traverseHooks ( memoizedState ) {
49
53
// Declare variables and assigned to 0th index and an empty object, respectively
@@ -52,7 +56,7 @@ module.exports = (snap, mode) => {
52
56
// while memoizedState is truthy, save the value to the object
53
57
while ( memoizedState ) {
54
58
// Increment the index by 1
55
- memoizedObj [ `useState ${ index += 1 } ` ] = memoizedState . memoizedState ;
59
+ memoizedObj [ `state ${ index += 1 } ` ] = memoizedState . memoizedState ;
56
60
// Reassign memoizedState to its next value
57
61
memoizedState = memoizedState . next ;
58
62
}
@@ -61,8 +65,7 @@ module.exports = (snap, mode) => {
61
65
62
66
function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
63
67
if ( ! currentFiber ) return tree ;
64
- // We have to figure out which properties to destructure from currentFiber
65
- // To support hooks and Context API
68
+
66
69
const {
67
70
sibling,
68
71
stateNode,
@@ -82,10 +85,10 @@ module.exports = (snap, mode) => {
82
85
// TODO: Refactor the conditionals - think about the edge case where a stateful
83
86
// component might have a key called 'baseState' in the state
84
87
if ( memoizedState && memoizedState . hasOwnProperty ( 'baseState' ) ) {
85
- // console.log('The memoizedState is: ', memoizedState);
86
- const result = traverseHooks ( memoizedState ) ;
87
- console . log ( 'This is the result of calling traverseHooks:' , result ) ;
88
- nextTree = tree . appendChild ( result ) ;
88
+ // console.log('The memoizedState is: ', memoizedState)
89
+
90
+ const traversed = traverseHooks ( memoizedState ) ;
91
+ nextTree = tree . appendChild ( traversed ) ;
89
92
}
90
93
91
94
// iterate through siblings
@@ -97,21 +100,18 @@ module.exports = (snap, mode) => {
97
100
}
98
101
99
102
function updateSnapShotTree ( ) {
100
- // console.log('fiberRoot', fiberRoot);
101
103
const { current } = fiberRoot ;
102
- console . log ( 'current' , current ) ;
103
104
snap . tree = createTree ( current ) ;
104
105
}
105
-
106
106
// return container => {
107
- // // console.log('Container ', container);
107
+ // console.log('this is the container ', container)
108
108
// const {
109
109
// _reactRootContainer: { _internalRoot },
110
110
// _reactRootContainer,
111
- // } = container;
112
- // // console.log('Root container', _reactRootContainer);
111
+ // } = container;
113
112
// // only assign internal root if it actually exists
114
113
// fiberRoot = _internalRoot || _reactRootContainer;
114
+ // console.log('fiberRoot', fiberRoot);
115
115
// updateSnapShotTree();
116
116
117
117
// // send the initial snapshot once the content script has started up
@@ -121,37 +121,49 @@ module.exports = (snap, mode) => {
121
121
// };
122
122
123
123
return {
124
- main ( container ) {
125
- // console.log('Container', container);
124
+ _ ( container ) {
126
125
const {
127
126
_reactRootContainer : { _internalRoot } ,
128
127
_reactRootContainer,
129
128
} = container ;
130
- // console.log('Root container', _reactRootContainer);
131
129
// only assign internal root if it actually exists
132
130
fiberRoot = _internalRoot || _reactRootContainer ;
133
131
updateSnapShotTree ( ) ;
134
-
135
132
// send the initial snapshot once the content script has started up
136
133
window . addEventListener ( 'message' , ( { data : { action } } ) => {
137
134
if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
138
135
} ) ;
139
136
} ,
140
- changeUseState ( useState ) {
141
- return function ( initial ) {
137
+ testUseState ( useState ) {
138
+ return function ( initial ) {
142
139
// running the original useState and storing its result (state and dispatch function)
143
140
const toReturn = useState ( initial ) ;
144
- console . log ( toReturn ) ;
145
141
// storing the original dispatch function definition somewhere
146
142
const oldDispatch = toReturn [ 1 ] ;
147
143
// redefining the dispatch function so we can inject our code
148
- toReturn [ 1 ] = function ( callback ) {
149
- oldDispatch ( callback ) ;
144
+ toReturn [ 1 ] = function ( newVal ) {
145
+ oldDispatch ( newVal ) ;
150
146
updateSnapShotTree ( ) ;
151
147
sendSnapshot ( ) ;
152
148
} ;
153
149
return toReturn ;
154
150
} ;
155
151
} ,
152
+ testUseReducer ( useReducer ) {
153
+ return function ( reducer , initialState , init ) {
154
+ // Declare a constant and initialize to the built-in useReducer method
155
+ // Which returns an array with the state and dispatch
156
+ const reduced = useReducer ( reducer , initialState , init ) ;
157
+ // Save the dispatch method
158
+ const oldDispatch = reduced [ 1 ] ;
159
+ // reassign the dispatch method with the additional methods
160
+ reduced [ 1 ] = function ( type ) {
161
+ oldDispatch ( type ) ;
162
+ updateSnapShotTree ( ) ;
163
+ sendSnapshot ( ) ;
164
+ }
165
+ return reduced ;
166
+ }
167
+ } ,
156
168
} ;
157
169
} ;
0 commit comments