Skip to content

Commit b81a56a

Browse files
committed
merge
2 parents 26fd771 + 72bdf0a commit b81a56a

File tree

1 file changed

+29
-36
lines changed

1 file changed

+29
-36
lines changed

dev-reactime/linkFiber.js

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@ const componentActionsRecord = require('./masterState');
4949

5050
module.exports = (snap, mode) => {
5151
let fiberRoot = null;
52-
let astHooks;
53-
let concurrent = false; // flag to check if we are in concurrent mode
54-
const reactWorkTags = [
55-
'FunctionComponent',
56-
'ClassComponent',
57-
'IndeterminateComponent',
58-
'HostRoot', // Root of a host tree. Could be nested inside another node.
59-
'HostPortal', // A subtree. Could be an entry point to a different renderer.
60-
'HostComponent',
61-
'HostText',
62-
];
63-
6452

6553
async function sendSnapshot() {
6654
// Don't send messages while jumping or while paused
@@ -81,14 +69,14 @@ module.exports = (snap, mode) => {
8169
// Carlos: Injects instrumentation to update our state tree every time
8270
// a hooks component changes state
8371
function traverseHooks(memoizedState) {
84-
const hooksComponents = [];
72+
const hooksStates = [];
8573
while (memoizedState && memoizedState.queue) {
8674
// Carlos: these two are legacy comments, we should look into them later
8775
// prevents useEffect from crashing on load
8876
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
8977
if (memoizedState.memoizedState) {
9078
console.log('memoizedState in traverseHooks is:', memoizedState);
91-
hooksComponents.push({
79+
hooksStates.push({
9280
component: memoizedState.queue,
9381
state: memoizedState.memoizedState,
9482
});
@@ -97,7 +85,7 @@ module.exports = (snap, mode) => {
9785
memoizedState = memoizedState.next !== memoizedState
9886
? memoizedState.next : null;
9987
}
100-
return hooksComponents;
88+
return hooksStates;
10189
}
10290

10391
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
@@ -107,7 +95,6 @@ module.exports = (snap, mode) => {
10795
if (!currentFiber) return null;
10896
if (!tree) return tree;
10997

110-
11198
// These have the newest state. We update state and then
11299
// called updateSnapshotTree()
113100
const {
@@ -126,39 +113,39 @@ module.exports = (snap, mode) => {
126113
let newState = null;
127114
let componentData = {};
128115
let componentFound = false;
129-
// Check if node is a stateful component
116+
117+
// Check if node is a stateful setState component
130118
if (stateNode && stateNode.state && (tag === 0 || tag === 1)) {
131119
console.log('in create tree if')
132120
console.log('this is currentFiber from createTree', currentFiber)
133121
// Save component's state and setState() function to our record for future
134122
// time-travel state changing. Add record index to snapshot so we can retrieve.
135123
componentData.index = componentActionsRecord.saveNew(stateNode.state, stateNode);
136-
newState = stateNode.state;
124+
newState.state = stateNode.state;
137125
componentFound = true;
138-
// tree.appendToChild(stateNode.state, elementType.name, index); // Add component to tree
139-
} else if (tag === 0 || tag === 1) {
140-
// grab stateless components here
141-
newState = 'stateless';
142-
// tree.appendChild({}, elementType.name) // Add component to tree
143126
}
144127

145-
// Check if node is a hooks function
128+
// Check if node is a hooks useState function
146129
let hooksIndex;
147130
if (memoizedState && (tag === 0 || tag === 1 || tag === 10)) {
148131
console.log('in create tree if')
149132
console.log('this is currentFiber from createTree', currentFiber)
150133
if (memoizedState.queue) {
151-
const hooksComponents = traverseHooks(memoizedState);
152-
hooksComponents.forEach(c => {
153-
hooksIndex = componentActionsRecord.saveNew(c.state, c.component);
154-
if (newState.hooksState) {
155-
newState.hooksState.push([c.state, hooksIndex]);
134+
// Hooks states are stored as a linked list using memoizedState.next,
135+
// so we must traverse through the list and get the states.
136+
// We then store them along with the corresponding memoizedState.queue,
137+
// which includes the dispatch() function we use to change their state.
138+
const hooksStates = traverseHooks(memoizedState);
139+
hooksStates.forEach(state => {
140+
hooksIndex = componentActionsRecord.saveNew(state.state, state.component);
141+
if (newState && newState.hooksState) {
142+
newState.hooksState.push([state.state, hooksIndex]);
143+
} else if (newState) {
144+
newState.hooksState = [[state.state, hooksIndex]];
156145
} else {
157-
newState.hooksState = [[c.state, hooksIndex]];
146+
newState = { hooksState: [[state.state, hooksIndex]] };
158147
}
159148
componentFound = true;
160-
// newState = { ...newState, hooksState: c.state };
161-
// tree.appendSibling(c.state, elementType.name ? elementType.name : 'nameless', index);
162149
});
163150
}
164151
} else {
@@ -175,6 +162,12 @@ module.exports = (snap, mode) => {
175162
}
176163
}
177164

165+
// This grabs stateless components
166+
if (!componentFound && (tag === 0 || tag === 1)) {
167+
newState = 'stateless';
168+
}
169+
170+
// Adds performance metrics to the component data
178171
componentData = {
179172
...componentData,
180173
actualDuration,
@@ -191,6 +184,9 @@ module.exports = (snap, mode) => {
191184

192185
// Recurse on children
193186
if (child) {
187+
// If this node had state we appended to the children array,
188+
// so attach children to the newly appended child.
189+
// Otherwise, attach children to this same node.
194190
if (tree.children.length > 0) {
195191
createTree(child, tree.children[tree.children.length - 1]);
196192
} else {
@@ -237,6 +233,7 @@ module.exports = (snap, mode) => {
237233

238234
// ! BUG: skips 1st hook click
239235
function updateSnapShotTree() {
236+
<<<<<<< HEAD
240237
/* let current;
241238
// If concurrent mode, grab current.child
242239
if (concurrent) {
@@ -260,21 +257,17 @@ module.exports = (snap, mode) => {
260257
// Point fiberRoot to FiberRootNode
261258
if (container._internalRoot) {
262259
fiberRoot = container._internalRoot;
263-
concurrent = true;
264260
} else {
265261
const {
266262
_reactRootContainer: { _internalRoot },
267263
_reactRootContainer,
268264
} = container;
269265
// Only assign internal root if it actually exists
270266
fiberRoot = _internalRoot || _reactRootContainer;
271-
// console.log('_reactRootContainer is:', _reactRootContainer);
272-
// console.log('linkFiber.js, fiberRoot:', fiberRoot);
273267
}
274268
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
275269
console.log('this is devTools', devTools)
276270
const reactInstance = devTools ? devTools.renderers.get(1) : null;
277-
const overrideHookState = reactInstance ? reactInstance.overrideHookState : null;
278271

279272
if (reactInstance && reactInstance.version) {
280273
devTools.onCommitFiberRoot = (function (original) {

0 commit comments

Comments
 (0)