Skip to content

Commit a800648

Browse files
committed
conflicts solved
2 parents eed276c + c3ee9de commit a800648

File tree

12 files changed

+251
-179
lines changed

12 files changed

+251
-179
lines changed

dev-reactime/linkFiber.js

Lines changed: 82 additions & 45 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');
@@ -65,7 +76,7 @@ export default (snap, mode) => {
6576

6677
if (!snap.tree) {
6778
// console.log('snapshot empty, sending root');
68-
snap.tree = new Tree('root');
79+
snap.tree = new Tree('root', 'root');
6980
}
7081
const payload = snap.tree.cleanTreeCopy();// snap.tree.getCopy();
7182

@@ -103,9 +114,11 @@ export default (snap, mode) => {
103114

104115
// Carlos: This runs after EVERY Fiber commit. It creates a new snapshot,
105116
//
106-
function createTree(currentFiber, tree = new Tree('root'), fromSibling = false) {
117+
118+
let ctRunning = 0;
119+
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,83 +193,106 @@ 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-
// if (container._internalRoot) {
227-
// fiberRoot = container._internalRoot;
228-
// } else {
229-
// const {
230-
// _reactRootContainer: { _internalRoot },
231-
// _reactRootContainer,
232-
// } = container;
233-
// // Only assign internal root if it actually exists
234-
// fiberRoot = _internalRoot || _reactRootContainer;
235-
// }
236-
255+
return async () => {
256+
/* const container = document.getElementById('root');
257+
if (container._internalRoot) {
258+
fiberRoot = container._internalRoot;
259+
} else {
260+
const {
261+
_reactRootContainer: { _internalRoot },
262+
_reactRootContainer,
263+
} = container;
264+
// Only assign internal root if it actually exists
265+
fiberRoot = _internalRoot || _reactRootContainer;
266+
}
267+
*/
237268
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
238269
const reactInstance = devTools ? devTools.renderers.get(1) : null;
239-
// console.log('devTools:', devTools);
240-
270+
fiberRoot = devTools.getFiberRoots(1).values().next().value;
271+
272+
alwaysLog('fiberRoot:', fiberRoot);
241273
if (reactInstance && reactInstance.version) {
242274
devTools.onCommitFiberRoot = (function (original) {
243275
return function (...args) {
244276
fiberRoot = args[1];
277+
//console.log('Fiber committed, updating snapshot tree with:', fiberRoot);
245278
updateSnapShotTree();
279+
console.log('Fiber committed, sending latest snapshot');
246280
sendSnapshot();
281+
console.log('Fiber committed, latest snapshot sent');
247282
return original(...args);
248283
};
249284
}(devTools.onCommitFiberRoot));
250285
}
251286
updateSnapShotTree();
287+
sendSnapshot();
288+
// updateSnapShotTree();
252289
// Send the initial snapshot once the content script has started up
253290
// This message is sent from contentScript.js in chrome extension bundles
254-
window.addEventListener('message', ({ data: { action } }) => {
255-
if (action === 'contentScriptStarted') {
256-
// console.log('content script started received at linkFiber.js')
257-
sendSnapshot();
258-
}
259-
});
291+
// window.addEventListener('message', ({ data: { action } }) => {
292+
// if (action === 'contentScriptStarted') {
293+
// // console.log('content script started received at linkFiber.js')
294+
// sendSnapshot();
295+
// }
296+
// });
260297
};
261298
};

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/components/ErrorHandler.jsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React from 'react';
2+
3+
class ErrorHandler extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = { errorOccurred: false };
7+
}
8+
9+
componentDidCatch(error, info) {
10+
this.setState({ errorOccurred: true })
11+
console.log('Error occurred in React Component: ', error, info);
12+
}
13+
14+
render() {
15+
return this.state.errorOccurred ? <h1>An error occurred</h1> : this.props.children
16+
}
17+
}
18+
19+
export default ErrorHandler;

0 commit comments

Comments
 (0)