Skip to content

Commit c3ee9de

Browse files
authored
Merge pull request #45 from oslabs-beta/staging
Staging
2 parents 4bd126a + 698ac19 commit c3ee9de

File tree

6 files changed

+110
-43
lines changed

6 files changed

+110
-43
lines changed

dev-reactime/linkFiber.js

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,29 @@
4545
/* eslint-disable no-param-reassign */
4646

4747
// const Tree = require('./tree').default;
48-
// const componentActionsRecord = require('./masterState');\
49-
48+
// const componentActionsRecord = require('./masterState');
5049
import Tree from './tree';
5150
import 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) => {
5666
export 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));

dev-reactime/tree.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
/* eslint-disable no-param-reassign */
55
// import * as reactWorkTags from './reactWorkTags';
66

7+
const Flatted = require('flatted');
8+
let copyInstances = 0;
9+
const circularComponentTable = new Set();
10+
11+
712
// removes unserializable state data such as functions
813
function scrubUnserializableMembers(tree) {
914
Object.entries(tree.state).forEach(keyValuePair => {
@@ -15,15 +20,15 @@ function scrubUnserializableMembers(tree) {
1520
// this is the current snapshot that is being sent to the snapshots array.
1621
class Tree {
1722
constructor(state, name = 'nameless', componentData = {}) {
18-
this.state = state === 'root' ? 'root' : JSON.parse(JSON.stringify(state));
23+
this.state = state === 'root' ? 'root' : Flatted.parse(Flatted.stringify(state));
1924
this.name = name;
2025
this.componentData = componentData ? JSON.parse(JSON.stringify(componentData)) : {};
2126
this.children = [];
2227
this.parent = null; // ref to parent so we can add siblings
2328
}
2429

2530
addChild(state, name, componentData) {
26-
// console.log('tree.js: in addChild');
31+
//console.log('tree.js: in addChild');
2732
const newChild = new Tree(state, name, componentData);
2833
newChild.parent = this;
2934
this.children.push(newChild);
@@ -39,15 +44,28 @@ class Tree {
3944
}
4045

4146
cleanTreeCopy() {
47+
if (copyInstances === 0) {
48+
copyInstances++;
49+
circularComponentTable.clear();
50+
}
4251
// creates copy of present node
4352
let copy = new Tree(this.state, this.name, this.componentData);
53+
circularComponentTable.add(this);
4454
copy = scrubUnserializableMembers(copy);
45-
copy.children = this.children;
55+
56+
// copy.children = this.children;
4657

4758
// creates copy of each child of the present node
48-
copy.children = this.children.map(child => child.cleanTreeCopy());
59+
copy.children = this.children.map(child => {
60+
if (!circularComponentTable.has(child)) {
61+
return child.cleanTreeCopy()
62+
} else {
63+
return 'circular';
64+
}
65+
});
4966

5067
// returns copy
68+
copyInstances--;
5169
return copy;
5270
}
5371

flatted_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Flatted = require('flatted');
2+
3+
4+
let a = {'a': 'b', c: 3};
5+
6+
console.log(a);
7+
console.log(Flatted.stringify(a));
8+
9+
console.log(a);

package-lock.json

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"bower": "^1.8.8",
7373
"d3": "^5.16.0",
7474
"d3-zoom": "^1.8.3",
75+
"flatted": "^3.0.2",
7576
"immer": "^3.2.0",
7677
"jest-runner": "^24.9.0",
7778
"jsondiffpatch": "^0.3.11",

src/app/containers/ActionContainer.jsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@ function ActionContainer() {
3131
componentName: obj.stateSnapshot.children[0].name,
3232
componentData: JSON.stringify(obj.stateSnapshot.children[0].componentData) === '{}' ? '' : obj.stateSnapshot.children[0].componentData
3333
};
34-
3534
hierarchyArr.push(newObj);
36-
if (obj.children) {
37-
obj.children.forEach(element => {
38-
displayArray(element);
39-
});
40-
}
35+
}
36+
if (obj.children) {
37+
obj.children.forEach(element => {
38+
displayArray(element);
39+
});
4140
}
4241
};
4342
// gabi :: the hierarchy get set on the first click in the page, when page in refreshed we don't have a hierarchy so we need to check if hierarchy was inicialize involk displayArray to display the hierarchy

0 commit comments

Comments
 (0)