4444/* eslint-disable no-use-before-define */
4545/* eslint-disable no-param-reassign */
4646
47- const Tree = require ( './tree' ) ;
48- const componentActionsRecord = require ( './masterState' ) ;
47+ // const Tree = require('./tree').default ;
48+ // const componentActionsRecord = require('./masterState');\
4949
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 ) => {
5157 let fiberRoot = null ;
5258
53- async function sendSnapshot ( ) {
59+ function sendSnapshot ( ) {
5460 // Don't send messages while jumping or while paused
61+ circularComponentTable . clear ( ) ;
62+ // console.log('sending snapshot');
5563 if ( mode . jumping || mode . paused ) return ;
56- console . log ( 'PAYLOAD: before cleaning' , snap . tree ) ;
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' , 'root' ) ;
69+ }
5770 const payload = snap . tree . cleanTreeCopy ( ) ; // snap.tree.getCopy();
58- console . log ( 'PAYLOAD: after cleaning' , payload ) ;
59- try {
60- await window . postMessage ( {
71+
72+ // console.log('PAYLOAD: after cleaning', payload);
73+ // try {
74+ // await window.postMessage({
75+ window . postMessage ( {
6176 action : 'recordSnap' ,
6277 payload,
6378 } ) ;
64- } catch ( e ) {
65- console . log ( 'failed to send postMessage:' , e ) ;
66- }
79+ // } catch (e) {
80+ // console.log('failed to send postMessage:', e);
81+ // }
6782 }
6883
6984 // Carlos: Injects instrumentation to update our state tree every time
@@ -75,13 +90,11 @@ module.exports = (snap, mode) => {
7590 // prevents useEffect from crashing on load
7691 // if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
7792 if ( memoizedState . memoizedState ) {
78- console . log ( 'memoizedState in traverseHooks is:' , memoizedState ) ;
7993 hooksStates . push ( {
8094 component : memoizedState . queue ,
8195 state : memoizedState . memoizedState ,
8296 } ) ;
8397 }
84- // console.log('GOT STATE', memoizedState.memoizedState);
8598 memoizedState = memoizedState . next !== memoizedState
8699 ? memoizedState . next : null ;
87100 }
@@ -90,8 +103,9 @@ module.exports = (snap, mode) => {
90103
91104 // Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
92105 //
93- function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
106+ function createTree ( currentFiber , tree = new Tree ( 'root' , 'root' ) , fromSibling = false ) {
94107 // Base case: child or sibling pointed to null
108+ // console.log('linkFiber.js: creating tree');
95109 if ( ! currentFiber ) return null ;
96110 if ( ! tree ) return tree ;
97111
@@ -115,11 +129,10 @@ module.exports = (snap, mode) => {
115129 let componentFound = false ;
116130
117131 // 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 ) ) {
121133 // Save component's state and setState() function to our record for future
122134 // time-travel state changing. Add record index to snapshot so we can retrieve.
135+ // console.log('linkFiber.js: found stateNode component');
123136 componentData . index = componentActionsRecord . saveNew ( stateNode . state , stateNode ) ;
124137 newState = stateNode . state ;
125138 componentFound = true ;
@@ -128,9 +141,8 @@ module.exports = (snap, mode) => {
128141 // Check if node is a hooks useState function
129142 let hooksIndex ;
130143 if ( memoizedState && ( tag === 0 || tag === 1 || tag === 10 ) ) {
131- console . log ( 'in create tree if' ) ;
132- console . log ( 'this is currentFiber from createTree' , currentFiber ) ;
133144 if ( memoizedState . queue ) {
145+ // console.log('linkFiber.js: found hooks component');
134146 // Hooks states are stored as a linked list using memoizedState.next,
135147 // so we must traverse through the list and get the states.
136148 // We then store them along with the corresponding memoizedState.queue,
@@ -151,7 +163,7 @@ module.exports = (snap, mode) => {
151163 }
152164
153165 // This grabs stateless components
154- if ( ! componentFound && ( tag === 0 || tag === 1 ) ) {
166+ if ( ! componentFound && ( tag === 0 || tag === 1 || tag === 2 ) ) {
155167 newState = 'stateless' ;
156168 }
157169
@@ -164,52 +176,55 @@ module.exports = (snap, mode) => {
164176 treeBaseDuration,
165177 } ;
166178
167- if ( componentFound ) {
168- tree . addChild ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
169- } else if ( newState === 'stateless' ) {
170- tree . addChild ( newState , elementType . name ? elementType . name : elementType , componentData ) ;
179+ let newNode = null ;
180+ if ( componentFound || newState === 'stateless' ) {
181+ if ( fromSibling ) {
182+ newNode = tree . addSibling ( newState ,
183+ elementType . name ? elementType . name : elementType ,
184+ componentData ) ;
185+ } else {
186+ newNode = tree . addChild ( newState ,
187+ elementType . name ? elementType . name : elementType ,
188+ componentData ) ;
189+ }
190+ } else {
191+ newNode = tree ;
171192 }
172193
173194 // Recurse on children
174- if ( child ) {
195+
196+ if ( child ) { // && !circularComponentTable.has(child)) {
175197 // If this node had state we appended to the children array,
176198 // so attach children to the newly appended child.
177199 // Otherwise, attach children to this same node.
178- if ( tree . children . length > 0 ) {
179- createTree ( child , tree . children [ tree . children . length - 1 ] ) ;
180- } else {
181- createTree ( child , tree ) ;
182- }
200+ // console.log('going into child');
201+ // circularComponentTable.set(child, true);
202+ createTree ( child , newNode ) ;
183203 }
184204 // Recurse on siblings
185- if ( sibling ) createTree ( sibling , tree ) ;
205+ if ( sibling ) { // && !circularComponentTable.has(sibling)) {
206+ // console.log('going into sibling');
207+ // circularComponentTable.set(sibling, true);
208+ createTree ( sibling , newNode , true ) ;
209+ }
186210
211+ // console.log('linkFiber.js: processed children and sibling, returning tree');
187212 return tree ;
188213 }
189214
190- // ! BUG: skips 1st hook click
191215 function updateSnapShotTree ( ) {
192- /* let current;
193- // If concurrent mode, grab current.child
194- if (concurrent) {
195- // we need a way to wait for current child to populate
196- const promise = new Promise((resolve, reject) => {
197- setTimeout(() => resolve(fiberRoot.current.child), 400);
198- });
199- current = await promise;
200- current = fiberRoot.current.child;
201- } else {
202- current = fiberRoot.current;
203- } */
204- const { current } = fiberRoot ; // Carlos: get rid of concurrent mode for now
205-
206- // console.log('FIBER COMMITTED, new fiber is:', util.inspect(current, false, 4));
207- // fs.appendFile('fiberlog.txt', util.inspect(current, false, 10));
208- snap . tree = createTree ( current ) ; // Carlos: pass new hooks state here?
216+ // console.log('linkFiber.js, updateSnapshotTree(), checking if fiberRoot updated');
217+ if ( fiberRoot ) {
218+ // console.log('linkFiber.js, updateSnapshotTree(), updating snapshot', snap.tree);
219+ const { current } = fiberRoot ;
220+ snap . tree = createTree ( current ) ;
221+ // console.log('linkFiber.js, updateSnapshotTree(), completed snapshot', snap.tree);
222+ }
209223 }
210224
211- return async container => {
212- // Point fiberRoot to FiberRootNode
225+ return async ( ) => {
226+
227+ const container = document . getElementById ( 'root' ) ;
213228 if ( container . _internalRoot ) {
214229 fiberRoot = container . _internalRoot ;
215230 } else {
@@ -220,28 +235,31 @@ module.exports = (snap, mode) => {
220235 // Only assign internal root if it actually exists
221236 fiberRoot = _internalRoot || _reactRootContainer ;
222237 }
238+
223239 const devTools = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
224- console . log ( 'this is devTools' , devTools ) ;
225240 const reactInstance = devTools ? devTools . renderers . get ( 1 ) : null ;
226241
242+ //console.log('fiberRoot retrieved:', fiberRoot);
227243 if ( reactInstance && reactInstance . version ) {
228244 devTools . onCommitFiberRoot = ( function ( original ) {
229245 return function ( ...args ) {
230246 fiberRoot = args [ 1 ] ;
231- console . log ( 'this is fiberRoot' , fiberRoot ) ;
232247 updateSnapShotTree ( ) ;
233248 sendSnapshot ( ) ;
234249 return original ( ...args ) ;
235250 } ;
236251 } ( devTools . onCommitFiberRoot ) ) ;
237252 }
238253 updateSnapShotTree ( ) ;
254+ sendSnapshot ( ) ;
255+ // updateSnapShotTree();
239256 // Send the initial snapshot once the content script has started up
240257 // This message is sent from contentScript.js in chrome extension bundles
241- window . addEventListener ( 'message' , ( { data : { action } } ) => {
242- if ( action === 'contentScriptStarted' ) {
243- sendSnapshot ( ) ;
244- }
245- } ) ;
258+ // window.addEventListener('message', ({ data: { action } }) => {
259+ // if (action === 'contentScriptStarted') {
260+ // // console.log('content script started received at linkFiber.js')
261+ // sendSnapshot();
262+ // }
263+ // });
246264 } ;
247265} ;
0 commit comments