1+ /* eslint-disable no-underscore-dangle */
12/* eslint-disable func-names */
23/* eslint-disable no-use-before-define */
34/* eslint-disable no-param-reassign */
@@ -10,6 +11,7 @@ const { saveState } = require('./masterState');
1011module . exports = ( snap , mode ) => {
1112 let fiberRoot = null ;
1213 let astHooks ;
14+ let concurrent = false ; // flag to check if we are in concurrent mode
1315
1416 function sendSnapshot ( ) {
1517 // don't send messages while jumping or while paused
@@ -70,20 +72,24 @@ module.exports = (snap, mode) => {
7072 let index = 0 ;
7173 astHooks = Object . values ( astHooks ) ;
7274 // 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
7477 changeUseState ( memoizedState ) ;
75-
78+ // }
79+ // memoized[astHooks[index]] = memoizedState.memoizedState;
7680 memoized [ astHooks [ index ] ] = memoizedState . memoizedState ;
7781 // Reassign memoizedState to its next value
7882 memoizedState = memoizedState . next ;
79- index += 2 ; // Increment the index by 2
83+ // Increment the index by 2
84+ index += 2 ;
8085 }
8186 return memoized ;
8287 }
8388
8489 function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
8590 if ( ! currentFiber ) return tree ;
8691
92+
8793 const {
8894 sibling,
8995 stateNode,
@@ -101,10 +107,11 @@ module.exports = (snap, mode) => {
101107 changeSetState ( stateNode ) ;
102108 }
103109 // 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 ;
108115 // Traverse through the currentFiber and extract the getters/setters
109116 astHooks = astParser ( elementType ) ;
110117 saveState ( astHooks ) ;
@@ -122,20 +129,39 @@ module.exports = (snap, mode) => {
122129 }
123130 // runs when page initially loads
124131 // 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+
127148 snap . tree = createTree ( current ) ;
128149 }
129150
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+ }
137163
138- updateSnapShotTree ( ) ;
164+ await updateSnapShotTree ( ) ;
139165 // send the initial snapshot once the content script has started up
140166 window . addEventListener ( 'message' , ( { data : { action } } ) => {
141167 if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
0 commit comments