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
- 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
+ }
57
70
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 ( {
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' , '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,
@@ -151,7 +163,7 @@ module.exports = (snap, mode) => {
151
163
}
152
164
153
165
// This grabs stateless components
154
- if ( ! componentFound && ( tag === 0 || tag === 1 ) ) {
166
+ if ( ! componentFound && ( tag === 0 || tag === 1 || tag === 2 ) ) {
155
167
newState = 'stateless' ;
156
168
}
157
169
@@ -164,52 +176,55 @@ module.exports = (snap, mode) => {
164
176
treeBaseDuration,
165
177
} ;
166
178
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 ;
171
192
}
172
193
173
194
// Recurse on children
174
- if ( child ) {
195
+
196
+ if ( child ) { // && !circularComponentTable.has(child)) {
175
197
// If this node had state we appended to the children array,
176
198
// so attach children to the newly appended child.
177
199
// 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 ) ;
183
203
}
184
204
// 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
+ }
186
210
211
+ // console.log('linkFiber.js: processed children and sibling, returning tree');
187
212
return tree ;
188
213
}
189
214
190
- // ! BUG: skips 1st hook click
191
215
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
+ }
209
223
}
210
224
211
- return async container => {
212
- // Point fiberRoot to FiberRootNode
225
+ return async ( ) => {
226
+
227
+ const container = document . getElementById ( 'root' ) ;
213
228
if ( container . _internalRoot ) {
214
229
fiberRoot = container . _internalRoot ;
215
230
} else {
@@ -220,28 +235,31 @@ module.exports = (snap, mode) => {
220
235
// Only assign internal root if it actually exists
221
236
fiberRoot = _internalRoot || _reactRootContainer ;
222
237
}
238
+
223
239
const devTools = window . __REACT_DEVTOOLS_GLOBAL_HOOK__ ;
224
- console . log ( 'this is devTools' , devTools ) ;
225
240
const reactInstance = devTools ? devTools . renderers . get ( 1 ) : null ;
226
241
242
+ //console.log('fiberRoot retrieved:', fiberRoot);
227
243
if ( reactInstance && reactInstance . version ) {
228
244
devTools . onCommitFiberRoot = ( function ( original ) {
229
245
return function ( ...args ) {
230
246
fiberRoot = args [ 1 ] ;
231
- console . log ( 'this is fiberRoot' , fiberRoot ) ;
232
247
updateSnapShotTree ( ) ;
233
248
sendSnapshot ( ) ;
234
249
return original ( ...args ) ;
235
250
} ;
236
251
} ( devTools . onCommitFiberRoot ) ) ;
237
252
}
238
253
updateSnapShotTree ( ) ;
254
+ sendSnapshot ( ) ;
255
+ // updateSnapShotTree();
239
256
// Send the initial snapshot once the content script has started up
240
257
// 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
+ // });
246
264
} ;
247
265
} ;
0 commit comments