Skip to content

Commit 8ed1dd5

Browse files
committed
Reduce amount of if statements in createTree
1 parent ef91bd4 commit 8ed1dd5

File tree

1 file changed

+59
-66
lines changed

1 file changed

+59
-66
lines changed

src/backend/controllers/createTree/createTree.ts

Lines changed: 59 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ import {
4343
getStateAndContextData,
4444
filterAndFormatData,
4545
} from './statePropExtractors';
46-
import { nextJSDefaultComponent, remixDefaultComponents } from '../../models/filterConditions';
46+
import {
47+
nextJSDefaultComponent,
48+
remixDefaultComponents,
49+
allowedComponentTypes,
50+
} from '../../models/filterConditions';
4751

4852
// -------------------------CREATE TREE TO SEND TO FRONT END--------------------
4953
/**
@@ -76,7 +80,6 @@ export default function createTree(currentFiberNode: Fiber): Tree {
7680
} else {
7781
circularComponentTable.add(currentFiberNode);
7882
}
79-
8083
// ------------------OBTAIN DATA FROM THE CURRENT FIBER NODE----------------
8184
// Destructure the current fiber node:
8285
const {
@@ -120,9 +123,33 @@ export default function createTree(currentFiberNode: Fiber): Tree {
120123
// // dependencies,
121124
// // _debugHookTypes,
122125
// });
126+
// --------------------FILTER COMPONENTS/FIBER NODE-------------------------
127+
/**
128+
* For the snapshot tree,
129+
* 1. We will only interested in components that are one of these types: Function Component, Class Component, Indeterminate Component or Context Provider.
130+
* NOTE: this list of components may change depending on future use
131+
* 2. If user use Next JS, filter out default NextJS components
132+
* 3. If user use Remix JS, filter out default Remix components
133+
*/
123134

135+
if (
136+
!allowedComponentTypes.has(tag) ||
137+
nextJSDefaultComponent.has(componentName) ||
138+
remixDefaultComponents.has(componentName)
139+
) {
140+
// -------------------TRAVERSE TO NEXT FIBERNODE------------------------
141+
// If currentFiberNode has children, recurse on children
142+
if (child) _createTree(child, tree);
143+
144+
// If currentFiberNode has siblings, recurse on siblings
145+
if (sibling) {
146+
_createTree(sibling, tree);
147+
}
148+
// -------------RETURN THE TREE OUTPUT & PASS TO FRONTEND FOR RENDERING-------
149+
return tree;
150+
}
124151

