Skip to content

Commit cc611ff

Browse files
committed
Add Host component for backEnd Type, refactor React Tree Traverse on LinkFiber & Refactor ComponentMap to reduce filtering. All data sanitization is performed in backend
1 parent d4f2e92 commit cc611ff

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

src/app/components/StateRoute/ComponentMap/ComponentMap.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,20 @@ export default function ComponentMap({
188188
};
189189

190190
const formatProps = (data) => {
191+
console.log('ComponentMap', { data });
191192
const propsFormat = [];
192193
const nestedObj = [];
193194
for (const key in data) {
194195
if (
195-
data[key] !== 'reactFiber' &&
196-
typeof data[key] !== 'object' &&
197-
exclude.includes(key) !== true
196+
// data[key] !== 'reactFiber' &&
197+
typeof data[key] !== 'object'
198+
// exclude.includes(key) !== true
198199
) {
199200
propsFormat.push(<p className='stateprops'>{`${key}: ${data[key]}`}</p>);
200201
} else if (
201-
data[key] !== 'reactFiber' &&
202-
typeof data[key] === 'object' &&
203-
exclude.includes(key) !== true
202+
// data[key] !== 'reactFiber' &&
203+
typeof data[key] === 'object'
204+
// exclude.includes(key) !== true
204205
) {
205206
const result = formatProps(data[key]);
206207
nestedObj.push(result);

src/backend/linkFiber.ts

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ const exclude = new Set([
149149
'dependencies',
150150
'destroy',
151151
'effects',
152+
'element',
152153
'elementType',
153154
'firstBaseUpdate',
154155
'firstEffect',
@@ -158,18 +159,22 @@ const exclude = new Set([
158159
'lanes',
159160
'lastBaseUpdate',
160161
'lastEffect',
162+
'navigator',
161163
'memoizedState',
162164
'mode',
163165
'next',
164166
'nextEffect',
165167
'pending',
166168
'parentSub',
169+
'pathnameBase',
167170
'pendingProps',
168171
'Provider',
169172
'updateQueue',
170173
'ref',
171174
'responders',
172175
'return',
176+
'route',
177+
'routeContext',
173178
'shared',
174179
'sibling',
175180
'stateNode',
@@ -192,20 +197,26 @@ const exclude = new Set([
192197
// This recursive function is used to grab the state of children components
193198
// and push them into the parent componenent
194199
// react elements throw errors on client side of application - convert react/functions into string
195-
function convertDataToString(newObj, oldObj, depth = 0) {
196-
const newPropData = oldObj || {};
200+
function convertDataToString(newObj, newPropData = {}, depth = 0) {
201+
// const newPropData = oldObj;
197202
for (const key in newObj) {
198-
if (typeof newObj[key] === 'function') {
203+
// Skip keys that are in exclude list OR if there is no value at key
204+
if (exclude.has(key) || !newObj[key]) {
205+
continue;
206+
// newPropData[key] = 'reactFiber';
207+
// return newPropData;
208+
}
209+
// If value at key is a function, assign key with value 'function' to newPropData object
210+
else if (typeof newObj[key] === 'function') {
199211
newPropData[key] = 'function';
200-
} else if (exclude.has(key)) {
201-
newPropData[key] = 'reactFiber';
202-
return newPropData;
203-
} else if (typeof newObj[key] === 'object' && !exclude.has(key)) {
204-
newPropData[key] =
205-
depth > 10
206-
? 'convertDataToString reached max depth'
207-
: convertDataToString(newObj[key], null, depth + 1);
208-
} else if (!exclude.has(key)) {
212+
}
213+
// If value at key is an object, recusive call convertDataToString to traverse through all keys and append to newPropData object accodingly
214+
else if (typeof newObj[key] === 'object') {
215+
// newPropData[key] =
216+
depth > 10
217+
? 'convertDataToString reached max depth'
218+
: convertDataToString(newObj[key], newPropData, depth + 1);
219+
} else {
209220
newPropData[key] = newObj[key];
210221
}
211222
}
@@ -241,15 +252,25 @@ function createTree(
241252
dependencies,
242253
_debugHookTypes,
243254
} = currentFiber;
255+
console.log('LinkFiber', {
256+
tag,
257+
elementType:
258+
elementType?._context?.displayName ||
259+
elementType?.render?.name ||
260+
elementType?.name ||
261+
elementType,
262+
memoizedProps,
263+
memoizedState,
264+
});
244265

245-
// Tag === 5 signify this is a React Fragment. Each JSX component return a React fragment
266+
// Tag === 5 signify this is a React Fragment. Each JSX component return a React fragment => The parent of a React Fragment could be a JSX component
246267
if (tag === 5) {
247268
try {
248269
// Ensure parent component has memoizedProps property
249270
if (
250271
memoizedProps.children &&
251272
memoizedProps.children[0]?._owner?.memoizedProps !== undefined
252-
) {
273+
) {
253274
// Access the memoizedProps of the parent component
254275
const propsData = memoizedProps.children[0]._owner.memoizedProps;
255276
const newPropData = convertDataToString(
@@ -281,13 +302,13 @@ function createTree(
281302
let componentFound = false;
282303

283304
// check to see if the parent component has any state/props
284-
if (memoizedProps) {
285-
componentData.props = convertDataToString(memoizedProps, null);
305+
if (memoizedProps && Object.keys(memoizedProps).length) {
306+
componentData.props = convertDataToString(memoizedProps, {});
286307
}
287308

288-
// if the component uses the useContext hook, we want to grab the co text object and add it to the componentData object for that fiber
289-
if (tag === 0 && _debugHookTypes) {
290-
componentData.context = convertDataToString(dependencies?.firstContext?.memoizedValue, null);
309+
// if the component uses the useContext hook, we want to grab the context object and add it to the componentData object for that fiber
310+
if (tag === 0 && _debugHookTypes && dependencies?.firstContext?.memoizedValue) {
311+
componentData.context = convertDataToString(dependencies.firstContext.memoizedValue, null);
291312
}
292313
// Check if node is a stateful class component
293314
if (stateNode && stateNode.state && (tag === 0 || tag === 1 || tag === 2)) {
@@ -337,7 +358,7 @@ function createTree(
337358
selfBaseDuration,
338359
treeBaseDuration,
339360
};
340-
361+
console.log('props', componentData.props);
341362
let newNode = null;
342363

343364
// We want to add this fiber node to the snapshot
@@ -392,6 +413,7 @@ function createTree(
392413
circularComponentTable.add(sibling);
393414
createTree(sibling, newNode, true);
394415
}
416+
395417
return tree;
396418
}
397419
/**

src/backend/types/backendTypes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ export const ClassComponent = 1;
115115
export const IndeterminateComponent = 2; // Before we know whether it is function or class
116116
export const HostRoot = 3; // Root of a host tree. Could be nested inside another node.
117117
export const HostPortal = 4; // A subtree. Could be an entry point to a different renderer.
118+
/**
119+
* Host Component: a type of component that represents a native DOM element in the browser environment, such as div, span, input, h1 etc.
120+
*/
118121
export const HostComponent = 5; // has stateNode of html elements
119122
export const HostText = 6;
120123
export const Fragment = 7;
@@ -138,6 +141,7 @@ export const LegacyHiddenComponent = 24;
138141

139142
/**
140143
* @type Fiber - The Fiber data structure that React uses to represent a component tree.
144+
* https://indepth.dev/posts/1008/inside-fiber-in-depth-overview-of-the-new-reconciliation-algorithm-in-react
141145
* @member actualDuration - The time taken to render the current Fiber node and its descendants during the previous render cycle. This value is used to optimize the rendering of components and to provide performance metrics to developers.
142146
* @member actualStartTime - The time at which the rendering of the current Fiber node started during the previous render cycle.
143147
* @member child - Pointer to the first child.

0 commit comments

Comments
 (0)