@@ -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,15 +43,32 @@ module.exports = (snap, mode) => {
46
43
component . setState . linkFiberChanged = true ;
47
44
}
48
45
49
- // Helper function to
50
-
46
+ function changeUseState ( component ) {
47
+ if ( component . queue . dispatch . linkFiberChanged ) return ;
48
+ // storing the original dispatch function definition somewhere
49
+ const oldDispatch = component . queue . dispatch . bind ( component . queue ) ;
50
+ // redefining the dispatch function so we can inject our code
51
+ component . queue . dispatch = function ( fiber , queue , action ) {
52
+ console . log ( 'mode' , mode ) ;
53
+ if ( mode . locked && ! mode . jumping ) return ;
54
+ oldDispatch ( fiber , queue , action ) ;
55
+ setTimeout ( ( ) => {
56
+ console . log ( 'Updating the snapshot tree after an action has been dispatched' ) ;
57
+ updateSnapShotTree ( ) ;
58
+ sendSnapshot ( ) ;
59
+ } , 100 ) ;
60
+ } ;
61
+ component . queue . dispatch . linkFiberChanged = true ;
62
+ } ;
63
+
51
64
// Helper function to traverse through the memoized state
52
65
function traverseHooks ( memoizedState ) {
53
66
// Declare variables and assigned to 0th index and an empty object, respectively
54
67
let index = 0 ;
55
68
const memoizedObj = { } ;
56
69
// while memoizedState is truthy, save the value to the object
57
70
while ( memoizedState ) {
71
+ changeUseState ( memoizedState ) ;
58
72
// Increment the index by 1
59
73
memoizedObj [ `state${ index += 1 } ` ] = memoizedState . memoizedState ;
60
74
// Reassign memoizedState to its next value
@@ -85,12 +99,10 @@ module.exports = (snap, mode) => {
85
99
// TODO: Refactor the conditionals - think about the edge case where a stateful
86
100
// component might have a key called 'baseState' in the state
87
101
if ( memoizedState && memoizedState . hasOwnProperty ( 'baseState' ) ) {
88
- // console.log('The memoizedState is: ', memoizedState)
89
-
90
- const traversed = traverseHooks ( memoizedState ) ;
91
- nextTree = tree . appendChild ( traversed ) ;
102
+ console . log ( "I'm not supposed to run" , currentFiber ) ;
103
+ memoizedState . traversed = traverseHooks ( memoizedState ) ;
104
+ nextTree = tree . appendChild ( memoizedState ) ;
92
105
}
93
-
94
106
// iterate through siblings
95
107
createTree ( sibling , tree ) ;
96
108
// iterate through children
@@ -101,24 +113,9 @@ module.exports = (snap, mode) => {
101
113
102
114
function updateSnapShotTree ( ) {
103
115
const { current } = fiberRoot ;
116
+ console . log ( 'current' , current ) ;
104
117
snap . tree = createTree ( current ) ;
105
118
}
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
119
123
120
return {
124
121
_ ( container ) {
@@ -134,36 +131,5 @@ module.exports = (snap, mode) => {
134
131
if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
135
132
} ) ;
136
133
} ,
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 ) ;
146
- updateSnapShotTree ( ) ;
147
- sendSnapshot ( ) ;
148
- } ;
149
- return toReturn ;
150
- } ;
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
- } ,
168
134
} ;
169
- } ;
135
+ } ;
0 commit comments