45
45
/* eslint-disable no-param-reassign */
46
46
47
47
// const Tree = require('./tree').default;
48
- // const componentActionsRecord = require('./masterState');\
49
-
48
+ // const componentActionsRecord = require('./masterState');
50
49
import Tree from './tree' ;
51
50
import componentActionsRecord from './masterState' ;
52
51
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 ( ) ;
54
64
55
65
// module.exports = (snap, mode) => {
56
66
export default ( snap , mode ) => {
57
67
let fiberRoot = null ;
58
68
59
69
function sendSnapshot ( ) {
70
+ alwaysLog ( 'sendSnapshot called' ) ;
60
71
// Don't send messages while jumping or while paused
61
72
circularComponentTable . clear ( ) ;
62
73
// console.log('sending snapshot');
@@ -103,9 +114,11 @@ export default (snap, mode) => {
103
114
104
115
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
105
116
//
117
+
118
+ let ctRunning = 0 ;
106
119
function createTree ( currentFiber , tree = new Tree ( 'root' , 'root' ) , fromSibling = false ) {
107
120
// Base case: child or sibling pointed to null
108
- // console.log('linkFiber.js : creating tree');
121
+ console . log ( 'createTree : creating tree' ) ;
109
122
if ( ! currentFiber ) return null ;
110
123
if ( ! tree ) return tree ;
111
124
@@ -129,20 +142,20 @@ export default (snap, mode) => {
129
142
let componentFound = false ;
130
143
131
144
// 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)) {
133
146
// Save component's state and setState() function to our record for future
134
147
// 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' ) ;
136
149
componentData . index = componentActionsRecord . saveNew ( stateNode . state , stateNode ) ;
137
150
newState = stateNode . state ;
138
151
componentFound = true ;
139
152
}
140
153
141
154
// Check if node is a hooks useState function
142
155
let hooksIndex ;
143
- if ( memoizedState && ( tag === 0 || tag === 1 || tag === 10 ) ) {
156
+ if ( memoizedState && ( tag === 0 || tag === 1 || tag === 2 || tag === 10 ) ) {
144
157
if ( memoizedState . queue ) {
145
- // console.log('linkFiber.js: found hooks component');
158
+ console . log ( 'createTree() found hooks component' ) ;
146
159
// Hooks states are stored as a linked list using memoizedState.next,
147
160
// so we must traverse through the list and get the states.
148
161
// We then store them along with the corresponding memoizedState.queue,
@@ -163,9 +176,10 @@ export default (snap, mode) => {
163
176
}
164
177
165
178
// This grabs stateless components
179
+ /*
166
180
if (!componentFound && (tag === 0 || tag === 1 || tag === 2)) {
167
181
newState = 'stateless';
168
- }
182
+ }*/
169
183
170
184
// Adds performance metrics to the component data
171
185
componentData = {
@@ -179,52 +193,67 @@ export default (snap, mode) => {
179
193
let newNode = null ;
180
194
if ( componentFound || newState === 'stateless' ) {
181
195
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' ,
184
199
componentData ) ;
185
200
} 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' ,
188
204
componentData ) ;
189
205
}
190
206
} else {
207
+ console . log ( 'createTree(), no new relevant nodes, continuing from same node' )
191
208
newNode = tree ;
192
209
}
193
210
194
211
// Recurse on children
195
212
196
- if ( child ) { // && !circularComponentTable.has(child)) {
213
+ if ( child && ! circularComponentTable . has ( child ) ) {
197
214
// If this node had state we appended to the children array,
198
215
// so attach children to the newly appended child.
199
216
// 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 ) ;
202
219
createTree ( child , newNode ) ;
203
220
}
204
221
// 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 ) ;
208
225
createTree ( sibling , newNode , true ) ;
209
226
}
210
227
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');
212
237
return tree ;
213
238
}
214
239
240
+ let updateSnapshotTreeCount = 0 ;
215
241
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 ) ;
217
246
if ( fiberRoot ) {
218
- // console.log('linkFiber.js, updateSnapshotTree(), updating snapshot', snap.tree);
247
+ console . log ( 'updateSnapshotTree(), updating snapshot' , snap . tree ) ;
219
248
const { current } = fiberRoot ;
220
249
snap . tree = createTree ( current ) ;
221
- // console.log('linkFiber.js, updateSnapshotTree(), completed snapshot', snap.tree);
250
+ console . log ( 'updateSnapshotTree(), completed snapshot' , snap . tree ) ;
222
251
}
252
+ updateSnapshotTreeCount -- ;
223
253
}
224
254
225
- return async ( ) => {
226
-
227
- const container = document . getElementById ( 'root' ) ;
255
+ return async ( ) => {
256
+ /* const container = document.getElementById('root');
228
257
if (container._internalRoot) {
229
258
fiberRoot = container._internalRoot;
230
259
} else {
@@ -235,17 +264,21 @@ export default (snap, mode) => {
235
264
// Only assign internal root if it actually exists
236
265
fiberRoot = _internalRoot || _reactRootContainer;
237
266
}
238
-
267
+ */
239
268
const devTools = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
240
269
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 ) ;
243
273
if ( reactInstance && reactInstance . version ) {
244
274
devTools . onCommitFiberRoot = ( function ( original ) {
245
275
return function ( ...args ) {
246
276
fiberRoot = args [ 1 ] ;
277
+ //console.log('Fiber committed, updating snapshot tree with:', fiberRoot);
247
278
updateSnapShotTree ( ) ;
279
+ console . log ( 'Fiber committed, sending latest snapshot' ) ;
248
280
sendSnapshot ( ) ;
281
+ console . log ( 'Fiber committed, latest snapshot sent' ) ;
249
282
return original ( ...args ) ;
250
283
} ;
251
284
} ( devTools . onCommitFiberRoot ) ) ;
0 commit comments