Skip to content

Commit 19a79b0

Browse files
committed
added checks for circular nodes in createTree and tree.js; overwrote console.log function in linkFiber to use flag
1 parent e77094b commit 19a79b0

File tree

5 files changed

+70
-20
lines changed

5 files changed

+70
-20
lines changed

dev-reactime/linkFiber.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ console.log = (original => {
6060
})(console.log);
6161

6262

63-
const circularComponentTable = new Map();
63+
const circularComponentTable = new Set();
6464

6565
// module.exports = (snap, mode) => {
6666
export default (snap, mode) => {
@@ -114,6 +114,8 @@ export default (snap, mode) => {
114114

115115
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
116116
//
117+
118+
let ctRunning = 0;
117119
function createTree(currentFiber, tree = new Tree('root', 'root'), fromSibling = false) {
118120
// Base case: child or sibling pointed to null
119121
console.log('createTree: creating tree');
@@ -140,7 +142,7 @@ export default (snap, mode) => {
140142
let componentFound = false;
141143

142144
// Check if node is a stateful setState component
143-
if (stateNode && stateNode.state && (tag === 0 || tag === 1)) { // { || tag === 2)) {
145+
if (stateNode && stateNode.state && (tag === 0 || tag === 1 || tag ===2)) { // { || tag === 2)) {
144146
// Save component's state and setState() function to our record for future
145147
// time-travel state changing. Add record index to snapshot so we can retrieve.
146148
console.log('createTree() found setState component');
@@ -151,7 +153,7 @@ export default (snap, mode) => {
151153

152154
// Check if node is a hooks useState function
153155
let hooksIndex;
154-
if (memoizedState && (tag === 0 || tag === 1 || tag === 10)) {
156+
if (memoizedState && (tag === 0 || tag === 1 || tag === 2 || tag === 10)) {
155157
if (memoizedState.queue) {
156158
console.log('createTree() found hooks component');
157159
// Hooks states are stored as a linked list using memoizedState.next,
@@ -174,9 +176,10 @@ export default (snap, mode) => {
174176
}
175177

176178
// This grabs stateless components
177-
if (!componentFound && (tag === 0 || tag === 1)) { // || tag === 2)) {
179+
/*
180+
if (!componentFound && (tag === 0 || tag === 1 || tag === 2)) {
178181
newState = 'stateless';
179-
}
182+
}*/
180183

181184
// Adds performance metrics to the component data
182185
componentData = {
@@ -207,38 +210,49 @@ export default (snap, mode) => {
207210

208211
// Recurse on children
209212

210-
if (child) { // && !circularComponentTable.has(child)) {
213+
if (child && !circularComponentTable.has(child)) {
211214
// If this node had state we appended to the children array,
212215
// so attach children to the newly appended child.
213216
// Otherwise, attach children to this same node.
214217
console.log('going into child');
215-
// circularComponentTable.set(child, true);
218+
circularComponentTable.add(child);
216219
createTree(child, newNode);
217220
}
218221
// Recurse on siblings
219-
if (sibling) { // && !circularComponentTable.has(sibling)) {
222+
if (sibling && !circularComponentTable.has(sibling)) {
220223
console.log('going into sibling');
221-
// circularComponentTable.set(sibling, true);
224+
circularComponentTable.add(sibling);
222225
createTree(sibling, newNode, true);
223226
}
224227

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+
225236
// // console.log('linkFiber.js: processed children and sibling, returning tree');
226237
return tree;
227238
}
228239

240+
let updateSnapshotTreeCount = 0;
229241
function updateSnapShotTree() {
230242
// console.log('updateSnapshotTree(), checking if fiberRoot updated');
243+
244+
updateSnapshotTreeCount++;
245+
if (updateSnapshotTreeCount > 1) alwaysLog('MULTIPLE SNAPSHOT TREE UPDATES:', updateSnapshotTreeCount);
231246
if (fiberRoot) {
232247
console.log('updateSnapshotTree(), updating snapshot', snap.tree);
233248
const { current } = fiberRoot;
234249
snap.tree = createTree(current);
235250
console.log('updateSnapshotTree(), completed snapshot', snap.tree);
236251
}
252+
updateSnapshotTreeCount--;
237253
}
238254

239-
return async () => {
240-
241-
255+
return async () => {
242256
/* const container = document.getElementById('root');
243257
if (container._internalRoot) {
244258
fiberRoot = container._internalRoot;
@@ -254,7 +268,8 @@ export default (snap, mode) => {
254268
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
255269
const reactInstance = devTools ? devTools.renderers.get(1) : null;
256270
fiberRoot = devTools.getFiberRoots(1).values().next().value;
257-
271+
272+
alwaysLog('fiberRoot:', fiberRoot);
258273
if (reactInstance && reactInstance.version) {
259274
devTools.onCommitFiberRoot = (function (original) {
260275
return function (...args) {

dev-reactime/tree.js

Lines changed: 21 additions & 3 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,7 +20,7 @@ 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 = [];
@@ -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",

0 commit comments

Comments
 (0)