Skip to content

Commit ba5f8d8

Browse files
committed
timeJump comments
1 parent b5405b2 commit ba5f8d8

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

package/timeJump.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
/**
2+
* This file contains necessary functionality for time-travel feature
3+
*
4+
* It exports an anonymous
5+
* @function
6+
* that is invoked on
7+
* @param target --> a target snapshot portraying some past state
8+
* and recursively sets state for any stateful components.
9+
*
10+
*/
11+
112
/* eslint-disable no-param-reassign */
2-
// traverses given tree by accessing children through coords array
13+
314
const { returnState } = require('./masterState');
415

16+
// Traverses given tree by accessing children through coords array
517
function traverseTree(tree, coords) {
618
let curr = tree;
719
coords.forEach(coord => {
@@ -11,30 +23,33 @@ function traverseTree(tree, coords) {
1123
}
1224

1325
module.exports = (origin, mode) => {
14-
// recursively change state of tree
26+
// Recursively change state of tree
1527
function jump(target, coords = []) {
1628
const originNode = traverseTree(origin.tree, coords);
17-
// set the state of the origin tree if the component is stateful
29+
30+
// Set the state of the origin tree if the component is stateful
1831
if (originNode.component.setState) {
19-
originNode.component.setState((prevState) => {
20-
Object.keys(prevState).forEach((key) => {
32+
// * Use the function argument when setting state to account for any state properties
33+
// * that may not have existed in the past
34+
originNode.component.setState(prevState => {
35+
Object.keys(prevState).forEach(key => {
2136
if (target.state[key] === undefined) {
2237
target.state[key] = undefined;
2338
}
24-
})
39+
});
2540
return target.state;
2641
}, () => {
27-
// iterate through new children once state has been set
42+
// Iterate through new children once state has been set
2843
target.children.forEach((child, i) => {
2944
jump(child, coords.concat(i));
3045
});
3146
});
3247
} else {
33-
// if component uses hooks, traverse through the memoize tree
48+
// If component uses hooks, traverse through the memoize tree
3449
let current = originNode.component;
3550
let index = 0;
3651
const hooks = returnState();
37-
// while loop through the memoize tree
52+
// While loop through the memoize tree
3853
while (current && current.queue) { // allows time travel with useEffect
3954
current.queue.dispatch(target.state[hooks[index]]);
4055
// Reassign the current value
@@ -45,7 +60,7 @@ module.exports = (origin, mode) => {
4560
}
4661

4762
return target => {
48-
// setting mode disables setState from posting messages to window
63+
// * Setting mode disables setState from posting messages to window
4964
mode.jumping = true;
5065
jump(target);
5166
setTimeout(() => {

0 commit comments

Comments
 (0)