Skip to content

Commit 5018666

Browse files
authored
Merge pull request #5 from kevinfey/map
Adding Visualization to Map Current Component State
2 parents eebc62f + a917c6b commit 5018666

File tree

6 files changed

+68
-34
lines changed

6 files changed

+68
-34
lines changed

package-lock.json

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

src/app/components/Map.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ const Map = (props) => {
1212
console.log('MAP SNAPSHOT', snapshot);
1313

1414
// set the heights and width of the tree to be passed into treeMap function
15-
const width: number = 600;
16-
const height: number = 1100;
15+
const width: number = 1100;
16+
const height: number = 600;
1717

1818
// this state allows the canvas to stay at the zoom level on multiple re-renders
1919
const [{ x, y, k }, setZoomState]: any = useState({ x: 0, y: 0, k: 0 });
2020

2121
useEffect(() => {
2222
setZoomState(d3.zoomTransform(d3.select('#canvas').node()));
23-
}, [snapshot]);
23+
}, [snapshot.children]);
2424

2525
// this only clears the canvas if Visualizer is already rendered on the extension
2626
useEffect(() => {
@@ -31,9 +31,9 @@ const Map = (props) => {
3131
.select('#canvas')
3232
.attr('width', width)
3333
.attr('height', height);
34-
3534
// creating a pseudo-class for reusability
3635
const g: any = svgContainer
36+
3737
.append('g')
3838
.attr('transform', `translate(${x}, ${y}), scale(${k})`); // sets the canvas to the saved zoomState
3939

@@ -46,13 +46,12 @@ const Map = (props) => {
4646
// childrenArr.push(el)
4747
// );
4848
// }
49-
49+
5050
// console.log('CHILDREN', childrenArr);
5151

5252
const appState: any = {
5353
name: ' Root',
5454
// pass in parsed data here
55-
// call the helper function passing in the most recent snapshot
5655
children: snapshot.children,
5756
};
5857

@@ -96,6 +95,7 @@ const Map = (props) => {
9695
// returns a flat array of objects containing all the nodes and their information
9796
// renders nodes onto the canvas
9897
let nodes: any = hierarchyNodes.descendants();
98+
console.log("NODES",nodes)
9999

100100
// const node is used to create all the nodes
101101
// this segment places all the nodes on the canvas
@@ -111,13 +111,16 @@ const Map = (props) => {
111111
.attr('class', 'atomNodes');
112112

113113
// for each node that got created, append a circle element
114-
node.append('circle').attr('fill', '#c300ff').attr('r', 50);
114+
node
115+
.append('circle')
116+
.attr('fill', (d: any) => (d.children ? '#3214db' : '#c300ff'))
117+
.attr('r', 50);
115118

116119
// for each node that got created, append a text element that displays the name of the node
117120
node
118121
.append('text')
119122
.attr('dy', '.31em')
120-
.attr('x', (d: any) => (d.children ? -75 : 75))
123+
.attr('x', (d: any) => (d.children ? -50 : 50))
121124
.attr('text-anchor', (d: any) => (d.children ? 'end' : 'start'))
122125
.text((d: any) => d.data.name)
123126
.style('font-size', `2rem`)
@@ -136,9 +139,10 @@ const Map = (props) => {
136139
.append('text')
137140
.text(JSON.stringify(d.data, undefined, 2))
138141
.style('fill', 'white')
139-
.attr('x', 75)
140-
.attr('y', 60)
142+
.attr('x', -999)
143+
.attr('y', 100)
141144
.style('font-size', '3rem')
145+
.style('text-align', 'center')
142146
.attr('stroke', '#646464')
143147
.attr('id', `popup${i}`);
144148
}
@@ -166,7 +170,7 @@ const Map = (props) => {
166170
[0, 0],
167171
[width, height],
168172
])
169-
.scaleExtent([0, 8])
173+
.scaleExtent([0, 5])
170174
.on('zoom', zoomed)
171175
);
172176

@@ -192,7 +196,6 @@ const Map = (props) => {
192196
}
193197
});
194198

195-
console.log('208');
196199
return (
197200
<div data-testid="canvas">
198201
<div className="Visualizer">

src/app/components/PerfView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const PerfView = (props: PerfViewProps) => {
4040
// snapshots.forEach((snapshot) => snapshot.children[0].children.shift());
4141
// console.log('SNAPSHOTS -------------------------->', snapshots);
4242
// Figure out which snapshot index to use
43+
4344
let indexToDisplay: number | null = null;
4445
if (viewIndex < 0) indexToDisplay = snapshots.length - 1;
4546
else indexToDisplay = viewIndex;

src/backend/linkFiber.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
9494
component: memoizedState.queue,
9595
state: memoizedState.memoizedState,
9696
});
97+
9798
}
9899
memoizedState =
99100
memoizedState.next !== memoizedState ? memoizedState.next : null;
@@ -166,6 +167,7 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
166167
// });
167168
atomArray.push(memoizedProps);
168169

170+
169171
// console.log('1st ATOM ARRAY', atomArray);
170172

171173
function traverseRecoilHooks(memoizedState: any): HookStates {
@@ -182,6 +184,7 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
182184
state: memoizedProps,
183185
});
184186
}
187+
185188
}
186189
memoizedState =
187190
memoizedState.next !== memoizedState ? memoizedState.next : null;
@@ -202,20 +205,32 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
202205
// which includes the dispatch() function we use to change their state.
203206

