4545/* eslint-disable no-param-reassign */
4646
4747// const Tree = require('./tree').default;
48- // const componentActionsRecord = require('./masterState');\
49-
48+ // const componentActionsRecord = require('./masterState');
5049import Tree from './tree' ;
5150import componentActionsRecord from './masterState' ;
5251
53- const circularComponentTable = new Map ( ) ;
52+ const DEBUG_MODE = false ;
53+
54+ const alwaysLog = console . log ;
55+
56+ console . log = ( original => {
57+ return ( ...args ) => {
58+ if ( DEBUG_MODE ) original ( ...args ) ;
59+ }
60+ } ) ( console . log ) ;
61+
62+
63+ const circularComponentTable = new Set ( ) ;
5464
5565// module.exports = (snap, mode) => {
5666export default ( snap , mode ) => {
5767 let fiberRoot = null ;
5868
5969 function sendSnapshot ( ) {
70+ alwaysLog ( 'sendSnapshot called' ) ;
6071 // Don't send messages while jumping or while paused
6172 circularComponentTable . clear ( ) ;
6273 // console.log('sending snapshot');
@@ -103,9 +114,11 @@ export default (snap, mode) => {
103114
104115 // Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
105116 //
117+
118+ let ctRunning = 0 ;
106119 function createTree ( currentFiber , tree = new Tree ( 'root' , 'root' ) , fromSibling = false ) {
107120 // Base case: child or sibling pointed to null
108- // console.log('linkFiber.js : creating tree');
121+ console . log ( 'createTree : creating tree' ) ;
109122 if ( ! currentFiber ) return null ;
110123 if ( ! tree ) return tree ;
111124
@@ -129,20 +142,20 @@ export default (snap, mode) => {
129142 let componentFound = false ;
130143
131144 // Check if node is a stateful setState component
132- if ( stateNode && stateNode . state && ( tag === 0 || tag === 1 || tag === 2 ) ) {
145+ if ( stateNode && stateNode . state && ( tag === 0 || tag === 1 || tag === 2 ) ) { // { || tag === 2)) {
133146 // Save component's state and setState() function to our record for future
134147 // time-travel state changing. Add record index to snapshot so we can retrieve.
135- // console.log('linkFiber.js: found stateNode component');
148+ console . log ( 'createTree() found setState component' ) ;
136149 componentData . index = componentActionsRecord . saveNew ( stateNode . state , stateNode ) ;
137150 newState = stateNode . state ;
138151 componentFound = true ;
139152 }
140153
141154 // Check if node is a hooks useState function
142155 let hooksIndex ;
143- if ( memoizedState && ( tag === 0 || tag === 1 || tag === 10 ) ) {
156+ if ( memoizedState && ( tag === 0 || tag === 1 || tag === 2 || tag === 10 ) ) {
144157 if ( memoizedState . queue ) {
145- // console.log('linkFiber.js: found hooks component');
158+ console . log ( 'createTree() found hooks component' ) ;
146159 // Hooks states are stored as a linked list using memoizedState.next,
147160 // so we must traverse through the list and get the states.
148161 // We then store them along with the corresponding memoizedState.queue,
@@ -163,9 +176,10 @@ export default (snap, mode) => {
163176 }
164177
165178 // This grabs stateless components
179+ /*
166180 if (!componentFound && (tag === 0 || tag === 1 || tag === 2)) {
167181 newState = 'stateless';
168- }
182+ }*/
169183
170184 // Adds performance metrics to the component data
171185 componentData = {
@@ -179,52 +193,67 @@ export default (snap, mode) => {
179193 let newNode = null ;
180194 if ( componentFound || newState === 'stateless' ) {
181195 if ( fromSibling ) {
182- newNode = tree . addSibling ( newState ,
183- elementType . name ? elementType . name : elementType ,
196+ console . log ( 'createTree(), relevant component found in sibling' ) ;
197+ newNode = tree . addSibling ( newState ,
198+ elementType ? elementType . name : 'nameless' ,
184199 componentData ) ;
185200 } else {
186- newNode = tree . addChild ( newState ,
187- elementType . name ? elementType . name : elementType ,
201+ console . log ( 'createTree(), relevant component found in child' ) ;
202+ newNode = tree . addChild ( newState ,
203+ elementType ? elementType . name : 'nameless' ,
188204 componentData ) ;
189205 }
190206 } else {
207+ console . log ( 'createTree(), no new relevant nodes, continuing from same node' )
191208 newNode = tree ;
192209 }
193210
194211 // Recurse on children
195212
196- if ( child ) { // && !circularComponentTable.has(child)) {
213+ if ( child && ! circularComponentTable . has ( child ) ) {
197214 // If this node had state we appended to the children array,
198215 // so attach children to the newly appended child.
199216 // Otherwise, attach children to this same node.
200- // console.log('going into child');
201- // circularComponentTable.set (child, true );
217+ console . log ( 'going into child' ) ;
218+ circularComponentTable . add ( child ) ;
202219 createTree ( child , newNode ) ;
203220 }
204221 // Recurse on siblings
205- if ( sibling ) { // && !circularComponentTable.has(sibling)) {
206- // console.log('going into sibling');
207- // circularComponentTable.set (sibling, true );
222+ if ( sibling && ! circularComponentTable . has ( sibling ) ) {
223+ console . log ( 'going into sibling' ) ;
224+ circularComponentTable . add ( sibling ) ;
208225 createTree ( sibling , newNode , true ) ;
209226 }
210227
211- // console.log('linkFiber.js: processed children and sibling, returning tree');
228+ if ( circularComponentTable . has ( child ) ) {
229+ console . log ( 'found circular child, exiting tree loop' ) ;
230+ }
231+
232+ if ( circularComponentTable . has ( sibling ) ) {
233+ console . log ( 'found circular sibling, exiting tree loop' ) ;
234+ }
235+
236+ // // console.log('linkFiber.js: processed children and sibling, returning tree');
212237 return tree ;
213238 }
214239
240+ let updateSnapshotTreeCount = 0 ;
215241 function updateSnapShotTree ( ) {
216- // console.log('linkFiber.js, updateSnapshotTree(), checking if fiberRoot updated');
242+ // console.log('updateSnapshotTree(), checking if fiberRoot updated');
243+
244+ updateSnapshotTreeCount ++ ;
245+ if ( updateSnapshotTreeCount > 1 ) alwaysLog ( 'MULTIPLE SNAPSHOT TREE UPDATES:' , updateSnapshotTreeCount ) ;
217246 if ( fiberRoot ) {
218- // console.log('linkFiber.js, updateSnapshotTree(), updating snapshot', snap.tree);
247+ console . log ( 'updateSnapshotTree(), updating snapshot' , snap . tree ) ;
219248 const { current } = fiberRoot ;
220249 snap . tree = createTree ( current ) ;
221- // console.log('linkFiber.js, updateSnapshotTree(), completed snapshot', snap.tree);
250+ console . log ( 'updateSnapshotTree(), completed snapshot' , snap . tree ) ;
222251 }
252+ updateSnapshotTreeCount -- ;
223253 }
224254
225- return async ( ) => {
226-
227- const container = document . getElementById ( 'root' ) ;
255+ return async ( ) => {
256+ /* const container = document.getElementById('root');
228257 if (container._internalRoot) {
229258 fiberRoot = container._internalRoot;
230259 } else {
@@ -235,17 +264,21 @@ export default (snap, mode) => {
235264 // Only assign internal root if it actually exists
236265 fiberRoot = _internalRoot || _reactRootContainer;
237266 }
238-
267+ */
239268 const devTools = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
240269 const reactInstance = devTools ? devTools . renderers . get ( 1 ) : null ;
241-
242- //console.log('fiberRoot retrieved:', fiberRoot);
270+ fiberRoot = devTools . getFiberRoots ( 1 ) . values ( ) . next ( ) . value ;
271+
272+ alwaysLog ( 'fiberRoot:' , fiberRoot ) ;
243273 if ( reactInstance && reactInstance . version ) {
244274 devTools . onCommitFiberRoot = ( function ( original ) {
245275 return function ( ...args ) {
246276 fiberRoot = args [ 1 ] ;
277+ //console.log('Fiber committed, updating snapshot tree with:', fiberRoot);
247278 updateSnapShotTree ( ) ;
279+ console . log ( 'Fiber committed, sending latest snapshot' ) ;
248280 sendSnapshot ( ) ;
281+ console . log ( 'Fiber committed, latest snapshot sent' ) ;
249282 return original ( ...args ) ;
250283 } ;
251284 } ( devTools . onCommitFiberRoot ) ) ;
0 commit comments