1+ /* eslint-disable func-names */
12/* eslint-disable no-use-before-define */
23/* eslint-disable no-param-reassign */
34// links component state tree to library
45// changes the setState method to also update our snapshot
56const Tree = require ( './tree' ) ;
7+ const astParser = require ( './astParser.js' ) ;
68
79module . exports = ( snap , mode ) => {
810 let fiberRoot = null ;
11+ let astHooks ;
912
1013 function sendSnapshot ( ) {
1114 // don't send messages while jumping or while paused
15+ // DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
1216 if ( mode . jumping || mode . paused ) return ;
1317 const payload = snap . tree . getCopy ( ) ;
18+ // console.log('payload', payload);
1419 window . postMessage ( {
1520 action : 'recordSnap' ,
1621 payload,
@@ -20,14 +25,12 @@ module.exports = (snap, mode) => {
2025 function changeSetState ( component ) {
2126 // check that setState hasn't been changed yet
2227 if ( component . setState . linkFiberChanged ) return ;
23-
2428 // make a copy of setState
2529 const oldSetState = component . setState . bind ( component ) ;
26-
2730 // replace component's setState so developer doesn't change syntax
2831 // component.setState = newSetState.bind(component);
29- component . setState = ( state , callback = ( ) => { } ) => {
30- // dont do anything if state is locked
32+ component . setState = ( state , callback = ( ) => { } ) => {
33+ // don't do anything if state is locked
3134 // UNLESS we are currently jumping through time
3235 if ( mode . locked && ! mode . jumping ) return ;
3336 // continue normal setState functionality, except add sending message middleware
@@ -40,10 +43,51 @@ module.exports = (snap, mode) => {
4043 component . setState . linkFiberChanged = true ;
4144 }
4245
46+ function changeUseState ( component ) {
47+ if ( component . queue . dispatch . linkFiberChanged ) return ;
48+ // store the original dispatch function definition
49+ const oldDispatch = component . queue . dispatch . bind ( component . queue ) ; ;
50+ // redefine the dispatch function so we can inject our code
51+ component . queue . dispatch = ( fiber , queue , action ) => {
52+ // don't do anything if state is locked
53+ if ( mode . locked && ! mode . jumping ) return ;
54+ oldDispatch ( fiber , queue , action ) ;
55+ setTimeout ( ( ) => {
56+ updateSnapShotTree ( ) ;
57+ sendSnapshot ( ) ;
58+ } , 100 ) ;
59+ } ;
60+ component . queue . dispatch . linkFiberChanged = true ;
61+ }
62+
63+ // Helper function to traverse through the memoized state
64+ // TODO: WE NEED TO CLEAN IT UP A BIT
65+ function traverseHooks ( memoizedState ) {
66+ // Declare variables and assigned to 0th index and an empty object, respectively
67+ const memoized = { } ;
68+ let index = 0 ;
69+ astHooks = Object . values ( astHooks ) ;
70+ // while memoizedState is truthy, save the value to the object
71+ while ( memoizedState && astHooks ) {
72+ changeUseState ( memoizedState ) ;
73+ memoized [ astHooks [ index ] ] = memoizedState . memoizedState ;
74+ // Reassign memoizedState to its next value
75+ memoizedState = memoizedState . next ;
76+ // Increment the index by 2
77+ index += 2 ;
78+ }
79+ return memoized ;
80+ }
81+
4382 function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
4483 if ( ! currentFiber ) return tree ;
4584
46- const { sibling, stateNode, child } = currentFiber ;
85+ const {
86+ sibling,
87+ stateNode,
88+ child,
89+ memoizedState,
90+ } = currentFiber ;
4791
4892 let nextTree = tree ;
4993 // check if stateful component
@@ -53,7 +97,13 @@ module.exports = (snap, mode) => {
5397 // change setState functionality
5498 changeSetState ( stateNode ) ;
5599 }
56-
100+ // Check if the component uses hooks
101+ if ( memoizedState && memoizedState . hasOwnProperty ( 'baseState' ) ) {
102+ // Add a traversed property and initialize to the evaluated result
103+ // of invoking traverseHooks, and reassign nextTree
104+ memoizedState . traversed = traverseHooks ( memoizedState ) ;
105+ nextTree = tree . appendChild ( memoizedState ) ;
106+ }
57107 // iterate through siblings
58108 createTree ( sibling , tree ) ;
59109 // iterate through children
@@ -67,18 +117,20 @@ module.exports = (snap, mode) => {
67117 snap . tree = createTree ( current ) ;
68118 }
69119
70- return container => {
120+ return ( container , entryFile ) => {
71121 const {
72122 _reactRootContainer : { _internalRoot } ,
73123 _reactRootContainer,
74124 } = container ;
75- // only assign internal root if it actually exists
125+ // only assign internal rootp if it actually exists
76126 fiberRoot = _internalRoot || _reactRootContainer ;
77- updateSnapShotTree ( ) ;
127+ // If hooks are implemented, traverse through the source code
128+ if ( entryFile ) astHooks = astParser ( entryFile ) ;
78129
130+ updateSnapShotTree ( ) ;
79131 // send the initial snapshot once the content script has started up
80132 window . addEventListener ( 'message' , ( { data : { action } } ) => {
81133 if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
82134 } ) ;
83- } ;
135+ }
84136} ;
0 commit comments