42
42
/* eslint-disable no-param-reassign */
43
43
44
44
const Tree = require ( './tree' ) ;
45
- const { saveState , saveHooksComponent } = require ( './masterState' ) ;
45
+ const componentActionsRecord = require ( './masterState' ) ;
46
46
47
47
module . exports = ( snap , mode ) => {
48
48
let fiberRoot = null ;
@@ -74,21 +74,22 @@ module.exports = (snap, mode) => {
74
74
// prevents useEffect from crashing on load
75
75
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
76
76
if ( memoizedState . memoizedState ) {
77
+ console . log ( 'memoizedState in traverseHooks is:' , memoizedState ) ;
77
78
hooksComponents . push ( {
78
- action : memoizedState . queue . dispatch ,
79
+ component : memoizedState . queue ,
79
80
state : memoizedState . memoizedState ,
80
81
} ) ;
81
82
}
82
- //console.log('GOT STATE', memoizedState.memoizedState);
83
+ // console.log('GOT STATE', memoizedState.memoizedState);
83
84
memoizedState = memoizedState . next !== memoizedState
84
85
? memoizedState . next : null ;
85
86
}
86
87
return hooksComponents ;
87
88
}
88
89
89
90
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
90
- //
91
- function createTree ( currentFiber , tree = new Tree ( 'root' ) , hooksDispatch ) {
91
+ //
92
+ function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
92
93
// Base case: child or sibling pointed to null
93
94
if ( ! currentFiber ) return tree ;
94
95
@@ -103,55 +104,30 @@ module.exports = (snap, mode) => {
103
104
tag,
104
105
} = currentFiber ;
105
106
106
- let nextTree = tree ;
107
-
108
- // Check if stateful component
107
+ let index ;
108
+ // Check if node is a stateful component
109
109
if ( stateNode && stateNode . state && ( tag === 0 || tag === 1 ) ) {
110
- // Carlos: this is a Tree class object, which has an appendChild
111
- // method that adds stateNode to an array. We should refactor
112
- // into variable because there is always at most one element in the array
113
- tree . appendChild ( stateNode . state , elementType . name ) ; // Add component to tree
114
- Map ( currentFiber , UUID ) - > UUID
115
- tree . components [ elementType . name ] = stateNode . setState ;
110
+ // Save component's state and setState() function to our record for future
111
+ // time-travel state changing. Add record index to snapshot so we can retrieve.
112
+ index = componentActionsRecord . saveNew ( stateNode . state , stateNode ) ;
113
+ tree . appendChild ( stateNode . state , elementType . name , index ) ; // Add component to tree
116
114
} else {
117
115
// grab stateless components here
118
116
}
119
117
120
- // Check if the component uses hooks
121
- if (
122
- memoizedState
123
- && ( tag === 0 || tag === 1 || tag === 10 )
124
- ) {
125
-
126
- // Traverse through the currentFiber and extract the getters/setters
127
- // astHooks = astParser(elementType);
128
- // console.log('astHooks: ', astHooks);
129
- // saveState(astHooks);
130
- // Create a traversed property and assign to the evaluated result of
131
- // invoking traverseHooks with memoizedState
132
- // Carlos: try passing new state in updateSnapShotTree instead ****
133
- // memoizedState.traversed = traverseHooks(memoizedState);
118
+ // Check if node is a hooks function
119
+ if ( memoizedState && ( tag === 0 || tag === 1 || tag === 10 ) ) {
134
120
if ( memoizedState . queue ) {
135
121
const hooksComponents = traverseHooks ( memoizedState ) ;
136
122
hooksComponents . forEach ( c => {
137
123
if ( elementType . name ) {
138
- const hooksSnapshot = { [ elementType . name ] : { action : c . action , state : c . state } } ;
139
- if ( hooksSnapshot ) saveHooksComponent ( hooksSnapshot ) ;
140
- tree . appendChild ( { state : c . state } , elementType . name ? elementType . name : 'nameless' ) ;
141
- // console.log('GOT STATE, HOOKS SNAPSHOT:', hooksSnapshot);
124
+ index = componentActionsRecord . saveNew ( c . state , c . component ) ;
125
+ tree . appendChild ( c . state , elementType . name ? elementType . name : 'nameless' , index ) ;
142
126
}
143
127
} ) ;
144
128
}
145
-
146
- /*
147
- if (hooksDispatch) {
148
- memoizedState.component = { hooksDispatch };
149
- console.log('hooks dispatch args saved:', memoizedState);
150
- nextTree = tree.appendChild(memoizedState.hooksDispatch);
151
- } */
152
129
}
153
130
154
-
155
131
// Recurse on siblings
156
132
createTree ( sibling , tree ) ;
157
133
// Recurse on children
@@ -165,27 +141,24 @@ module.exports = (snap, mode) => {
165
141
}
166
142
167
143
// ! BUG: skips 1st hook click
168
- function updateSnapShotTree ( hooksDispatch ) {
144
+ function updateSnapShotTree ( ) {
169
145
/* let current;
170
146
// If concurrent mode, grab current.child
171
147
if (concurrent) {
172
148
// we need a way to wait for current child to populate
173
149
const promise = new Promise((resolve, reject) => {
174
150
setTimeout(() => resolve(fiberRoot.current.child), 400);
175
151
});
176
-
177
152
current = await promise;
178
-
179
153
current = fiberRoot.current.child;
180
- } else {
154
+ } else {
181
155
current = fiberRoot.current;
182
156
} */
183
- const current = fiberRoot . current ; // Carlos: get rid of concurrent mode for now
157
+ const { current } = fiberRoot ; // Carlos: get rid of concurrent mode for now
184
158
185
159
// console.log('FIBER COMMITTED, new fiber is:', util.inspect(current, false, 4));
186
160
// fs.appendFile('fiberlog.txt', util.inspect(current, false, 10));
187
- snap . tree = createTree ( current , undefined , hooksDispatch ) ; // Carlos: pass new hooks state here?
188
- // console.log("GOT STATE, MOVING TO CHILD/SIBLING, tree:", snap.tree);
161
+ snap . tree = createTree ( current ) ; // Carlos: pass new hooks state here?
189
162
}
190
163
191
164
return async container => {
@@ -205,6 +178,9 @@ module.exports = (snap, mode) => {
205
178
}
206
179
const devTools = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
207
180
const reactInstance = devTools ? devTools . renderers . get ( 1 ) : null ;
181
+ const overrideHookState = reactInstance ? reactInstance . overrideHookState : null ;
182
+ console . log ( 'DEVTOOLS:' , devTools ) ;
183
+ console . log ( 'roots:' , reactInstance . getCurrentFiber ( ) )
208
184
209
185
if ( reactInstance && reactInstance . version ) {
210
186
devTools . onCommitFiberRoot = ( function ( original ) {
0 commit comments