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
+
1
12
/* eslint-disable no-param-reassign */
2
- // traverses given tree by accessing children through coords array
13
+
3
14
const { returnState } = require ( './masterState' ) ;
4
15
16
+ // Traverses given tree by accessing children through coords array
5
17
function traverseTree ( tree , coords ) {
6
18
let curr = tree ;
7
19
coords . forEach ( coord => {
@@ -11,30 +23,33 @@ function traverseTree(tree, coords) {
11
23
}
12
24
13
25
module . exports = ( origin , mode ) => {
14
- // recursively change state of tree
26
+ // Recursively change state of tree
15
27
function jump ( target , coords = [ ] ) {
16
28
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
18
31
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 => {
21
36
if ( target . state [ key ] === undefined ) {
22
37
target . state [ key ] = undefined ;
23
38
}
24
- } )
39
+ } ) ;
25
40
return target . state ;
26
41
} , ( ) => {
27
- // iterate through new children once state has been set
42
+ // Iterate through new children once state has been set
28
43
target . children . forEach ( ( child , i ) => {
29
44
jump ( child , coords . concat ( i ) ) ;
30
45
} ) ;
31
46
} ) ;
32
47
} else {
33
- // if component uses hooks, traverse through the memoize tree
48
+ // If component uses hooks, traverse through the memoize tree
34
49
let current = originNode . component ;
35
50
let index = 0 ;
36
51
const hooks = returnState ( ) ;
37
- // while loop through the memoize tree
52
+ // While loop through the memoize tree
38
53
while ( current && current . queue ) { // allows time travel with useEffect
39
54
current . queue . dispatch ( target . state [ hooks [ index ] ] ) ;
40
55
// Reassign the current value
@@ -45,7 +60,7 @@ module.exports = (origin, mode) => {
45
60
}
46
61
47
62
return target => {
48
- // setting mode disables setState from posting messages to window
63
+ // * Setting mode disables setState from posting messages to window
49
64
mode . jumping = true ;
50
65
jump ( target ) ;
51
66
setTimeout ( ( ) => {
0 commit comments