1
1
/* eslint-disable func-names */
2
2
/* eslint-disable no-use-before-define */
3
3
/* eslint-disable no-param-reassign */
4
+
4
5
// links component state tree to library
5
6
// changes the setState method to also update our snapshot
7
+
6
8
const Tree = require ( './tree' ) ;
7
9
const astParser = require ( './astParser' ) ;
8
- const { saveState } = require ( './masterState' ) ;
10
+ const { saveState } = require ( './masterState' ) ; // saves AST state as array for later use
9
11
10
12
module . exports = ( snap , mode ) => {
13
+ // snap is the current tree
14
+ // mode is {jumping: bool, locked: bool, paused: bool}
15
+
11
16
let fiberRoot = null ;
12
17
let astHooks ;
13
18
14
- function sendSnapshot ( ) {
19
+ function sendSnapshot ( ) { // send snapshot of current fiber tree to chrome extension ?
15
20
// don't send messages while jumping or while paused
16
- // DEV: So that when we are jumping to an old snapshot it
17
- // wouldn't think we want to create new snapshots
21
+ // DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
18
22
if ( mode . jumping || mode . paused ) return ;
19
- const payload = snap . tree . getCopy ( ) ;
23
+ const payload = snap . tree . getCopy ( ) ; // copy of current react fiber tree
20
24
// console.log('payload', payload);
21
- window . postMessage ( {
25
+ window . postMessage ( { // send to window
22
26
action : 'recordSnap' ,
23
27
payload,
24
28
} ) ;
25
29
}
26
30
27
- function changeSetState ( component ) {
31
+ function changeSetState ( component ) { // if invoked, change setState functionality so that it also updates our snapshot
32
+ // console.log("what is component?", component);
28
33
// check that setState hasn't been changed yet
29
- if ( component . setState . linkFiberChanged ) return ;
34
+ if ( component . setState . linkFiberChanged ) {
35
+ // console.log("setState has already been changed for", component);
36
+ return ;
37
+ } ;
30
38
// make a copy of setState
31
39
const oldSetState = component . setState . bind ( component ) ;
32
40
// replace component's setState so developer doesn't change syntax
@@ -35,23 +43,24 @@ module.exports = (snap, mode) => {
35
43
// don't do anything if state is locked
36
44
// UNLESS we are currently jumping through time
37
45
if ( mode . locked && ! mode . jumping ) return ;
38
- // continue normal setState functionality, except add sending message middleware
46
+ // continue normal setState functionality, except add sending message (to chrome extension) middleware
39
47
oldSetState ( state , ( ) => {
40
- updateSnapShotTree ( ) ;
41
- sendSnapshot ( ) ;
42
- callback . bind ( component ) ( ) ;
48
+ updateSnapShotTree ( ) ; // this doubles the actions in reactime for star wars app, also invokes changeSetState twice, also invokes changeSetState with Route and Characters
49
+ sendSnapshot ( ) ; //runs once on page load, after event listener: message (line 145)
50
+ callback . bind ( component ) ( ) ; // WHY DO WE NEED THIS ?
43
51
} ) ;
44
52
} ;
45
- component . setState . linkFiberChanged = true ;
53
+ component . setState . linkFiberChanged = true ; // we changed setState.
46
54
}
47
55
48
- function changeUseState ( component ) {
56
+ function changeUseState ( component ) { // if invoked, change useState dispatch functionality so that it also updates our snapshot
57
+ //check that changeUseState hasn't been changed yet
49
58
if ( component . queue . dispatch . linkFiberChanged ) return ;
50
59
// store the original dispatch function definition
51
60
const oldDispatch = component . queue . dispatch . bind ( component . queue ) ;
52
61
// redefine the dispatch function so we can inject our code
53
62
component . queue . dispatch = ( fiber , queue , action ) => {
54
- // don't do anything if state is locked
63
+ // don't do anything if state is locked, UNLESS we are currently jumping through time
55
64
if ( mode . locked && ! mode . jumping ) return ;
56
65
oldDispatch ( fiber , queue , action ) ;
57
66
setTimeout ( ( ) => {
@@ -83,21 +92,22 @@ module.exports = (snap, mode) => {
83
92
}
84
93
85
94
function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
95
+ // if there is no current fiber just return the new tree as-is
86
96
if ( ! currentFiber ) return tree ;
87
-
97
+ // console.log("what is currentFiber", currentFiber);
88
98
const {
89
99
sibling,
90
100
stateNode,
91
101
child,
92
102
memoizedState,
93
103
elementType,
94
- } = currentFiber ;
104
+ } = currentFiber ; // extract properties of current fiber
95
105
96
- let nextTree = tree ;
106
+ let childTree = tree ; // initialize child fiber tree as current fiber tree
97
107
// check if stateful component
98
108
if ( stateNode && stateNode . state ) {
99
109
// add component to tree
100
- nextTree = tree . appendChild ( stateNode ) ;
110
+ childTree = tree . appendChild ( stateNode ) ; // returns newly appended tree
101
111
// change setState functionality
102
112
changeSetState ( stateNode ) ;
103
113
}
@@ -109,34 +119,43 @@ module.exports = (snap, mode) => {
109
119
// Create a traversed property and assign to the evaluated result of
110
120
// invoking traverseHooks with memoizedState
111
121
memoizedState . traversed = traverseHooks ( memoizedState ) ;
112
- nextTree = tree . appendChild ( memoizedState ) ;
122
+ childTree = tree . appendChild ( memoizedState ) ;
113
123
}
114
124
// iterate through siblings
115
125
createTree ( sibling , tree ) ;
116
126
// iterate through children
117
- createTree ( child , nextTree ) ;
127
+ createTree ( child , childTree ) ;
118
128
119
129
return tree ;
120
130
}
121
- // runs when page initially loads
131
+
132
+ // runs when page initially loads and on subsequent state changes
122
133
// but skips 1st hook click
123
134
function updateSnapShotTree ( ) {
124
- const { current } = fiberRoot ;
135
+ const { current } = fiberRoot ; // on initial page load, current - fiberNode is tag type HostRoot (entire fiber tree)
136
+ console . log ( "current" , current ) ;
125
137
snap . tree = createTree ( current ) ;
126
138
}
127
139
140
+ // RUNS ONCE, ON INITIAL PAGE LOAD ?
128
141
return container => {
142
+ // on first page load, container is entire html hierarchy of top level div
143
+ // _reactRootContainer is that invisible top level object which wraps the top level div
144
+ // _reactRootContainer._internalRoot is an object with property .current which includes HostRoot fiberNode (entire fiber tree)
129
145
const {
130
146
_reactRootContainer : { _internalRoot } ,
131
147
_reactRootContainer,
132
148
} = container ;
133
- // only assign internal rootp if it actually exists
149
+ // only assign internal root if it actually exists
134
150
fiberRoot = _internalRoot || _reactRootContainer ;
135
151
136
152
updateSnapShotTree ( ) ;
137
153
// send the initial snapshot once the content script has started up
138
154
window . addEventListener ( 'message' , ( { data : { action } } ) => {
139
- if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
155
+ if ( action === 'contentScriptStarted' ) { // runs once on initial page load
156
+ // console.log("in window.addEL")
157
+ sendSnapshot ( )
158
+ } ;
140
159
} ) ;
141
160
} ;
142
161
} ;
0 commit comments