Skip to content

Commit f901503

Browse files
committed
Complete review for dispatch setState jumping
1 parent bb8b317 commit f901503

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

src/backend/linkFiber.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,6 @@ export default function linkFiber(snapShot: Snapshot, mode: Status): () => void
655655
// eslint-disable-next-line prefer-destructuring
656656
// Obtain the updated FiberRootNode, after the target React application re-renders
657657
fiberRoot = args[1];
658-
console.log('onCommit', { fiberRoot });
659658
// If the target React application is visible, send a request to update the snapShot tree displayed on Chrome Extension
660659
if (isVisible) throttledUpdateSnapshot(fiberRoot);
661660
// After our added work is completed we invoke the original onComitFiberRoot function

src/backend/masterState.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default {
2727
},
2828
// adds new component to ComponentActionsRecord
2929
saveNew: (state, component): number => {
30+
console.log('masterState', { component, componentActionsRecord });
3031
componentActionsRecord[index] = { state, component };
3132
index++;
3233

@@ -35,7 +36,7 @@ export default {
3536
getRecordByIndex: (inputIndex: number): HookStateItem => componentActionsRecord[inputIndex],
3637
// this is used for class components -
3738
/* inputIndex will always be a fixed number (coming in timeJump.ts) */
38-
getComponentByIndex: (inputIndex: number): any =>
39+
getComponentByIndex: (inputIndex: number): HookStateItem['component'] | undefined =>
3940
componentActionsRecord[inputIndex] ? componentActionsRecord[inputIndex].component : undefined,
4041
// this is used for react hooks - hooks will be passed in as an array from timeJump.ts
4142
getComponentByIndexHooks: (inputIndex: Array<number> = []): Array<any> => {

src/backend/routes.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class Routes {
5454
// If the last visited index is not the last position in routeHistory stack. This happens when user use the timeJump functionality.
5555
// => Rebuild the browserHistory
5656
if (this.current !== this.routeHistory.length - 1) {
57-
console.log('Rebuild browser history');
5857
this.rebuildHistory(url);
5958
}
6059
// Create a new route instance from the passed in url.
@@ -64,8 +63,6 @@ class Routes {
6463
// Update the last visited index.
6564
this.current = this.routeHistory.length - 1;
6665
}
67-
console.log('Route History', this.routeHistory, this.current);
68-
console.log('History length', window.history.length);
6966
return route;
7067
}
7168

src/backend/snapShot.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export default function updateSnapShotTree(
2020
mode: Status,
2121
fiberRoot: FiberRoot,
2222
): void {
23-
console.log('snapShot.ts - Update');
2423
// this is the currently active root fiber(the mutable root of the tree)
2524
const { current } = fiberRoot;
2625
componentActionsRecord.clear();

src/backend/timeJump.ts

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,63 @@ const circularComponentTable = new Set();
2222
export default function timeJump(mode) {
2323
// Recursively change state of tree
2424
// Set the state of the origin tree if the component is stateful
25-
function jump(target): void {
25+
async function jump(target): Promise<void> {
26+
// ----------------
2627
if (!target) return;
27-
if (target.state === 'stateless') {
28+
// Base Case: if has visited, return
29+
if (circularComponentTable.has(target)) {
30+
return;
31+
} else circularComponentTable.add(target);
32+
// ------------------------STATELESS/ROOT COMPONENT-------------------------
33+
// Since stateless component has no data to update, continue to traverse its child nodes:
34+
if (target.state === 'stateless' || target.state === 'root') {
2835
target.children.forEach((child) => jump(child));
2936
return;
3037
}
31-
// for stateful class components
38+
// Obtain component data & its update method at the given index
3239
const component = componentActionsRecord.getComponentByIndex(target.componentData.index);
3340

41+
// ------------------------STATEFUL CLASS COMPONENT-------------------------
42+
// for stateful class components
3443
// check if it is a stateful class component
3544
// if yes, find the component by its index and assign it to a variable
3645
// call that components setState method to reset state to the state at the time of the jump snapshot
3746
if (component && component.setState) {
38-
// console.log('timeJumps', { component });
39-
component.setState(
47+
console.log('timeJump CLASS', componentActionsRecord);
48+
await component.setState(
4049
// prevState contains the states of the snapshots we are jumping FROM, not jumping TO
41-
(prevState) => {
42-
Object.keys(prevState).forEach((key) => {
43-
// 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
44-
// if (target.state[key] !== undefined) {
45-
// target.state[key] = undefined;
46-
// }
47-
});
48-
return target.state;
49-
},
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+
5061
// Iterate through new children after state has been set
5162
() => target.children.forEach((child) => jump(child)),
5263
);
64+
return;
5365
}
5466

55-
target.children.forEach((child) => {
56-
if (!circularComponentTable.has(child)) {
57-
circularComponentTable.add(child);
58-
jump(child);
59-
}
60-
});
67+
// target.children.forEach((child) => {
68+
// if (!circularComponentTable.has(child)) {
69+
// circularComponentTable.add(child);
70+
// jump(child);
71+
// }
72+
// });
6173

74+
// ----------------------STATEFUL FUNCTIONAL COMPONENT----------------------
6275
// REACT HOOKS
6376
// check if component states are set with hooks
6477
// if yes, grab all relevant components for this snapshot in numArr
6578
// call dispatch on each component passing in the corresponding currState value
6679
if (target.state && target.state.hooksState) {
6780
const currState = target.state.hooksState;
68-
const numArr: Array<number> = [];
81+
const numArr: number[] = [];
6982
let counter = 1;
7083
while (counter < currState.length + 1) {
7184
numArr.push(target.componentData.hooksIndex - currState.length + counter);
@@ -87,16 +100,18 @@ export default function timeJump(mode) {
87100
const navigating: boolean = routes.navigate(target.route);
88101
if (navigating) {
89102
addEventListener('popstate', (event) => {
90-
jump(target);
103+
jump(target).then(() => {
104+
document.body.onmouseover = () => {
105+
mode.jumping = false;
106+
};
107+
});
108+
});
109+
} else {
110+
jump(target).then(() => {
91111
document.body.onmouseover = () => {
92112
mode.jumping = false;
93113
};
94114
});
95-
} else {
96-
jump(target);
97-
document.body.onmouseover = () => {
98-
mode.jumping = false;
99-
};
100115
}
101116
};
102117
}

0 commit comments

Comments
 (0)