Skip to content

Commit 1cbd30c

Browse files
committed
Update createTree
1 parent 8a83b97 commit 1cbd30c

File tree

2 files changed

+70
-110
lines changed

2 files changed

+70
-110
lines changed

src/backend/controllers/createTree.ts

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
// import typescript types
1+
// --------------------------START OF IMPORT------------------------------------
22
import {
3-
// object with tree structure
4-
Fiber,
5-
ComponentData,
6-
} from '../types/backendTypes';
3+
getHooksNames,
4+
getHooksStateAndUpdateMethod,
5+
// getStateAndContextData, //COMMENT OUT SINCE EXTRACTING CONTEXT IS STILL IN EXPERIMENT
6+
filterAndFormatData,
7+
} from './statePropExtractors';
8+
9+
import { Fiber, ComponentData } from '../types/backendTypes';
710
import {
811
FunctionComponent,
912
ClassComponent,
10-
IndeterminateComponent, // Before we know whether it is function or class
13+
IndeterminateComponent,
1114
ContextProvider,
1215
} from '../types/backendTypes';
13-
// import function that creates a tree
16+
1417
import Tree from '../models/tree';
15-
// passes the data down to its components
1618
import componentActionsRecord from '../models/masterState';
17-
import {
18-
getHooksNames,
19-
getHooksStateAndUpdateMethod,
20-
// getStateAndContextData, //COMMENT OUT SINCE EXTRACTING CONTEXT IS STILL IN EXPERIMENT
21-
filterAndFormatData,
22-
} from './statePropExtractors';
2319
import {
2420
nextJSDefaultComponent,
2521
remixDefaultComponents,
@@ -54,7 +50,6 @@ export default function createTree(currentFiberNode: Fiber): Tree {
5450
sibling,
5551
stateNode,
5652
child,
57-
// with memoizedState we can grab the root type and construct an Abstract Syntax Tree from the hooks structure using Acorn in order to extract the hook getters and match them with their corresponding setters in an object
5853
memoizedState,
5954
memoizedProps,
6055
elementType,
@@ -63,33 +58,17 @@ export default function createTree(currentFiberNode: Fiber): Tree {
6358
actualStartTime,
6459
selfBaseDuration,
6560
treeBaseDuration,
66-
_debugHookTypes,
61+
// _debugHookTypes, //COMMENT OUT SINCE EXTRACTING CONTEXT IS STILL IN EXPERIMENT
6762
} = currentFiberNode;
6863

6964
// Obtain component name:
7065
let componentName =
7166
elementType?._context?.displayName || //For ContextProvider
7267
elementType?._result?.name || //For lazy Component
7368
elementType?.render?.name ||
74-
elementType?.name ||
69+
elementType?.name || //For Functional/Class Component
7570
'nameless';
7671

77-
// console.log('CREATE TREE', {
78-
// currentFiberNode,
79-
// tag,
80-
// // elementType,
81-
// componentName:
82-
// elementType?._context?.displayName || //For ContextProvider
83-
// elementType?._result?.name || //For lazy Component
84-
// elementType?.render?.name ||
85-
// elementType?.name ||
86-
// elementType,
87-
// // memoizedProps,
88-
// // memoizedState,
89-
// // stateNode,
90-
// // dependencies,
91-
// // _debugHookTypes,
92-
// });
9372
// --------------------FILTER COMPONENTS/FIBER NODE-------------------------
9473
/**
9574
* For the snapshot tree,
@@ -193,13 +172,11 @@ export default function createTree(currentFiberNode: Fiber): Tree {
193172
// 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
194173
// Example: for tic-tac-toe demo-app: Board is a stateful component that use setState to store state data.
195174
if ((tag === ClassComponent || tag === IndeterminateComponent) && stateNode?.state) {
196-
// Save component's state and setState() function to our record for future
197-
// time-travel state changing. Add record index to snapshot so we can retrieve.
175+
// Save component's state and setState() function to our record for future time-travel state changing. Add record index to snapshot so we can retrieve.
198176
componentData.index = componentActionsRecord.saveNew(stateNode);
199177
// Save state information in componentData.
200178
componentData.state = stateNode.state;
201179
// Passess to front end
202-
// TODO: Refactor this, this is currently being used for Tree & Diff tabs
203180
newState = componentData.state;
204181
}
205182

@@ -208,16 +185,12 @@ export default function createTree(currentFiberNode: Fiber): Tree {
208185
if (
209186
(tag === FunctionComponent ||
210187
tag === IndeterminateComponent ||
211-
//TODO: Need to figure out why we need context provider
188+
//TODO: Need reasoning for why we evaluate context provider
212189
tag === ContextProvider) &&
213190
memoizedState
214191
) {
215192
if (memoizedState.queue) {
216193
try {
217-
// Hooks states are stored as a linked list using memoizedState.next,
218-
// so we must traverse through the list and get the states.
219-
// We then store them along with the corresponding memoizedState.queue,
220-
// which includes the dispatch() function we use to change their state.
221194
const hooksStates = getHooksStateAndUpdateMethod(memoizedState);
222195
const hooksNames = getHooksNames(elementType.toString());
223196
// Intialize state & index:

src/backend/types/backendTypes.ts

Lines changed: 56 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ export interface ComponentData {
6464
}
6565

6666
/**
67-
* @type HookStateItem
68-
* @member state -
69-
* @member component -
67+
* @member state - states within the current functional component
68+
* @member component - contains bound dispatch method to update state of the current functional component
7069
*/
7170
export interface HookStateItem {
7271
state: any;
@@ -108,9 +107,12 @@ export type WorkTag =
108107

109108
export const FunctionComponent = 0;
110109
export const ClassComponent = 1;
111-
export const IndeterminateComponent = 2; // Before we know whether it is function or class
112-
export const HostRoot = 3; // Root of a host tree. Could be nested inside another node.
113-
export const HostPortal = 4; // A subtree. Could be an entry point to a different renderer.
110+
/** Before we know whether it is function or class */
111+
export const IndeterminateComponent = 2;
112+
/** Root of a host tree. Could be nested inside another node. */
113+
export const HostRoot = 3;
114+
/** A subtree. Could be an entry point to a different renderer. */
115+
export const HostPortal = 4;
114116
/**
115117
* Host Component: a type of component that represents a native DOM element in the browser environment, such as div, span, input, h1 etc.
116118
*/
@@ -142,94 +144,79 @@ export const LegacyHiddenComponent = 24;
142144
* @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.
143145
* @member actualStartTime - The time at which the rendering of the current Fiber node started during the previous render cycle.
144146
* @member child - Pointer to the first child.
145-
* @member dependencies - An array of values (such as state or props) that the current Fiber node depends on. This is used to determine whether the node needs to be re-rendered.
146-
* @member elementType - The type of the current Fiber node's element (e.g. the component function or class, or the DOM element type). Example: div, h1,
147-
* @member index - Index of the current Fiber node. Ex: if a div has 3 headings. The first child is heading with index = 0. The next sibling is a heading with index = 1 & the last sibling is a heading with index = 2.
148-
* @member key - Unique identifier of this child, used to identify the node when rendering lists of components.
147+
* @member elementType - The type of the current Fiber node's element (e.g. the component function or class, or the DOM element type). For class/functional component, elmementType stores the function definition.
149148
* @member memoizedProps - The current props of the component associated with the current Fiber node.
150149
* @member memoizedState - The current state of the component associated with the current Fiber node.
151150
* @member selfBaseDuration - The base duration of the current Fiber node's render phase (excluding the time taken to render its children). This field is only set when the enableProfilerTimer flag is enabled.
152151
* @member sibling - Pointer to next sibling
153-
* @member stateNode - The local state associated with this fiber.
152+
* @member stateNode - The local state associated with this fiber. For classComponent, stateNode contains current state and the bound update methods of the component
154153
* @member tag - The type of the current Fiber node, such as FunctionComponent, ClassComponent, or HostComponent (for DOM elements).
155154
* @member treeBaseDuration - The total base duration of the current Fiber node's subtree. This field is only set when the enableProfilerTimer flag is enabled.
156-
* @member type - Same as elementType.
157155
* @member _debugHookTypes - An array of hooks used for debugging purposes.
158156
*/
159157
export type Fiber = {
160-
// Tag identifying the type of fiber.
161-
tag: WorkTag;
162-
163-
// Unique identifier of this child.
164-
// key: null | string;
165-
166-
// The value of element.type which is used to preserve the identity during
167-
// reconciliation of this child.
168-
elementType: any;
169-
170-
// The resolved function/class/ associated with this fiber.
171-
// type: any;
172-
173-
// The local state associated with this fiber.
174-
stateNode: any;
175-
176-
// Conceptual aliases
177-
// parent : Instance -> return The parent happens to be the same as the
178-
// return fiber since we've merged the fiber and instance.
179-
180-
// Remaining fields belong to Fiber
158+
/**
159+
* Time spent rendering this Fiber and its descendants for the current update.
160+
*
161+
* This tells us how well the tree makes use of sCU for memoization. It is reset to 0 each time we render and only updated when we don't bailout.
162+
*
163+
* This field is only set when the enableProfilerTimer flag is enabled.
164+
*/
165+
actualDuration?: number;
181166

182-
// The Fiber to return to after finishing processing this one.
183-
// This is effectively the parent, but there can be multiple parents (two)
184-
// so this is only the parent of the thing we're currently processing.
185-
// It is conceptually the same as the return address of a stack frame.
186-
// return: Fiber | null,
167+
/**
168+
* If the Fiber is currently active in the "render" phase, this marks the time at which the work began.
169+
*
170+
* This field is only set when the enableProfilerTimer flag is enabled.
171+
*/
172+
actualStartTime?: number;
187173

188174
// Singly Linked List Tree Structure.
175+
/** Pointer to the first child. */
189176
child: Fiber | null;
190-
sibling: Fiber | null;
191-
// index: number;
192177

193-
// Input is the data coming into process this fiber. Arguments. Props.
194-
// pendingProps: any, // This type will be more specific once we overload the tag.
195-
// memoizedProps: any, // The props used to create the output.
178+
/**
179+
* The type of the current Fiber node's element (e.g. the component function or class, or the DOM element type).
180+
*
181+
* For class/functional component, elmementType stores the function definition.
182+
*/
183+
elementType: any;
196184

197-
// The state used to create the output
185+
/** The current props of the component associated with the current Fiber node. */
198186
memoizedState: any;
199187

188+
/** The current state of the component associated with the current Fiber node. */
200189
memoizedProps: any;
201190

202-
// Singly linked list fast path to the next fiber with side-effects.
203-
// nextEffect: Fiber | null,
204-
205-
// This is a pooled version of a Fiber. Every fiber that gets updated will
206-
// eventually have a pair. There are cases when we can clean up pairs to save
207-
// memory if we need to.
208-
// alternate: Fiber | null,
191+
/**
192+
* Duration of the most recent render time for this Fiber. This value is not updated when we bailout for memoization purposes.
193+
*
194+
* This field is only set when the enableProfilerTimer flag is enabled.
195+
*/
196+
selfBaseDuration?: number;
209197

210-
// Time spent rendering this Fiber and its descendants for the current update.
211-
// This tells us how well the tree makes use of sCU for memoization.
212-
// It is reset to 0 each time we render and only updated when we don't bailout.
213-
// This field is only set when the enableProfilerTimer flag is enabled.
214-
actualDuration?: number;
198+
// Singly Linked List Tree Structure.
199+
/** Pointer to next sibling */
200+
sibling: Fiber | null;
215201

216-
// If the Fiber is currently active in the "render" phase,
217-
// This marks the time at which the work began.
218-
// This field is only set when the enableProfilerTimer flag is enabled.
219-
actualStartTime?: number;
202+
/**
203+
* The local state associated with this fiber.
204+
*
205+
* For classComponent, stateNode contains current state and the bound update methods of the component.
206+
*/
207+
stateNode: any;
220208

221-
// Duration of the most recent render time for this Fiber.
222-
// This value is not updated when we bailout for memoization purposes.
223-
// This field is only set when the enableProfilerTimer flag is enabled.
224-
selfBaseDuration?: number;
209+
/** The type of the current Fiber node, such as FunctionComponent, ClassComponent, or HostComponent (for DOM elements). */
210+
tag: WorkTag;
225211

226-
// Sum of base times for all descendants of this Fiber.
227-
// This value bubbles up during the "complete" phase.
228-
// This field is only set when the enableProfilerTimer flag is enabled.
212+
/**
213+
* Sum of base times for all descendants of this Fiber. This value bubbles up during the "complete" phase.
214+
*
215+
* This field is only set when the enableProfilerTimer flag is enabled.
216+
*/
229217
treeBaseDuration?: number;
230218

231-
// dependencies: any;
232-
219+
/** An array of hooks used for debugging purposes. */
233220
_debugHookTypes: string[] | null;
234221
};
235222

0 commit comments

Comments
 (0)