Skip to content

Commit e80de1f

Browse files
authored
Merge pull request #32 from oslabs-beta/staging
Staging
2 parents 7a59cf2 + 4dd0738 commit e80de1f

File tree

18 files changed

+1159
-426
lines changed

18 files changed

+1159
-426
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"eslint.enable": true
3+
}

dev-reactime/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import 'regenerator-runtime/runtime';
77
import { exist } from 'acorn-jsx/xhtml';
88

99
// * State snapshot object initialized here
10-
const snapShot = { tree: null };
10+
const snapShot = {
11+
tree: null,
12+
unfilteredTree: null
13+
};
1114

1215

1316
const mode = {

dev-reactime/linkFiber.js

Lines changed: 138 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/* eslint-disable indent */
2+
/* eslint-disable brace-style */
3+
/* eslint-disable comma-dangle */
14
/**
25
* This file contains core module functionality.
36
*
@@ -113,16 +116,20 @@ module.exports = (snap, mode) => {
113116

114117
// Check if node is a stateful setState component
115118
if (stateNode && stateNode.state && (tag === 0 || tag === 1)) {
119+
console.log('in create tree if')
120+
console.log('this is currentFiber from createTree', currentFiber)
116121
// Save component's state and setState() function to our record for future
117122
// time-travel state changing. Add record index to snapshot so we can retrieve.
118123
componentData.index = componentActionsRecord.saveNew(stateNode.state, stateNode);
119-
newState.state = stateNode.state;
124+
newState = stateNode.state;
120125
componentFound = true;
121126
}
122127

123128
// Check if node is a hooks useState function
124129
let hooksIndex;
125130
if (memoizedState && (tag === 0 || tag === 1 || tag === 10)) {
131+
console.log('in create tree if')
132+
console.log('this is currentFiber from createTree', currentFiber)
126133
if (memoizedState.queue) {
127134
// Hooks states are stored as a linked list using memoizedState.next,
128135
// so we must traverse through the list and get the states.
@@ -180,10 +187,57 @@ module.exports = (snap, mode) => {
180187
return tree;
181188
}
182189

190+
191+
// function createUnfilteredTree(curFiber, parentNode) {
192+
// // on call from updateSnapShot, no parentNode provided, so create a root node
193+
// if(! parentNode) parentNode = new Tree('root');
194+
195+
// // Base case: parentNode's child or sibling pointed to null
196+
// if (!curFiber) return parentNode;
197+
198+
// let newChildNode = null;
199+
200+
// // If stateful, add to parentNode's children array, then inject new setState into fiber node
201+
// if (curFiber.stateNode && curFiber.stateNode.state) {
202+
// newChildNode = parentNode.appendChild(curFiber.stateNode);
203+
// // changeSetState(curFiber.stateNode);
204+
// newChildNode.isStateful = true;
205+
// }
206+
// else {
207+
208+
// }
209+
210+
// // Recurse to sibling; siblings that have state should be added to our parentNode
211+
// createTree(curFiber.sibling, parentNode);
212+
213+
// // Recurse to child; If this fiber was stateful, then we added a newChildNode here, and we want
214+
// // to attach further children to that. If this fiber wasn't stateful, we want to attach any
215+
// // children to our existing parentNode.
216+
// createTree(curFiber.child, newChildNode || parentNode);
217+
218+
// return parentNode;
219+
// }
220+
221+
183222
// ! BUG: skips 1st hook click
184223
function updateSnapShotTree() {
185-
const { current } = fiberRoot;
186-
snap.tree = createTree(current);
224+
/* let current;
225+
// If concurrent mode, grab current.child
226+
if (concurrent) {
227+
// we need a way to wait for current child to populate
228+
const promise = new Promise((resolve, reject) => {
229+
setTimeout(() => resolve(fiberRoot.current.child), 400);
230+
});
231+
current = await promise;
232+
current = fiberRoot.current.child;
233+
} else {
234+
current = fiberRoot.current;
235+
} */
236+
const { current } = fiberRoot; // Carlos: get rid of concurrent mode for now
237+
238+
// console.log('FIBER COMMITTED, new fiber is:', util.inspect(current, false, 4));
239+
// fs.appendFile('fiberlog.txt', util.inspect(current, false, 10));
240+
snap.tree = createTree(current); // Carlos: pass new hooks state here?
187241
}
188242

