Skip to content

Commit a574380

Browse files
committed
Remove duplicate state from backend. Working on review component data type.
1 parent 69e7240 commit a574380

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

src/backend/helpers.ts

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,54 @@ export const throttle = (
9696
};
9797
};
9898

99+
export const getHooksNames = (
100+
elementType: string,
101+
): { hookName: string; varName: string }[] | undefined => {
102+
// Initialize empty object to store the setters and getter
103+
let AST: any;
104+
try {
105+
AST = JSXParser.parse(elementType);
106+
} catch (e) {
107+
throw Error('Error occurs at helpers getHooksName.ts Cannot parse functional component.');
108+
}
109+
// Begin search for hook names, only if ast has a body property.
110+
111+
AST = AST.body;
112+
113+
// Statements get all the names of the hooks. For example: useCount, useWildcard, ...
114+
const statements: { hookName: string; varName: string }[] = [];
115+
/** All module exports always start off as a single 'FunctionDeclaration' type
116+
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
117+
* Iterate through AST of every function declaration
118+
* Check within each function declaration if there are hook declarations & variable name declaration */
119+
AST.forEach((functionDec: any) => {
120+
let declarationBody: any;
121+
if (functionDec.expression?.body) declarationBody = functionDec.expression.body.body;
122+
// check if functionDec.expression.body exists, then set declarationBody to functionDec's body
123+
else declarationBody = functionDec.body?.body ?? [];
124+
// Traverse through the function's funcDecs and Expression Statements
125+
declarationBody.forEach((elem: any) => {
126+
// Hooks will always be contained in a variable declaration
127+
if (elem.type === 'VariableDeclaration') {
128+
// Obtain the declarations array from elem.
129+
const { declarations } = elem;
130+
// Obtain the reactHook:
131+
const reactHook: string = declarations[0]?.init?.callee?.expressions[1]?.property?.name;
132+
if (reactHook === 'useState') {
133+
// Obtain the variable being set:
134+
let varName: string = declarations[1]?.id?.name;
135+
// Obtain the setState method:
136+
let hookName: string = declarations[2]?.id?.name;
137+
// Push reactHook & varName to statements array
138+
statements.push({ hookName, varName });
139+
}
140+
}
141+
});
142+
});
143+
return statements;
144+
};
145+
146+
// DEPERACATED: After React DEV Tool Update. This function no longer works. Keep for history record
99147
// // Helper function to grab the getters/setters from `elementType`
100148
// /**
101149
// * @method getHooksNames
@@ -166,50 +214,3 @@ export const throttle = (
166214
// return Object.values(hooksNames);
167215
// }
168216
// };
169-
170-
export const getHooksNames = (
171-
elementType: string,
172-
): { hookName: string; varName: string }[] | undefined => {
173-
// Initialize empty object to store the setters and getter
174-
let AST: any;
175-
try {
176-
AST = JSXParser.parse(elementType);
177-
} catch (e) {
178-
throw Error('Error occurs at helpers ts getComponentName. Cannot parse functional component.');
179-
}
180-
// Begin search for hook names, only if ast has a body property.
181-
182-
AST = AST.body;
183-
184-
// Statements get all the names of the hooks. For example: useCount, useWildcard, ...
185-
const statements: { hookName: string; varName: string }[] = [];
186-
/** All module exports always start off as a single 'FunctionDeclaration' type
187-
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
188-
* Iterate through AST of every function declaration
189-
* Check within each function declaration if there are hook declarations & variable name declaration */
190-
AST.forEach((functionDec: any) => {
191-
let declarationBody: any;
192-
if (functionDec.expression?.body) declarationBody = functionDec.expression.body.body;
193-
// check if functionDec.expression.body exists, then set declarationBody to functionDec's body
194-
else declarationBody = functionDec.body?.body ?? [];
195-
// Traverse through the function's funcDecs and Expression Statements
196-
declarationBody.forEach((elem: any) => {
197-
// Hooks will always be contained in a variable declaration
198-
if (elem.type === 'VariableDeclaration') {
199-
// Obtain the declarations array from elem.
200-
const { declarations } = elem;
201-
// Obtain the reactHook:
202-
const reactHook: string = declarations[0]?.init?.callee?.expressions[1]?.property?.name;
203-
if (reactHook === 'useState') {
204-
// Obtain the variable being set:
205-
let varName: string = declarations[1]?.id?.name;
206-
// Obtain the setState method:
207-
let hookName: string = declarations[2]?.id?.name;
208-
// Push reactHook & varName to statements array
209-
statements.push({ hookName, varName });
210-
}
211-
}
212-
});
213-
});
214-
return statements;
215-
};

src/backend/linkFiber.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ type ReactimeData = {
7878
[key: string]: any;
7979
};
8080

81-
let rtidCounter = 0;
82-
8381
/**
8482
* @function traverseHooks
8583
* @param memoizedState - The current state of the component associated with the current Fiber node.
@@ -281,6 +279,9 @@ function trimContextData(
281279
* @param fromSibling A boolean, default initialized to false
282280
* @return An instance of a Tree object
283281
*/
282+
// TODO: Not sure why the ritd need to be outside of the createTree function. Want to put inside, but in case this need to be keep track for front end.
283+
let rtidCounter = 0;
284+
284285
export function createTree(
285286
currentFiberNode: Fiber,
286287
circularComponentTable: Set<Fiber> = new Set(),
@@ -431,7 +432,7 @@ export function createTree(
431432
if (stateNode?.state && (tag === ClassComponent || tag === IndeterminateComponent)) {
432433
// Save component's state and setState() function to our record for future
433434
// time-travel state changing. Add record index to snapshot so we can retrieve.
434-
componentData.index = componentActionsRecord.saveNew(stateNode.state, stateNode);
435+
componentData.index = componentActionsRecord.saveNew(stateNode);
435436
// Save state information in componentData.
436437
componentData.state = stateNode.state;
437438
// Passess to front end
@@ -457,14 +458,13 @@ export function createTree(
457458
// which includes the dispatch() function we use to change their state.
458459
const hooksStates = traverseHooks(memoizedState);
459460
const hooksNames = getHooksNames(elementType.toString());
460-
console.log({ hooksNames });
461461
// Intialize state & index:
462462
newState.hooksState = [];
463463
componentData.hooksState = [];
464464
componentData.hooksIndex = [];
465465
hooksStates.forEach((state, i) => {
466466
// Save component's state and dispatch() function to our record for future time-travel state changing. Add record index to snapshot so we can retrieve.
467-
componentData.hooksIndex.push(componentActionsRecord.saveNew(state.state, state.component));
467+
componentData.hooksIndex.push(componentActionsRecord.saveNew(state.component));
468468
// Save state information in componentData.
469469
newState.hooksState.push({ [hooksNames[i].hookName]: state.state });
470470
// Passess to front end

src/backend/masterState.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import {
1010
HookStates, // array of hook state items
1111
} from './types/backendTypes';
1212

13+
type ComponentAction = any[];
14+
1315
// HookState is an array that contains a "component" for
1416
// every single state change that occurs in the app
1517
// Information on these components include ComponentData as well as state
1618
// For class components, there will be one "component" for each snapshot
1719
// For functional components that utilize Hooks, there will be one "component"
1820
// for each setter/getter every time we have a new snapshot
19-
let componentActionsRecord: HookStates = [];
21+
let componentActionsRecord: ComponentAction = [];
2022
let index: number;
2123
index = 0;
2224

@@ -26,26 +28,23 @@ export default {
2628
index = 0;
2729
},
2830
// adds new component to ComponentActionsRecord
29-
saveNew: (state, component): number => {
30-
console.log('masterState', { component, componentActionsRecord });
31-
componentActionsRecord[index] = { state, component };
31+
saveNew: (component): number => {
32+
componentActionsRecord[index] = component;
3233
index++;
3334

3435
return index - 1;
3536
},
3637
getRecordByIndex: (inputIndex: number): HookStateItem => componentActionsRecord[inputIndex],
3738
// this is used for class components -
3839
/* inputIndex will always be a fixed number (coming in timeJump.ts) */
39-
getComponentByIndex: (inputIndex: number): HookStateItem['component'] | undefined =>
40-
componentActionsRecord[inputIndex] ? componentActionsRecord[inputIndex].component : undefined,
40+
getComponentByIndex: (inputIndex: number): any | undefined =>
41+
componentActionsRecord[inputIndex] ? componentActionsRecord[inputIndex] : undefined,
4142
// this is used for react hooks - hooks will be passed in as an array from timeJump.ts
42-
getComponentByIndexHooks: (
43-
inputIndex: Array<number> = [],
44-
): Array<HookStateItem['component']> | undefined => {
43+
getComponentByIndexHooks: (inputIndex: Array<number> = []): any[] | undefined => {
4544
const multiDispatch = [];
4645
for (let i = 0; i < inputIndex.length; i++) {
4746
if (componentActionsRecord[inputIndex[i]]) {
48-
multiDispatch.push(componentActionsRecord[inputIndex[i]].component);
47+
multiDispatch.push(componentActionsRecord[inputIndex[i]]);
4948
}
5049
}
5150
return multiDispatch;

0 commit comments

Comments
 (0)