1
+ /* eslint-disable no-underscore-dangle */
1
2
/* eslint-disable func-names */
2
3
/* eslint-disable no-use-before-define */
3
4
/* eslint-disable no-param-reassign */
@@ -10,6 +11,7 @@ const { saveState } = require('./masterState');
10
11
module . exports = ( snap , mode ) => {
11
12
let fiberRoot = null ;
12
13
let astHooks ;
14
+ let concurrent = false ; // flag to check if we are in concurrent mode
13
15
14
16
function sendSnapshot ( ) {
15
17
// don't send messages while jumping or while paused
@@ -70,20 +72,24 @@ module.exports = (snap, mode) => {
70
72
let index = 0 ;
71
73
astHooks = Object . values ( astHooks ) ;
72
74
// while memoizedState is truthy, save the value to the object
73
- while ( memoizedState && memoizedState . queue ) {
75
+ while ( memoizedState && memoizedState . queue ) { // prevents useEffect from crashing on load
76
+ // if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
74
77
changeUseState ( memoizedState ) ;
75
-
78
+ // }
79
+ // memoized[astHooks[index]] = memoizedState.memoizedState;
76
80
memoized [ astHooks [ index ] ] = memoizedState . memoizedState ;
77
81
// Reassign memoizedState to its next value
78
82
memoizedState = memoizedState . next ;
79
- index += 2 ; // Increment the index by 2
83
+ // Increment the index by 2
84
+ index += 2 ;
80
85
}
81
86
return memoized ;
82
87
}
83
88
84
89
function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
85
90
if ( ! currentFiber ) return tree ;
86
91
92
+
87
93
const {
88
94
sibling,
89
95
stateNode,
@@ -101,10 +107,11 @@ module.exports = (snap, mode) => {
101
107
changeSetState ( stateNode ) ;
102
108
}
103
109
// Check if the component uses hooks
104
- if (
105
- memoizedState
106
- && Object . hasOwnProperty . call ( memoizedState , 'baseState' )
107
- ) {
110
+ // console.log("memoizedState", memoizedState);
111
+
112
+ if ( memoizedState && Object . hasOwnProperty . call ( memoizedState , 'baseState' ) ) {
113
+ // 'catch-all' for suspense elements (experimental)
114
+ if ( typeof elementType . $$typeof === 'symbol' ) return ;
108
115
// Traverse through the currentFiber and extract the getters/setters
109
116
astHooks = astParser ( elementType ) ;
110
117
saveState ( astHooks ) ;
@@ -122,20 +129,39 @@ module.exports = (snap, mode) => {
122
129
}
123
130
// runs when page initially loads
124
131
// but skips 1st hook click
125
- function updateSnapShotTree ( ) {
126
- const { current } = fiberRoot ;
132
+ async function updateSnapShotTree ( ) {
133
+ let current ;
134
+ // if concurrent mode, grab current.child'
135
+ if ( concurrent ) {
136
+ // we need a way to wait for current child to populate
137
+ const promise = new Promise ( ( resolve , reject ) => {
138
+ setTimeout ( ( ) => resolve ( fiberRoot . current . child ) , 400 ) ;
139
+ } ) ;
140
+
141
+ current = await promise ;
142
+
143
+ current = fiberRoot . current . child ;
144
+ } else {
145
+ current = fiberRoot . current ;
146
+ }
147
+
127
148
snap . tree = createTree ( current ) ;
128
149
}
129
150
130
- return container => {
131
- const {
132
- _reactRootContainer : { _internalRoot } ,
133
- _reactRootContainer,
134
- } = container ;
135
- // only assign internal rootp if it actually exists
136
- fiberRoot = _internalRoot || _reactRootContainer ;
151
+ return async container => {
152
+ if ( container . _internalRoot ) {
153
+ fiberRoot = container . _internalRoot ;
154
+ concurrent = true ;
155
+ } else {
156
+ const {
157
+ _reactRootContainer : { _internalRoot } ,
158
+ _reactRootContainer,
159
+ } = container ;
160
+ // only assign internal rootp if it actually exists
161
+ fiberRoot = _internalRoot || _reactRootContainer ;
162
+ }
137
163
138
- updateSnapShotTree ( ) ;
164
+ await updateSnapShotTree ( ) ;
139
165
// send the initial snapshot once the content script has started up
140
166
window . addEventListener ( 'message' , ( { data : { action } } ) => {
141
167
if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
0 commit comments