204207
const hooksStates = traverseRecoilHooks(memoizedState);
208+
205209
const hooksNames = getHooksNames(elementType.toString());
206210
hooksStates.forEach((state, i) => {
211+
207212
hooksIndex = componentActionsRecord.saveNew(
208213
state.state,
209214
state.component
210215
);
211216
componentData.hooksIndex = hooksIndex;
212217

218+
// if (newState && newState.hooksState) {
219+
// newState.hooksState.push({ [hooksNames[i]]: state.state });
220+
// } else if (newState) {
221+
// newState.hooksState = [{ [hooksNames[i]]: state.state }];
222+
// } else {
223+
// newState = { hooksState: [] };
224+
// newState.hooksState.push({ [hooksNames[i]]: state.state });
225+
// }
226+
227+
//improves tree visualization but breaks jump
213228
if (newState && newState.hooksState) {
214229
newState.push(state.state);
215230
} else if (newState) {
216-
newState = [ state.state ];
231+
newState = [state.state];
217232
} else {
218-
newState.push(state.state );
233+
newState.push(state.state);
219234
}
220235
componentFound = true;
221236
});
@@ -224,8 +239,13 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
224239

225240
// Check if node is a hooks useState function
226241
//REGULAR REACT HOOKS
227-
if (memoizedState && (tag === 0 || tag === 1 || tag === 2 || tag === 10) && isRecoil === false) {
242+
if (
243+
memoizedState &&
244+
(tag === 0 || tag === 1 || tag === 2 || tag === 10) &&
245+
isRecoil === false
246+
) {
228247
if (memoizedState.queue) {
248+
229249
// Hooks states are stored as a linked list using memoizedState.next,
230250
// so we must traverse through the list and get the states.
231251
// We then store them along with the corresponding memoizedState.queue,
@@ -309,6 +329,7 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
309329
snap.tree = createTree(current);
310330
}
311331

332+
312333
sendSnapshot();
313334
}
314335

src/backend/masterState.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ import 'core-js';
66

77
const componentActionsRecord = {};
88
let index = 0;
9-
109
// module.exports = {
1110
export default {
1211
saveNew: (state, component) => {
12+
//console.log("COMPONENET ACTION RECORDS",componentActionsRecord)
13+
1314
componentActionsRecord[index] = { state, component };
1415
index++;
1516
return index - 1;

src/backend/timeJump.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,44 @@ export default (origin, mode) => {
3333
if (!target) return;
3434

3535
if (target.state === 'stateless') {
36-
target.children.forEach(child => jump(child));
36+
target.children.forEach((child) => jump(child));
3737
return;
3838
}
39-
const component = componentActionsRecord.getComponentByIndex(target.componentData.index);
39+
const component = componentActionsRecord.getComponentByIndex(
40+
target.componentData.index
41+
);
4042
if (component && component.setState) {
41-
component.setState(prevState => {
42-
Object.keys(prevState).forEach(key => {
43-
if (target.state[key] === undefined) {
44-
target.state[key] = undefined;
45-
}
46-
});
47-
return target.state;
48-
// Iterate through new children after state has been set
49-
}, () => target.children.forEach(child => jump(child)));
43+
component.setState(
44+
(prevState) => {
45+
Object.keys(prevState).forEach((key) => {
46+
if (target.state[key] === undefined) {
47+
target.state[key] = undefined;
48+
}
49+
});
50+
return target.state;
51+
// Iterate through new children after state has been set
52+
},
53+
() => target.children.forEach((child) => jump(child))
54+
);
5055
}
5156

5257
console.log('TARGET', target);
5358
// Check for hooks state and set it with dispatch()
5459
if (target.state && target.state.hooksState) {
55-
target.state.hooksState.forEach(hook => {
56-
const hooksComponent = componentActionsRecord.getComponentByIndex(target.componentData.hooksIndex);
60+
target.state.hooksState.forEach((hook) => {
61+
const hooksComponent = componentActionsRecord.getComponentByIndex(
62+
target.componentData.hooksIndex
63+
);
5764
const hookState = Object.values(hook);
65+
console.log('target', target);
5866
if (hooksComponent && hooksComponent.dispatch) {
59-
// console.log('attempt to dispatch this', hooksComponent, hookState[0]);
67+
6068
hooksComponent.dispatch(hookState[0]);
6169
}
6270
});
6371
}
6472

65-
target.children.forEach(child => {
73+
target.children.forEach((child) => {
6674
if (!circularComponentTable.has(child)) {
6775
circularComponentTable.add(child);
6876
jump(child);

0 commit comments

Comments
 (0)