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+
314const { returnState } = require ( './masterState' ) ;
415
16+ // Traverses given tree by accessing children through coords array
517function traverseTree ( tree , coords ) {
618 let curr = tree ;
719 coords . forEach ( coord => {
@@ -11,30 +23,33 @@ function traverseTree(tree, coords) {
1123}
1224
1325module . 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