@@ -11,7 +11,7 @@ const { saveState } = require('./masterState'); // saves AST state as array for
11
11
12
12
module . exports = ( snap , mode ) => {
13
13
// snap is the current tree
14
- // mode is {jumping: bool, locked: bool, paused: bool}
14
+ // mode is {jumping: bool, locked: bool, paused: bool
15
15
16
16
let fiberRoot = null ;
17
17
let astHooks ;
@@ -34,7 +34,7 @@ module.exports = (snap, mode) => {
34
34
if ( component . setState . linkFiberChanged ) {
35
35
// console.log("setState has already been changed for", component);
36
36
return ;
37
- } ;
37
+ }
38
38
// make a copy of setState
39
39
const oldSetState = component . setState . bind ( component ) ;
40
40
// replace component's setState so developer doesn't change syntax
@@ -46,27 +46,31 @@ module.exports = (snap, mode) => {
46
46
// continue normal setState functionality, except add sending message (to chrome extension) middleware
47
47
oldSetState ( state , ( ) => {
48
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)
49
+ sendSnapshot ( ) ; // runs once on page load, after event listener: message (line 145)
50
50
callback . bind ( component ) ( ) ; // WHY DO WE NEED THIS ?
51
51
} ) ;
52
52
} ;
53
53
component . setState . linkFiberChanged = true ; // we changed setState.
54
54
}
55
55
56
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
57
+ // check that changeUseState hasn't been changed yet
58
58
if ( component . queue . dispatch . linkFiberChanged ) return ;
59
59
// store the original dispatch function definition
60
- const oldDispatch = component . queue . dispatch . bind ( component . queue ) ;
60
+
61
+ // not sure why we need the bind, seems to work without it
62
+ // const oldDispatch = component.queue.dispatch.bind(component.queue);
63
+ const oldDispatch = component . queue . dispatch ;
64
+
61
65
// redefine the dispatch function so we can inject our code
62
66
component . queue . dispatch = ( fiber , queue , action ) => {
63
67
// don't do anything if state is locked, UNLESS we are currently jumping through time
64
68
if ( mode . locked && ! mode . jumping ) return ;
65
- oldDispatch ( fiber , queue , action ) ;
66
- setTimeout ( ( ) => {
67
- updateSnapShotTree ( ) ;
68
- sendSnapshot ( ) ;
69
- } , 100 ) ;
69
+ oldDispatch ( fiber , queue , action ) ; // hooks sees this and thinks its a side effect, that's why it's throwing an error
70
+ // setTimeout(() => {
71
+ updateSnapShotTree ( ) ;
72
+ sendSnapshot ( ) ;
73
+ // }, 100);
70
74
} ;
71
75
component . queue . dispatch . linkFiberChanged = true ;
72
76
}
@@ -79,8 +83,13 @@ module.exports = (snap, mode) => {
79
83
let index = 0 ;
80
84
astHooks = Object . values ( astHooks ) ;
81
85
// while memoizedState is truthy, save the value to the object
82
- while ( memoizedState ) {
83
- changeUseState ( memoizedState ) ;
86
+ while ( memoizedState && memoizedState . queue !== null ) {
87
+ // we only want to changeUseState (which updates and sends the snapshot)
88
+ // on the last item in the memoizedState chain. This makes sure it doesn't double-push
89
+ // values to the timeline.
90
+ if ( memoizedState . next === null ) {
91
+ changeUseState ( memoizedState ) ;
92
+ }
84
93
// memoized[astHooks[index]] = memoizedState.memoizedState;
85
94
memoized [ astHooks [ index ] ] = memoizedState . memoizedState ;
86
95
// Reassign memoizedState to its next value
@@ -103,7 +112,7 @@ module.exports = (snap, mode) => {
103
112
elementType,
104
113
} = currentFiber ; // extract properties of current fiber
105
114
106
- let childTree = tree ; // initialize child fiber tree as current fiber tree
115
+ let childTree = tree ; // initialize child fiber tree as current fiber tree
107
116
// check if stateful component
108
117
if ( stateNode && stateNode . state ) {
109
118
// add component to tree
@@ -112,6 +121,7 @@ module.exports = (snap, mode) => {
112
121
changeSetState ( stateNode ) ;
113
122
}
114
123
// Check if the component uses hooks
124
+
115
125
if ( memoizedState && Object . hasOwnProperty . call ( memoizedState , 'baseState' ) ) {
116
126
// Traverse through the currentFiber and extract the getters/setters
117
127
astHooks = astParser ( elementType ) ;
@@ -133,7 +143,7 @@ module.exports = (snap, mode) => {
133
143
// but skips 1st hook click
134
144
function updateSnapShotTree ( ) {
135
145
const { current } = fiberRoot ; // on initial page load, current - fiberNode is tag type HostRoot (entire fiber tree)
136
- console . log ( " current" , current ) ;
146
+ console . log ( ' current' , current ) ;
137
147
snap . tree = createTree ( current ) ;
138
148
}
139
149
@@ -154,8 +164,9 @@ module.exports = (snap, mode) => {
154
164
window . addEventListener ( 'message' , ( { data : { action } } ) => {
155
165
if ( action === 'contentScriptStarted' ) { // runs once on initial page load
156
166
// console.log("in window.addEL")
157
- sendSnapshot ( )
158
- } ;
167
+ console . log ( 'page running' ) ;
168
+ sendSnapshot ( ) ;
169
+ }
159
170
} ) ;
160
171
} ;
161
172
} ;
0 commit comments