44
44
/* eslint-disable no-use-before-define */
45
45
/* eslint-disable no-param-reassign */
46
46
47
- const Tree = require ( './tree' ) ;
48
- const componentActionsRecord = require ( './masterState' ) ;
47
+ // const Tree = require('./tree').default ;
48
+ // const componentActionsRecord = require('./masterState');\
49
49
50
- module . exports = ( snap , mode ) => {
50
+ import Tree from './tree' ;
51
+ import componentActionsRecord from './masterState' ;
52
+
53
+ const circularComponentTable = new Map ( ) ;
54
+
55
+ // module.exports = (snap, mode) => {
56
+ export default ( snap , mode ) => {
51
57
let fiberRoot = null ;
52
58
53
- async function sendSnapshot ( ) {
59
+ function sendSnapshot ( ) {
54
60
// Don't send messages while jumping or while paused
61
+ circularComponentTable . clear ( ) ;
62
+ console . log ( 'sending snapshot' ) ;
55
63
if ( mode . jumping || mode . paused ) return ;
56
64
console . log ( 'PAYLOAD: before cleaning' , snap . tree ) ;
65
+
66
+ if ( ! snap . tree ) {
67
+ console . log ( 'snapshot empty, sending root' ) ;
68
+ snap . tree = new Tree ( 'root' ) ;
69
+ }
57
70
const payload = snap . tree . cleanTreeCopy ( ) ; // snap.tree.getCopy();
71
+
58
72
console . log ( 'PAYLOAD: after cleaning' , payload ) ;
59
- try {
60
- await window . postMessage ( {
73
+ //try {
74
+ //await window.postMessage({
75
+ window . postMessage ( {
61
76
action : 'recordSnap' ,
62
77
payload,
63
78
} ) ;
64
- } catch ( e ) {
65
- console . log ( 'failed to send postMessage:' , e ) ;
66
- }
79
+ // } catch (e) {
80
+ // console.log('failed to send postMessage:', e);
81
+ // }
67
82
}
68
83
69
84
// Carlos: Injects instrumentation to update our state tree every time
@@ -75,13 +90,11 @@ module.exports = (snap, mode) => {
75
90
// prevents useEffect from crashing on load
76
91
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
77
92
if ( memoizedState . memoizedState ) {
78
- console . log ( 'memoizedState in traverseHooks is:' , memoizedState ) ;
79
93
hooksStates . push ( {
80
94
component : memoizedState . queue ,
81
95
state : memoizedState . memoizedState ,
82
96
} ) ;
83
97
}
84
- // console.log('GOT STATE', memoizedState.memoizedState);
85
98
memoizedState = memoizedState . next !== memoizedState
86
99
? memoizedState . next : null ;
87
100
}
@@ -90,8 +103,9 @@ module.exports = (snap, mode) => {
90
103
91
104
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
92
105
//
93
- function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
106
+ function createTree ( currentFiber , tree = new Tree ( 'root' ) , fromSibling = false ) {
94
107
// Base case: child or sibling pointed to null
108
+ console . log ( 'linkFiber.js: creating tree' ) ;
95
109
if ( ! currentFiber ) return null ;
96
110
if ( ! tree ) return tree ;
97
111
@@ -115,11 +129,10 @@ module.exports = (snap, mode) => {
115
129
let componentFound = false ;
116
130
117
131
// Check if node is a stateful setState component
118
- if ( stateNode && stateNode . state && ( tag === 0 || tag === 1 ) ) {
119
- console . log ( 'in create tree if' )
120
- console . log ( 'this is currentFiber from createTree' , currentFiber )
132
+ if ( stateNode && stateNode . state && ( tag === 0 || tag === 1 || tag === 2 ) ) {
121
133
// Save component's state and setState() function to our record for future
122
134
// time-travel state changing. Add record index to snapshot so we can retrieve.
135
+ console . log ( 'linkFiber.js: found stateNode component' ) ;
123
136
componentData . index = componentActionsRecord . saveNew ( stateNode . state , stateNode ) ;
124
137
newState = stateNode . state ;
125
138
componentFound = true ;
@@ -128,9 +141,8 @@ module.exports = (snap, mode) => {
128
141
// Check if node is a hooks useState function
129
142
let hooksIndex ;
130
143
if ( memoizedState && ( tag === 0 || tag === 1 || tag === 10 ) ) {
131
- console . log ( 'in create tree if' )
132
- console . log ( 'this is currentFiber from createTree' , currentFiber )
133
144
if ( memoizedState . queue ) {
145
+ console . log ( 'linkFiber.js: found hooks component' ) ;
134
146
// Hooks states are stored as a linked list using memoizedState.next,
135
147
// so we must traverse through the list and get the states.
136
148
// We then store them along with the corresponding memoizedState.queue,
@@ -164,103 +176,80 @@ module.exports = (snap, mode) => {
164
176
treeBaseDuration,
165
177
} ;
166
178
179
+ console . log ( 'linkFiber.js: adding new state to tree:' , newState ) ;
167
180
if ( componentFound ) {
168
- tree . addChild ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
181
+ console . log ( 'componentFound, calling tree.addChild' ) ;
182
+ if ( fromSibling ) {
183
+ tree . addSibling ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
184
+ } else {
185
+ tree . addChild ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
186
+ }
169
187
} else if ( newState === 'stateless' ) {
170
- tree . addChild ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
188
+ if ( fromSibling ) {
189
+ tree . addSibling ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
190
+ } else {
191
+ tree . addChild ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
192
+ }
171
193
}
172
194
173
195
// Recurse on children
174
- if ( child ) {
196
+
197
+ if ( child ) { // && !circularComponentTable.has(child)) {
175
198
// If this node had state we appended to the children array,
176
199
// so attach children to the newly appended child.
177
200
// Otherwise, attach children to this same node.
201
+ console . log ( 'going into child' ) ;
202
+ // circularComponentTable.set(child, true);
178
203
if ( tree . children . length > 0 ) {
179
204
createTree ( child , tree . children [ tree . children . length - 1 ] ) ;
180
205
} else {
181
206
createTree ( child , tree ) ;
182
207
}
183
208
}
184
209
// Recurse on siblings
185
- if ( sibling ) createTree ( sibling , tree ) ;
210
+ if ( sibling ) { // && !circularComponentTable.has(sibling)) {
211
+ console . log ( 'going into sibling' ) ;
212
+ // circularComponentTable.set(sibling, true);
213
+ createTree ( sibling , tree , true ) ;
214
+ }
186
215
216
+ console . log ( 'linkFiber.js: processed children and sibling, returning tree' ) ;
187
217
return tree ;
188
218
}
189
219
190
-
191
- // function createUnfilteredTree(curFiber, parentNode) {
192
- // // on call from updateSnapShot, no parentNode provided, so create a root node
193
- // if(! parentNode) parentNode = new Tree('root');
194
-
195
- // // Base case: parentNode's child or sibling pointed to null
196
- // if (!curFiber) return parentNode;
197
-
198
- // let newChildNode = null;
199
-
200
- // // If stateful, add to parentNode's children array, then inject new setState into fiber node
201
- // if (curFiber.stateNode && curFiber.stateNode.state) {
202
- // newChildNode = parentNode.appendChild(curFiber.stateNode);
203
- // // changeSetState(curFiber.stateNode);
204
- // newChildNode.isStateful = true;
205
- // }
206
- // else {
207
-
208
- // }
209
-
210
- // // Recurse to sibling; siblings that have state should be added to our parentNode
211
- // createTree(curFiber.sibling, parentNode);
212
-
213
- // // Recurse to child; If this fiber was stateful, then we added a newChildNode here, and we want
214
- // // to attach further children to that. If this fiber wasn't stateful, we want to attach any
215
- // // children to our existing parentNode.
216
- // createTree(curFiber.child, newChildNode || parentNode);
217
-
218
- // return parentNode;
219
- // }
220
-
221
-
222
- // ! BUG: skips 1st hook click
223
220
function updateSnapShotTree ( ) {
224
- /* let current;
225
- // If concurrent mode, grab current.child
226
- if (concurrent) {
227
- // we need a way to wait for current child to populate
228
- const promise = new Promise((resolve, reject) => {
229
- setTimeout(() => resolve(fiberRoot.current.child), 400);
230
- });
231
- current = await promise;
232
- current = fiberRoot.current.child;
233
- } else {
234
- current = fiberRoot.current;
235
- } */
236
- const { current } = fiberRoot ; // Carlos: get rid of concurrent mode for now
237
-
238
- // console.log('FIBER COMMITTED, new fiber is:', util.inspect(current, false, 4));
239
- // fs.appendFile('fiberlog.txt', util.inspect(current, false, 10));
240
- snap . tree = createTree ( current ) ; // Carlos: pass new hooks state here?
221
+ console . log ( 'linkFiber.js, updateSnapshotTree(), checking if we have fiberRoot to update' ) ;
222
+ if ( fiberRoot ) {
223
+ console . log ( 'linkFiber.js, updateSnapshotTree(), fiberRoot found, updating snapshot' , snap . tree ) ;
224
+ const { current } = fiberRoot ;
225
+ snap . tree = createTree ( current ) ;
226
+ console . log ( 'linkFiber.js, updateSnapshotTree(), completed snapshot' , snap . tree ) ;
227
+ }
241
228
}
242
229
243
- return async container => {
244
- // Point fiberRoot to FiberRootNode
245
- if ( container . _internalRoot ) {
246
- fiberRoot = container . _internalRoot ;
247
- } else {
248
- const {
249
- _reactRootContainer : { _internalRoot } ,
250
- _reactRootContainer ,
251
- } = container ;
252
- // Only assign internal root if it actually exists
253
- fiberRoot = _internalRoot || _reactRootContainer ;
254
- }
230
+ return async ( ) => {
231
+ // if (container._internalRoot) {
232
+ // fiberRoot = container._internalRoot;
233
+ // } else {
234
+ // const {
235
+ // _reactRootContainer: { _internalRoot },
236
+ // _reactRootContainer ,
237
+ // } = container;
238
+ // // Only assign internal root if it actually exists
239
+ // fiberRoot = _internalRoot || _reactRootContainer;
240
+ // }
241
+
255
242
const devTools = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
256
243
const reactInstance = devTools ? devTools . renderers . get ( 1 ) : null ;
244
+ console . log ( 'devTools:' , devTools ) ;
257
245
258
246
if ( reactInstance && reactInstance . version ) {
259
247
devTools . onCommitFiberRoot = ( function ( original ) {
260
248
return function ( ...args ) {
261
249
fiberRoot = args [ 1 ] ;
262
- console . log ( 'this is fiberRoot' , fiberRoot )
250
+ console . log ( 'this is fiberRoot' , fiberRoot ) ;
263
251
updateSnapShotTree ( ) ;
252
+ console . log ( 'snap.tree is: ' , snap . tree ) ;
264
253
sendSnapshot ( ) ;
265
254
return original ( ...args ) ;
266
255
} ;
@@ -271,6 +260,7 @@ module.exports = (snap, mode) => {
271
260
// This message is sent from contentScript.js in chrome extension bundles
272
261
window . addEventListener ( 'message' , ( { data : { action } } ) => {
273
262
if ( action === 'contentScriptStarted' ) {
263
+ console . log ( 'content script started received at linkFiber.js' )
274
264
sendSnapshot ( ) ;
275
265
}
276
266
} ) ;
0 commit comments