@@ -43,13 +43,34 @@ module.exports = (snap, mode) => {
43
43
component . setState . linkFiberChanged = true ;
44
44
}
45
45
46
+ function changeUseState ( component ) {
47
+ if ( component . queue . dispatch . linkFiberChanged ) return ;
48
+ // running the original useState and storing its result (state and dispatch function)
49
+ const oldDispatch = component . queue . dispatch . bind ( component . queue ) ;
50
+ // storing the original dispatch function definition somewhere
51
+ console . log ( 'inside changeusestate' , oldDispatch ) ;
52
+
53
+ // redefining the dispatch function so we can inject our code
54
+ component . queue . dispatch = function ( fiber , queue , action ) {
55
+ if ( mode . locked && ! mode . jumping ) return ;
56
+ oldDispatch ( fiber , queue , action ) ;
57
+ setTimeout ( ( ) => {
58
+ console . log ( 'Updating the snapshot tree after an action has been dispatched' ) ;
59
+ updateSnapShotTree ( ) ;
60
+ sendSnapshot ( ) ;
61
+ } , 100 ) ;
62
+ } ;
63
+ component . queue . dispatch . linkFiberChanged = true ;
64
+ } ;
65
+
46
66
// Helper function to traverse through the memoized state
47
67
function traverseHooks ( memoizedState ) {
48
68
// Declare variables and assigned to 0th index and an empty object, respectively
49
69
let index = 0 ;
50
70
const memoizedObj = { } ;
51
71
// while memoizedState is truthy, save the value to the object
52
72
while ( memoizedState ) {
73
+ changeUseState ( memoizedState ) ;
53
74
// Increment the index by 1
54
75
memoizedObj [ `state${ index += 1 } ` ] = memoizedState . memoizedState ;
55
76
// Reassign memoizedState to its next value
@@ -80,10 +101,8 @@ module.exports = (snap, mode) => {
80
101
// TODO: Refactor the conditionals - think about the edge case where a stateful
81
102
// component might have a key called 'baseState' in the state
82
103
if ( memoizedState && memoizedState . hasOwnProperty ( 'baseState' ) ) {
83
- // console.log('The memoizedState is: ', memoizedState)
84
-
85
- const traversed = traverseHooks ( memoizedState ) ;
86
- nextTree = tree . appendChild ( traversed ) ;
104
+ memoizedState . traversed = traverseHooks ( memoizedState ) ;
105
+ nextTree = tree . appendChild ( memoizedState ) ;
87
106
}
88
107
89
108
// iterate through siblings
@@ -113,81 +132,41 @@ module.exports = (snap, mode) => {
113
132
if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
114
133
} ) ;
115
134
} ,
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;
135
+ // testHooks(react) {
136
+ // return {
137
+ // useState: initialState => {
138
+ // // running the original useState and storing its result (state and dispatch function)
139
+ // const toReturn = react.useState(initialState);
140
+ // // storing the original dispatch function definition somewhere
141
+ // const oldDispatch = toReturn[1];
142
+ // console.log('old dispatch', oldDispatch)
143
+ // // redefining the dispatch function so we can inject our code
144
+ // toReturn[1] = function (newVal) {
145
+ // oldDispatch(newVal);
146
+ // setTimeout(() => {
147
+ // updateSnapShotTree();
148
+ // sendSnapshot();
149
+ // }, 100);
150
+ // };
151
+ // return toReturn;
152
+ // },
153
+ // useReducer: (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 = react.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
+ // setTimeout(() => {
163
+ // updateSnapShotTree();
164
+ // sendSnapshot();
165
+ // }, 100);
166
+ // };
167
+ // return reduced;
168
+ // },
131
169
// };
132
170
// },
133
- testHooks ( react ) {
134
- const reducerInjection = reducer => {
135
- return ( state , action ) => {
136
- reducer ( state , action ) ;
137
- updateSnapShotTree ( ) ;
138
- sendSnapshot ( ) ;
139
- } ;
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
- } ,
174
- } ;
175
- } ,
176
- testUseReducer ( useReducer ) {
177
- return ( reducer , initialState , init ) => {
178
- // Declare a constant and initialize to the built-in useReducer method
179
- // Which returns an array with the state and dispatch
180
- const reduced = useReducer ( reducer , initialState , init ) ;
181
- // Save the dispatch method
182
- const oldDispatch = reduced [ 1 ] ;
183
- // reassign the dispatch method with the additional methods
184
- reduced [ 1 ] = function ( type ) {
185
- oldDispatch ( type ) ;
186
- updateSnapShotTree ( ) ;
187
- sendSnapshot ( ) ;
188
- }
189
- return reduced ;
190
- }
191
- } ,
192
171
} ;
193
- } ;
172
+ } ;
0 commit comments