125-
// --------------INITIALIZE OBJECT TO CONTAIN COMPONENT DATA---------------
152+
// --------------INITIALIZE OBJECT TO CONTAIN COMPONENT DATA---------------
126153
let newState: any | { hooksState?: any[] } = {};
127154
let componentData: {
128155
actualDuration?: number;
@@ -146,16 +173,8 @@ export default function createTree(currentFiberNode: Fiber): Tree {
146173
let isStatefulComponent = false;
147174

148175
// ---------------APPEND PROP DATA FROM REACT DEV TOOL----------------------
149-
// Check to see if the parent component has any state/props
150-
if (
151-
!nextJSDefaultComponent.has(componentName) &&
152-
!remixDefaultComponents.has(componentName) &&
153-
(tag === FunctionComponent ||
154-
tag === ClassComponent ||
155-
tag === IndeterminateComponent ||
156-
tag === ContextProvider) &&
157-
memoizedProps
158-
) {
176+
// Check to see if the parent component has any props
177+
if (memoizedProps) {
159178
switch (elementType.name) {
160179
case 'Router':
161180
componentData.props = { pathname: memoizedProps?.location?.pathname };
@@ -169,13 +188,10 @@ export default function createTree(currentFiberNode: Fiber): Tree {
169188
}
170189

171190
// ------------APPEND CONTEXT DATA FROM REACT DEV TOOL----------------
172-
173191
// memoizedState
174192
// Note: if user use ReactHook, memoizedState.memoizedState can be a falsy value such as null, false, ... => need to specify this data is not undefined
175193
if (
176-
!nextJSDefaultComponent.has(componentName) &&
177-
!remixDefaultComponents.has(componentName) &&
178-
(tag === FunctionComponent || tag === ClassComponent) &&
194+
(tag === FunctionComponent || tag === ClassComponent || tag === IndeterminateComponent) &&
179195
memoizedState?.memoizedState !== undefined
180196
) {
181197
// If user uses Redux, context data will be stored in memoizedState of the Provider component => grab context object stored in the memoizedState
@@ -196,12 +212,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
196212
// if user uses useContext hook, context data will be stored in memoizedProps.value of the Context.Provider component => grab context object stored in memoizedprops
197213
// Different from other provider, such as Routes, BrowswerRouter, ReactRedux, ..., Context.Provider does not have a displayName
198214
// TODO: need to render this context provider when user use useContext hook.
199-
if (
200-
!nextJSDefaultComponent.has(componentName) &&
201-
!remixDefaultComponents.has(componentName) &&
202-
tag === ContextProvider &&
203-
!elementType._context.displayName
204-
) {
215+
if (tag === ContextProvider && !elementType._context.displayName) {
205216
let stateData = memoizedProps.value;
206217
if (stateData === null || typeof stateData !== 'object') {
207218
stateData = { CONTEXT: stateData };
@@ -221,12 +232,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
221232
// Check if node is a stateful class component when user use setState.
222233
// If user use setState to define/manage state, the state object will be stored in stateNode.state => grab the state object stored in the stateNode.state
223234
// Example: for tic-tac-toe demo-app: Board is a stateful component that use setState to store state data.
224-
if (
225-
!nextJSDefaultComponent.has(componentName) &&
226-
!remixDefaultComponents.has(componentName) &&
227-
stateNode?.state &&
228-
(tag === ClassComponent || tag === IndeterminateComponent)
229-
) {
235+
if ((tag === ClassComponent || tag === IndeterminateComponent) && stateNode?.state) {
230236
// Save component's state and setState() function to our record for future
231237
// time-travel state changing. Add record index to snapshot so we can retrieve.
232238
componentData.index = componentActionsRecord.saveNew(stateNode);
@@ -240,13 +246,11 @@ export default function createTree(currentFiberNode: Fiber): Tree {
240246
// --------OBTAIN STATE & DISPATCH METHODS FROM FUNCTIONAL COMPONENT--------
241247
// Check if node is a hooks useState function
242248
if (
243-
!nextJSDefaultComponent.has(componentName) &&
244-
!remixDefaultComponents.has(componentName) &&
245-
memoizedState &&
246249
(tag === FunctionComponent ||
247-
// tag === ClassComponent || WE SHOULD NOT BE ABLE TO USE HOOK IN CLASS
248250
tag === IndeterminateComponent ||
249-
tag === ContextProvider) //TODOD: Need to figure out why we need context provider
251+
//TODO: Need to figure out why we need context provider
252+
tag === ContextProvider) &&
253+
memoizedState
250254
) {
251255
if (memoizedState.queue) {
252256
try {
@@ -278,13 +282,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
278282
}
279283

280284
// This grabs stateless components
281-
if (
282-
!isStatefulComponent &&
283-
(tag === FunctionComponent ||
284-
tag === ClassComponent ||
285-
tag === IndeterminateComponent ||
286-
tag === ContextProvider)
287-
) {
285+
if (!isStatefulComponent) {
288286
newState = 'stateless';
289287
}
290288

@@ -299,33 +297,28 @@ export default function createTree(currentFiberNode: Fiber): Tree {
299297
*/
300298
let childNode: Tree = tree;
301299
// We want to add this fiber node to the snapshot
302-
if (
303-
(isStatefulComponent || newState === 'stateless') &&
304-
!nextJSDefaultComponent.has(componentName) &&
305-
!remixDefaultComponents.has(componentName)
306-
) {
307-
// Grab JSX Component & replace the 'fromLinkFiber' class value
308-
if (currentFiberNode.child?.stateNode?.setAttribute) {
309-
rtid = `fromLinkFiber${rtidCounter}`;
310-
// rtid = rtidCounter;
311-
// check if rtid is already present
312-
// remove existing rtid before adding a new one
313-
if (currentFiberNode.child.stateNode.classList.length > 0) {
314-
const lastClass =
315-
currentFiberNode.child.stateNode.classList[
316-
currentFiberNode.child.stateNode.classList.length - 1
317-
];
318-
if (lastClass.includes('fromLinkFiber')) {
319-
currentFiberNode.child.stateNode.classList.remove(lastClass);
320-
}
300+
301+
// Grab JSX Component & replace the 'fromLinkFiber' class value
302+
if (currentFiberNode.child?.stateNode?.setAttribute) {
303+
rtid = `fromLinkFiber${rtidCounter}`;
304+
// rtid = rtidCounter;
305+
// check if rtid is already present
306+
// remove existing rtid before adding a new one
307+
if (currentFiberNode.child.stateNode.classList.length > 0) {
308+
const lastClass =
309+
currentFiberNode.child.stateNode.classList[
310+
currentFiberNode.child.stateNode.classList.length - 1
311+
];
312+
if (lastClass.includes('fromLinkFiber')) {
313+
currentFiberNode.child.stateNode.classList.remove(lastClass);
321314
}
322-
currentFiberNode.child.stateNode.classList.add(rtid);
323315
}
324-
rtidCounter += 1; // I THINK THIS SHOULD BE UP IN THE IF STATEMENT. Still unsure the use of rtid
325-
326-
// Append the childNode to the tree
327-
childNode = tree.addChild(newState, componentName, componentData, rtid);
316+
currentFiberNode.child.stateNode.classList.add(rtid);
328317
}
318+
rtidCounter += 1; // I THINK THIS SHOULD BE UP IN THE IF STATEMENT. Still unsure the use of rtid
319+
320+
// Append the childNode to the tree
321+
childNode = tree.addChild(newState, componentName, componentData, rtid);
329322

330323
// ---------------------TRAVERSE TO NEXT FIBERNODE--------------------------
331324
// If currentFiberNode has children, recurse on children
@@ -336,7 +329,7 @@ export default function createTree(currentFiberNode: Fiber): Tree {
336329
_createTree(sibling, tree);
337330
}
338331

339-
// -------------RETURN THE TREE OUTPUT & PASS TO FRONTEND FOR RENDERING-------
332+
// ------------RETURN THE TREE OUTPUT & PASS TO FRONTEND FOR RENDERING------
340333
return tree;
341334
}
342335
}

0 commit comments

Comments
 (0)