189243
return async container => {
@@ -199,12 +253,14 @@ module.exports = (snap, mode) => {
199253
fiberRoot = _internalRoot || _reactRootContainer;
200254
}
201255
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
256+
console.log('this is devTools', devTools)
202257
const reactInstance = devTools ? devTools.renderers.get(1) : null;
203258

204259
if (reactInstance && reactInstance.version) {
205260
devTools.onCommitFiberRoot = (function (original) {
206261
return function (...args) {
207262
fiberRoot = args[1];
263+
console.log('this is fiberRoot', fiberRoot)
208264
updateSnapShotTree();
209265
sendSnapshot();
210266
return original(...args);
@@ -221,3 +277,82 @@ module.exports = (snap, mode) => {
221277
});
222278
};
223279
};
280+
281+
282+
283+
284+
// function createUnfilteredTree(currentFiber, tree = new Tree('root')) {
285+
// // Base case: child or sibling pointed to null
286+
// if (!currentFiber) return tree;
287+
288+
// const { sibling, stateNode, child, memoizedState, elementType,
289+
// tag, actualDuration, actualStartTime, selfBaseDuration, treeBaseDuration,
290+
// } = currentFiber;
291+
292+
// const extraProps = {
293+
// tag, actualDuration, actualStartTime, selfBaseDuration, treeBaseDuration,
294+
// };
295+
296+
// let nextTree = tree;
297+
// let nextTreeUnfiltered = unfilteredTreeNode = new UnfilteredTreeNode('root');
298+
299+
// // Check if stateful component
300+
// if (stateNode && stateNode.state) {
301+
// nextTree = tree.appendChild(stateNode); // Add component to tree
302+
// changeSetState(stateNode); // Change setState functionality
303+
// }
304+
// nextTreeUnfiltered = unfilteredTreeNode.appendChild(stateNode);
305+
306+
// // TODO: handle Hooks cases...
307+
308+
// // Recurse on siblings
309+
// createTree(sibling, tree);
310+
// // Recurse on children
311+
// createTree(child, nextTree);
312+
313+
// return tree;
314+
// }
315+
316+
317+
318+
// Check if the component uses hooks
319+
// if (memoizedState && Object.hasOwnProperty.call(memoizedState, 'baseState')) {
320+
// // 'catch-all' for suspense elements (experimental)
321+
// if (typeof elementType.$$typeof === 'symbol') return;
322+
// // Traverse through the currentFiber and extract the getters/setters
323+
// astHooks = astParser(elementType);
324+
// saveState(astHooks);
325+
// // Create a traversed property and assign to the evaluated result of
326+
// // invoking traverseHooks with memoizedState
327+
// memoizedState.traversed = traverseHooks(memoizedState);
328+
// nextTree = tree.appendChild(memoizedState);
329+
// }
330+
331+
332+
333+
334+
335+
336+
337+
338+
// function createTree(currentFiber, tree = new Tree('root')) {
339+
// // Base case: child or sibling pointed to null
340+
// if (!currentFiber) return tree;
341+
342+
// const { sibling, stateNode, child, memoizedState, elementType } = currentFiber;
343+
344+
// let nextTree = tree;
345+
346+
// // Check if stateful component
347+
// if (stateNode && stateNode.state) {
348+
// nextTree = tree.appendChild(stateNode); // Add component to tree
349+
// changeSetState(stateNode); // Change setState functionality
350+
// }
351+
352+
// // Recurse on siblings
353+
// createTree(sibling, tree);
354+
// // Recurse on children
355+
// createTree(child, nextTree);
356+
357+
// return tree;
358+
// }

dev-reactime/reactWorkTags.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// eslint-disable-next-line import/prefer-default-export
2+
export const reactWorkTags = [
3+
{ 0: 'FunctionComponent' },
4+
{ 1: 'ClassComponent' },
5+
{ 2: 'IndeterminateComponent' },
6+
{ 3: 'HostRoot' }, // Root of a host tree. Could be nested inside another node.
7+
{ 4: 'HostPortal' }, // A subtree. Could be an entry point to a different renderer.
8+
{ 5: 'HostComponent' },
9+
{ 6: 'HostText' },
10+
];

dev-reactime/tree.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
/* eslint-disable no-multiple-empty-lines */
2+
/* eslint-disable max-classes-per-file */
13
/* eslint-disable no-console */
24
/* eslint-disable no-param-reassign */
5+
// import * as reactWorkTags from './reactWorkTags';
36

47
// removes unserializable state data such as functions
58
function scrubUnserializableMembers(tree) {
@@ -129,4 +132,52 @@ class Tree {
129132
}
130133
}
131134

132-
module.exports = Tree;
135+
// class UnfilteredTreeNode extends Tree {
136+
// constructor(component, useStateInstead = false, name, unfilteredProps) {
137+
// super(component, useStateInstead, name);
138+
// // this.isStateful = unfilteredProps.isStateful;
139+
// // this.tagLabel = reactWorkTags[unfilteredProps.tag];
140+
// if(unfilteredProps) {
141+
// this.tag = unfilteredProps.tag;
142+
// this.actualDuration = unfilteredProps.actualDuration;
143+
// this.actualStartTime = unfilteredProps.actualStartTime;
144+
// this.selfBaseDuration = unfilteredProps.selfBaseDuration;
145+
// this.treeBaseDuration = unfilteredProps.treeBaseDuration;
146+
// }
147+
// }
148+
149+
// appendChild(component) {
150+
// const newChild = new UnfilteredTreeNode(component);
151+
// this.children.push(newChild);
152+
// return newChild;
153+
// }
154+
// }
155+
156+
module.exports = Tree
157+
// module.exports = {
158+
// Tree,
159+
// UnfilteredTreeNode,
160+
// };
161+
162+
163+
164+
165+
// // print out the tree structure in the console
166+
// // DEV: Process may be different for useState components
167+
// // BUG FIX: Don't print the Router as a component
168+
// // Change how the children are printed
169+
// print() {
170+
// console.log("current tree structure for *this : ", this);
171+
// const children = ['children: '];
172+
// // DEV: What should we push instead for components using hooks (it wouldn't be state)
173+
// this.children.forEach(child => { // if this.children is always initialized to empty array, when would there ever be anything to iterate through here?
174+
// children.push(child.state || child.component.state);
175+
// });
176+
// if (this.name) console.log("this.name if exists: ", this.name);
177+
// if (children.length === 1) {
178+
// console.log(`children length 1. ${this.state ? `this.state: ` : `this.component.state: `}`, this.state || this.component.state);
179+
// } else console.log(`children length !== 1. ${this.state ? `this.state: ` : `this.component.state, children: `}`, this.state || this.component.state, ...children);
180+
// this.children.forEach(child => {
181+
// child.print();
182+
// });
183+
// }

0 commit comments

Comments
 (0)