Skip to content

Commit 520e55b

Browse files
committed
Fully refactor to have 1 true source of state & props
1 parent 08a75e5 commit 520e55b

File tree

7 files changed

+48
-56
lines changed

7 files changed

+48
-56
lines changed

demo-app/src/client/Components/Increment.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BoardText } from '../../types';
44
import { Function } from 'lodash';
55
function Increment() {
66
const [count, setCount] = useState(0);
7-
const [value, setValue]: [BoardText, any] = useState('-');
7+
// const [value, setValue]: [BoardText, any] = useState('-');
88
return (
99
<div>
1010
<button className='increment' onClick={() => setCount(count + 1)}>

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,12 @@ export default function ComponentMap({
359359

360360
<div className='tooltipWrapper'>
361361
<h2>State:</h2>
362-
{formatData(tooltipData.componentData.state, 'state')}
362+
{formatData(
363+
tooltipData.componentData.hooksIndex
364+
? tooltipData.componentData.hooksState
365+
: tooltipData.componentData.state,
366+
'state',
367+
)}
363368
</div>
364369
</div>
365370
</div>

src/backend/linkFiber.ts

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ type ReactimeData = {
8989
function traverseHooks(memoizedState: any): HookStates {
9090
const hooksStates: HookStates = [];
9191
while (memoizedState) {
92-
// the !== undefined conditional is necessary here for correctly displaying react hooks because TypeScript recognizes 0 and "" as falsey value - DO NOT REMOVE
9392
if (memoizedState.queue) {
9493
hooksStates.push({
9594
component: memoizedState.queue,
@@ -293,6 +292,13 @@ export function createTree(
293292
// if (!tree) return tree; //TO BE DELETE: WE HAVE A DEFAULT PARAMETER TREE, THIS WILL NEVER BE TRUE
294293
// These have the newest state. We update state and then
295294
// called updateSnapshotTree()
295+
296+
// Base Case: if has visited, return
297+
if (circularComponentTable.has(currentFiberNode)) {
298+
return;
299+
} else {
300+
circularComponentTable.add(currentFiberNode);
301+
}
296302
const {
297303
sibling,
298304
stateNode,
@@ -359,7 +365,7 @@ export function createTree(
359365
props: {};
360366
context: {};
361367
state?: {};
362-
hooksState?: any[];
368+
hooksState?: {};
363369
hooksIndex?: number[];
364370
index?: number;
365371
} = {
@@ -381,7 +387,16 @@ export function createTree(
381387
tag === ContextProvider) &&
382388
memoizedProps
383389
) {
384-
Object.assign(componentData.props, convertDataToString(memoizedProps));
390+
switch (elementType.name) {
391+
case 'Router':
392+
componentData.props = { pathname: memoizedProps?.location?.pathname };
393+
break;
394+
case 'RenderedRoute':
395+
componentData.props = { pathname: memoizedProps?.match?.pathname };
396+
break;
397+
default:
398+
Object.assign(componentData.props, convertDataToString(memoizedProps));
399+
}
385400
}
386401

387402
// ------------APPEND STATE & CONTEXT DATA FROM REACT DEV TOOL----------------
@@ -441,8 +456,6 @@ export function createTree(
441456
}
442457

443458
// ---------OBTAIN STATE & DISPATCH METHODS FROM FUNCTIONAL COMPONENT---------
444-
// REGULAR REACT HOOKS
445-
let hooksIndex;
446459
// Check if node is a hooks useState function
447460
if (
448461
memoizedState &&
@@ -460,15 +473,15 @@ export function createTree(
460473
const hooksNames = getHooksNames(elementType.toString());
461474
// Intialize state & index:
462475
newState.hooksState = [];
463-
componentData.hooksState = [];
476+
componentData.hooksState = {};
464477
componentData.hooksIndex = [];
465-
hooksStates.forEach((state, i) => {
478+
hooksStates.forEach(({ state, component }, i) => {
466479
// 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.component));
480+
componentData.hooksIndex.push(componentActionsRecord.saveNew(component));
468481
// Save state information in componentData.
469-
newState.hooksState.push({ [hooksNames[i].hookName]: state.state });
482+
newState.hooksState.push({ [hooksNames[i].varName]: state });
470483
// Passess to front end
471-
componentData.hooksState.push({ [hooksNames[i].hookName]: state.state });
484+
componentData.hooksState[hooksNames[i].varName] = state;
472485
});
473486
isStatefulComponent = true;
474487
}
@@ -492,7 +505,6 @@ export function createTree(
492505
*/
493506
let rtid: string | null = null;
494507
// We want to add this fiber node to the snapshot
495-
// if (componentFound || (newState === 'stateless' && !newState.hooksState)) {
496508
if (isStatefulComponent || newState === 'stateless') {
497509
// Grab JSX Component & replace the 'fromLinkFiber' class value
498510
if (currentFiberNode.child?.stateNode?.setAttribute) {
@@ -512,41 +524,30 @@ export function createTree(
512524
currentFiberNode.child.stateNode.classList.add(rtid);
513525
}
514526
rtidCounter += 1; // I THINK THIS SHOULD BE UP IN THE IF STATEMENT. Still unsure the use of rtid
527+
528+
// Obtain component name:
529+
const componentName =
530+
elementType?._context?.displayName || //For ContextProvider
531+
elementType?._result?.name || //For lazy Component
532+
elementType?.render?.name ||
533+
elementType?.name ||
534+
'nameless';
515535
// checking if tree fromSibling is true
516536
if (fromSibling) {
517-
newNode = tree.addSibling(
518-
newState,
519-
elementType ? elementType.name : 'nameless',
520-
componentData,
521-
rtid,
522-
);
537+
newNode = tree.addSibling(newState, componentName, componentData, rtid);
523538
} else {
524-
newNode = tree.addChild(
525-
newState,
526-
elementType ? elementType.name : 'nameless',
527-
componentData,
528-
rtid,
529-
);
539+
newNode = tree.addChild(newState, componentName, componentData, rtid);
530540
}
531541
} else {
532542
newNode = tree;
533543
}
534544

535545
// ----------------------TRAVERSE TO NEXT FIBERNODE---------------------------
536-
// Recurse on children
537-
if (child && !circularComponentTable.has(child)) {
538-
// If this node had state we appended to the children array,
539-
// so attach children to the newly appended child.
540-
// Otherwise, attach children to this same node.
541-
circularComponentTable.add(child);
542-
createTree(child, circularComponentTable, newNode);
543-
}
546+
// If currentFiberNode has children, recurse on children
547+
if (child) createTree(child, circularComponentTable, newNode);
544548

545-
// Recurse on siblings
546-
if (sibling && !circularComponentTable.has(sibling)) {
547-
circularComponentTable.add(sibling);
548-
createTree(sibling, circularComponentTable, newNode, true);
549-
}
549+
// If currentFiberNode has siblings, recurse on siblings
550+
if (sibling) createTree(sibling, circularComponentTable, newNode, true);
550551

551552
// -------------RETURN THE TREE OUTPUT & PASS TO FRONTEND FOR RENDERING-------
552553
return tree;

src/backend/masterState.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
/* eslint-disable guard-for-in */
55
/* eslint-disable no-restricted-syntax */
66

7-
import {
8-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
9-
HookStateItem, // obj with state and component
10-
HookStates, // array of hook state items
11-
} from './types/backendTypes';
12-
137
type ComponentAction = any[];
148

159
// HookState is an array that contains a "component" for

src/backend/snapShot.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { Snapshot, Status, FiberRoot } from './types/backendTypes';
2-
// import function that creates a tree
3-
import Tree from './tree';
42
// passes the data down to its components
53
import componentActionsRecord from './masterState';
64
import routes from './routes';

src/backend/timeJump.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ export default function timeJump(mode) {
2323
// Recursively change state of tree
2424
// Set the state of the origin tree if the component is stateful
2525
async function jump(target): Promise<void> {
26-
// ----------------
2726
if (!target) return;
2827
// Base Case: if has visited, return
2928
if (circularComponentTable.has(target)) {
@@ -68,11 +67,10 @@ export default function timeJump(mode) {
6867
if (hooksIndex !== undefined) {
6968
// Obtain component data & its update method at the given index
7069
const functionalComponent = componentActionsRecord.getComponentByIndexHooks(hooksIndex);
71-
if (functionalComponent.length) {
72-
for (let i = 0; i < hooksState.length; i += 1) {
73-
await functionalComponent[i].dispatch(Object.values(hooksState[i])[0]);
74-
}
70+
for (let i in functionalComponent) {
71+
await functionalComponent[i].dispatch(Object.values(hooksState)[i]);
7572
}
73+
// Iterate through new children after state has been set
7674
target.children.forEach((child) => jump(child));
7775
return;
7876
}

src/backend/types/backendTypes.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ export interface HookStateItem {
8282
*/
8383
export type HookStates = Array<HookStateItem>;
8484

85-
export interface State {
86-
state?: {} | number;
87-
hooksState?: HookStates;
88-
}
89-
9085
export type WorkTag =
9186
| 0
9287
| 1
@@ -248,4 +243,5 @@ export type Fiber = {
248243
*/
249244
export type FiberRoot = {
250245
current: Fiber;
246+
Nok_Nok: string;
251247
};

0 commit comments

Comments
 (0)