@@ -20,9 +20,6 @@ module.exports = (snap, mode) => {
20
20
} ) ;
21
21
}
22
22
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
26
23
function changeSetState ( component ) {
27
24
// check that setState hasn't been changed yet
28
25
if ( component . setState . linkFiberChanged ) return ;
@@ -46,8 +43,6 @@ module.exports = (snap, mode) => {
46
43
component . setState . linkFiberChanged = true ;
47
44
}
48
45
49
- // Helper function to
50
-
51
46
// Helper function to traverse through the memoized state
52
47
function traverseHooks ( memoizedState ) {
53
48
// Declare variables and assigned to 0th index and an empty object, respectively
@@ -87,7 +82,7 @@ module.exports = (snap, mode) => {
87
82
if ( memoizedState && memoizedState . hasOwnProperty ( 'baseState' ) ) {
88
83
// console.log('The memoizedState is: ', memoizedState)
89
84
90
- const traversed = traverseHooks ( memoizedState ) ;
85
+ const traversed = traverseHooks ( memoizedState ) ;
91
86
nextTree = tree . appendChild ( traversed ) ;
92
87
}
93
88
@@ -103,22 +98,6 @@ module.exports = (snap, mode) => {
103
98
const { current } = fiberRoot ;
104
99
snap . tree = createTree ( current ) ;
105
100
}
106
- // return container => {
107
- // console.log('this is the container', container)
108
- // const {
109
- // _reactRootContainer: { _internalRoot },
110
- // _reactRootContainer,
111
- // } = container;
112
- // // only assign internal root if it actually exists
113
- // fiberRoot = _internalRoot || _reactRootContainer;
114
- // console.log('fiberRoot', fiberRoot);
115
- // updateSnapShotTree();
116
-
117
- // // send the initial snapshot once the content script has started up
118
- // window.addEventListener('message', ({ data: { action } }) => {
119
- // if (action === 'contentScriptStarted') sendSnapshot();
120
- // });
121
- // };
122
101
123
102
return {
124
103
_ ( container ) {
@@ -134,35 +113,80 @@ module.exports = (snap, mode) => {
134
113
if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
135
114
} ) ;
136
115
} ,
137
- testUseState ( useState ) {
138
- return function ( initial ) {
139
- // running the original useState and storing its result (state and dispatch function)
140
- const toReturn = useState ( initial ) ;
141
- // storing the original dispatch function definition somewhere
142
- const oldDispatch = toReturn [ 1 ] ;
143
- // redefining the dispatch function so we can inject our code
144
- toReturn [ 1 ] = function ( newVal ) {
145
- oldDispatch ( newVal ) ;
116
+ // testUseState(useState) {
117
+ // return initial => {
118
+ // // running the original useState and storing its result (state and dispatch function)
119
+ // const toReturn = useState(initial);
120
+ // // storing the original dispatch function definition somewhere
121
+ // const oldDispatch = toReturn[1];
122
+ // // redefining the dispatch function so we can inject our code
123
+ // toReturn[1] = function (newVal) {
124
+ // console.log('dispatch:', oldDispatch(newVal));
125
+ // setTimeout(() => {
126
+ // updateSnapShotTree();
127
+ // sendSnapshot();
128
+ // }, 100);
129
+ // };
130
+ // return toReturn;
131
+ // };
132
+ // },
133
+ testHooks ( react ) {
134
+ const reducerInjection = reducer => {
135
+ return ( state , action ) => {
136
+ reducer ( state , action ) ;
146
137
updateSnapShotTree ( ) ;
147
138
sendSnapshot ( ) ;
148
139
} ;
149
- return toReturn ;
140
+ } ;
141
+ return {
142
+ useState : initial => {
143
+ const basicStateReducerClone = ( state , action ) => {
144
+ return typeof action === 'function' ? action ( state ) : action ;
145
+ } ;
146
+ // running the original useState and storing its result (state and dispatch function)
147
+ const toReturn = react . useReducer ( initial , reducerInjection ( basicStateReducerClone ) ) ;
148
+ // // storing the original dispatch function definition somewhere
149
+ // const oldDispatch = toReturn[1];
150
+ // // redefining the dispatch function so we can inject our code
151
+ // toReturn[1] = function (newVal) {
152
+ // console.log('dispatch:', oldDispatch(newVal));
153
+ // setTimeout(() => {
154
+ // updateSnapShotTree();
155
+ // sendSnapshot();
156
+ // }, 100);
157
+ // };
158
+ return toReturn ;
159
+ } ,
160
+ useReducer : ( reducer , initialState , init ) => {
161
+ // Declare a constant and initialize to the built-in useReducer method
162
+ // Which returns an array with the state and dispatch
163
+ const reduced = react . useReducer ( reducer , initialState , init ) ;
164
+ // Save the dispatch method
165
+ const oldDispatch = reduced [ 1 ] ;
166
+ // reassign the dispatch method with the additional methods
167
+ reduced [ 1 ] = function ( type ) {
168
+ oldDispatch ( type ) ;
169
+ updateSnapShotTree ( ) ;
170
+ sendSnapshot ( ) ;
171
+ } ;
172
+ return reduced ;
173
+ } ,
150
174
} ;
151
175
} ,
152
176
testUseReducer ( useReducer ) {
153
- return function ( reducer , initialState , init ) {
177
+ return ( reducer , initialState , init ) => {
154
178
// Declare a constant and initialize to the built-in useReducer method
155
179
// Which returns an array with the state and dispatch
156
180
const reduced = useReducer ( reducer , initialState , init ) ;
157
181
// Save the dispatch method
158
182
const oldDispatch = reduced [ 1 ] ;
159
183
// reassign the dispatch method with the additional methods
160
- reduced [ 1 ] = function ( type ) {
184
+ reduced [ 1 ] = function ( type ) {
161
185
oldDispatch ( type ) ;
162
186
updateSnapShotTree ( ) ;
163
- sendSnapshot ( ) ;
187
+ sendSnapshot ( ) ;
164
188
}
165
- return reduced ;
189
+ return reduced ;
166
190
}
167
191
} ,
168
192
} ;
0 commit comments