Skip to content

Commit 45107cf

Browse files
committed
added reducer state logic to time jump so state on the same page can still time jump
1 parent e95923c commit 45107cf

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

src/backend/controllers/timeJump.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export default function timeJumpInitiation(mode: Status) {
4545
*
4646
*/
4747
async function updateReactFiberTree(
48-
//TypeScript Note: Adding a tree type to targetSnapshot throws errors for destructuring componentData below. Not sure how precisely to fix
4948
targetSnapshot,
5049
circularComponentTable: Set<any> = new Set(),
5150
): Promise<void> {
@@ -57,47 +56,43 @@ async function updateReactFiberTree(
5756
circularComponentTable.add(targetSnapshot);
5857
}
5958
// ------------------------STATELESS/ROOT COMPONENT-------------------------
60-
// Since stateless component has no data to update, continue to traverse its child nodes:
6159
if (targetSnapshot.state === 'stateless' || targetSnapshot.state === 'root') {
6260
targetSnapshot.children.forEach((child) => updateReactFiberTree(child, circularComponentTable));
6361
return;
6462
}
6563

66-
// Destructure component data:
67-
const { index, state, hooksIndex, hooksState } = targetSnapshot.componentData;
64+
const { index, state, hooksIndex, hooksState, reducerState } = targetSnapshot.componentData;
65+
6866
// ------------------------STATEFUL CLASS COMPONENT-------------------------
69-
// Check if it is a stateful class component
70-
// Index can be zero => falsy value => DO NOT REMOVE NULL
7167
if (index !== null) {
72-
// Obtain the BOUND update method at the given index
7368
const classComponent = componentActionsRecord.getComponentByIndex(index);
74-
// This conditional avoids the error that occurs when classComponent is undefined
7569
if (classComponent !== undefined) {
76-
// Update component state
77-
await classComponent.setState(
78-
// prevState contains the states of the snapshots we are jumping FROM, not jumping TO
79-
(prevState) => state,
80-
);
70+
await classComponent.setState(() => state);
8171
}
82-
// Iterate through new children after state has been set
8372
targetSnapshot.children.forEach((child) => updateReactFiberTree(child, circularComponentTable));
8473
return;
8574
}
8675

8776
// ----------------------STATEFUL FUNCTIONAL COMPONENT----------------------
88-
// Check if it is a stateful functional component
89-
// if yes, grab all relevant components for this snapshot by its index
90-
// call dispatch on each component passing in the corresponding currState value
91-
//index can be zero => falsy value => DO NOT REMOVE NULL
9277
if (hooksIndex !== null) {
93-
// Obtain the array of BOUND update methods at the given indexes.
94-
// NOTE: each useState will be a separate update method. So if a component have 3 useState, we will obtain an array of 3 update methods.
9578
const functionalComponent = componentActionsRecord.getComponentByIndexHooks(hooksIndex);
96-
// Update component state
97-
for (let i in functionalComponent) {
98-
await functionalComponent[i].dispatch(Object.values(hooksState)[i]);
79+
80+
// Handle reducer state if present
81+
if (reducerState) {
82+
try {
83+
// For reducer components, update using the first dispatch function
84+
await functionalComponent[0]?.dispatch(reducerState);
85+
} catch (err) {
86+
console.error('Error updating reducer state:', err);
87+
}
88+
} else {
89+
// Handle normal useState components
90+
for (let i in functionalComponent) {
91+
if (functionalComponent[i]?.dispatch) {
92+
await functionalComponent[i].dispatch(Object.values(hooksState)[i]);
93+
}
94+
}
9995
}
100-
// Iterate through new children after state has been set
10196
targetSnapshot.children.forEach((child) => updateReactFiberTree(child));
10297
return;
10398
}

0 commit comments

Comments
 (0)