Skip to content

Commit 69e7240

Browse files
committed
Refactor timejump for consitency. Working on removing state from backend
1 parent ecad08c commit 69e7240

File tree

5 files changed

+60
-61
lines changed

5 files changed

+60
-61
lines changed

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import { set } from 'lodash';
22
import React from 'react';
3-
import { useState, useEffect } from 'react';
3+
import { useState } from 'react';
44

55
function Home() {
66
const [dummyDummy1, setDummyData1] = useState('dummyData1');
77
const [dummyDummy2, setDummyData2] = useState('dummyData2');
8-
const [count, setCount] = useState(0);
9-
useEffect(() => {
10-
setCount((count) => count + 1);
11-
}, [dummyDummy1]);
8+
129
function handleClick1() {
1310
setDummyData1((dummyData1) => (dummyData1 === 'dummyData1' ? 'test1' : 'dummyData1'));
1411
}
15-
const [count1, setCount1] = useState(0);
1612

1713
function handleClick2() {
1814
setDummyData2((dummyData2) => (dummyData2 === 'dummyData2' ? 'test2' : 'dummyData2'));
@@ -30,7 +26,6 @@ function Home() {
3026
{' '}
3127
Test{' '}
3228
</button>
33-
<p>Count: {count}</p>
3429
{/* <h1>Lorem Ipsum</h1> */}
3530
{/* <p>
3631
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import React, { useState } from 'react';
2-
2+
import Box from './Box';
3+
import { BoardText } from '../../types';
4+
import { Function } from 'lodash';
35
function Increment() {
46
const [count, setCount] = useState(0);
5-
7+
const [value, setValue]: [BoardText, any] = useState('-');
68
return (
7-
<button className='increment' onClick={() => setCount(count + 1)}>
8-
You clicked me {count} times.
9-
</button>
9+
<div>
10+
<button className='increment' onClick={() => setCount(count + 1)}>
11+
You clicked me {count} times.
12+
</button>
13+
{/* <div> */}
14+
{/* <Box
15+
value={value}
16+
column={3}
17+
row={2}
18+
handleBoxClick={() => setValue((value: BoardText) => (value == 'X' ? 'O' : 'X'))}
19+
/> */}
20+
{/* </div> */}
21+
</div>
1022
);
1123
}
1224

src/backend/linkFiber.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ function convertDataToString(
210210
console.log('linkFiber', { reactDevData, key });
211211
throw Error(`Error caught at converDataToString: ${err}`);
212212
}
213-
return reactimeData;
214213
}
214+
return reactimeData;
215215
}
216216
// ------------------------FILTER STATE & CONTEXT DATA--------------------------
217217
/**
@@ -359,7 +359,7 @@ export function createTree(
359359
context: {};
360360
state?: {};
361361
hooksState?: any[];
362-
hooksIndex?: number;
362+
hooksIndex?: number[];
363363
index?: number;
364364
} = {
365365
actualDuration,
@@ -458,13 +458,17 @@ export function createTree(
458458
const hooksStates = traverseHooks(memoizedState);
459459
const hooksNames = getHooksNames(elementType.toString());
460460
console.log({ hooksNames });
461+
// Intialize state & index:
461462
newState.hooksState = [];
462-
componentData.state = {};
463+
componentData.hooksState = [];
464+
componentData.hooksIndex = [];
463465
hooksStates.forEach((state, i) => {
464-
hooksIndex = componentActionsRecord.saveNew(state.state, state.component);
465-
componentData.hooksIndex = hooksIndex;
466+
// 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));
468+
// Save state information in componentData.
466469
newState.hooksState.push({ [hooksNames[i].hookName]: state.state });
467-
componentData.state[hooksNames[i].varName] = state.state;
470+
// Passess to front end
471+
componentData.hooksState.push({ [hooksNames[i].hookName]: state.state });
468472
});
469473
isStatefulComponent = true;
470474
}

src/backend/masterState.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ export default {
3939
getComponentByIndex: (inputIndex: number): HookStateItem['component'] | undefined =>
4040
componentActionsRecord[inputIndex] ? componentActionsRecord[inputIndex].component : undefined,
4141
// this is used for react hooks - hooks will be passed in as an array from timeJump.ts
42-
getComponentByIndexHooks: (inputIndex: Array<number> = []): Array<any> => {
42+
getComponentByIndexHooks: (
43+
inputIndex: Array<number> = [],
44+
): Array<HookStateItem['component']> | undefined => {
4345
const multiDispatch = [];
4446
for (let i = 0; i < inputIndex.length; i++) {
4547
if (componentActionsRecord[inputIndex[i]]) {

src/backend/timeJump.ts

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -28,67 +28,53 @@ export default function timeJump(mode) {
2828
// Base Case: if has visited, return
2929
if (circularComponentTable.has(target)) {
3030
return;
31-
} else circularComponentTable.add(target);
31+
} else {
32+
circularComponentTable.add(target);
33+
}
3234
// ------------------------STATELESS/ROOT COMPONENT-------------------------
3335
// Since stateless component has no data to update, continue to traverse its child nodes:
3436
if (target.state === 'stateless' || target.state === 'root') {
3537
target.children.forEach((child) => jump(child));
3638
return;
3739
}
38-
// Obtain component data & its update method at the given index
39-
const component = componentActionsRecord.getComponentByIndex(target.componentData.index);
4040

41+
// Destructure component data:
42+
const { index, state, hooksIndex, hooksState } = target.componentData;
4143
// ------------------------STATEFUL CLASS COMPONENT-------------------------
4244
// for stateful class components
4345
// check if it is a stateful class component
4446
// if yes, find the component by its index and assign it to a variable
4547
// call that components setState method to reset state to the state at the time of the jump snapshot
46-
if (component && component.setState) {
47-
console.log('timeJump CLASS', componentActionsRecord);
48-
await component.setState(
49-
// prevState contains the states of the snapshots we are jumping FROM, not jumping TO
50-
(prevState) => target.state,
51-
// (prevState) => {
52-
// Object.keys(prevState).forEach((key) => {
53-
// // the if conditional below does not appear to ever be reached if all states are defined - leaving code in just in case codebases do have undefined states
54-
// // if (target.state[key] !== undefined) {
55-
// // target.state[key] = undefined;
56-
// // }
57-
// });
58-
// return target.state;
59-
// }
60-
61-
// Iterate through new children after state has been set
62-
() => target.children.forEach((child) => jump(child)),
63-
);
48+
//index can be zero => falsy value => DO NOT REMOVE UNDEFINED
49+
if (index !== undefined) {
50+
// Obtain component data & its update method at the given index
51+
const classComponent = componentActionsRecord.getComponentByIndex(index);
52+
if (classComponent && classComponent.setState) {
53+
await classComponent.setState(
54+
// prevState contains the states of the snapshots we are jumping FROM, not jumping TO
55+
(prevState) => state,
56+
);
57+
}
58+
// Iterate through new children after state has been set
59+
target.children.forEach((child) => jump(child));
6460
return;
6561
}
6662

67-
// target.children.forEach((child) => {
68-
// if (!circularComponentTable.has(child)) {
69-
// circularComponentTable.add(child);
70-
// jump(child);
71-
// }
72-
// });
73-
7463
// ----------------------STATEFUL FUNCTIONAL COMPONENT----------------------
75-
// REACT HOOKS
7664
// check if component states are set with hooks
77-
// if yes, grab all relevant components for this snapshot in numArr
65+
// if yes, grab all relevant components for this snapshot by its index
7866
// call dispatch on each component passing in the corresponding currState value
79-
if (target.state && target.state.hooksState) {
80-
const currState = target.state.hooksState;
81-
const numArr: number[] = [];
82-
let counter = 1;
83-
while (counter < currState.length + 1) {
84-
numArr.push(target.componentData.hooksIndex - currState.length + counter);
85-
counter += 1;
86-
}
87-
const hooksComponent = componentActionsRecord.getComponentByIndexHooks(numArr);
88-
console.log('timeJumps', { hooksComponent, currState });
89-
for (let i = 0; i < currState.length; i += 1) {
90-
hooksComponent[i].dispatch(Object.values(currState[i])[0]);
67+
//index can be zero => falsy value => DO NOT REMOVE UNDEFINED
68+
if (hooksIndex !== undefined) {
69+
// Obtain component data & its update method at the given index
70+
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+
}
9175
}
76+
target.children.forEach((child) => jump(child));
77+
return;
9278
}
9379
}
9480

0 commit comments

Comments
 (0)