4
4
// links component state tree to library
5
5
// changes the setState method to also update our snapshot
6
6
const Tree = require ( './tree' ) ;
7
+ const astParser = require ( './astParser.js' ) ;
7
8
8
9
module . exports = ( snap , mode ) => {
9
10
let fiberRoot = null ;
11
+ let astHooks ;
10
12
11
13
function sendSnapshot ( ) {
12
14
// don't send messages while jumping or while paused
@@ -23,14 +25,12 @@ module.exports = (snap, mode) => {
23
25
function changeSetState ( component ) {
24
26
// check that setState hasn't been changed yet
25
27
if ( component . setState . linkFiberChanged ) return ;
26
-
27
28
// make a copy of setState
28
29
const oldSetState = component . setState . bind ( component ) ;
29
-
30
30
// replace component's setState so developer doesn't change syntax
31
31
// component.setState = newSetState.bind(component);
32
- component . setState = ( state , callback = ( ) => { } ) => {
33
- // dont do anything if state is locked
32
+ component . setState = ( state , callback = ( ) => { } ) => {
33
+ // don't do anything if state is locked
34
34
// UNLESS we are currently jumping through time
35
35
if ( mode . locked && ! mode . jumping ) return ;
36
36
// continue normal setState functionality, except add sending message middleware
@@ -43,42 +43,45 @@ module.exports = (snap, mode) => {
43
43
component . setState . linkFiberChanged = true ;
44
44
}
45
45
46
- function changeUseState ( component ) {
46
+ function changeUseState ( component ) {
47
47
if ( component . queue . dispatch . linkFiberChanged ) return ;
48
- // storing the original dispatch function definition somewhere
49
- const oldDispatch = component . queue . dispatch . bind ( component . queue ) ;
50
- // redefining the dispatch function so we can inject our code
51
- component . queue . dispatch = function ( fiber , queue , action ) {
52
- console . log ( 'mode' , mode ) ;
53
- if ( mode . locked && ! mode . jumping ) 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
54
oldDispatch ( fiber , queue , action ) ;
55
55
setTimeout ( ( ) => {
56
56
updateSnapShotTree ( ) ;
57
57
sendSnapshot ( ) ;
58
58
} , 100 ) ;
59
59
} ;
60
60
component . queue . dispatch . linkFiberChanged = true ;
61
- } ;
62
-
61
+ }
62
+
63
63
// Helper function to traverse through the memoized state
64
+ // TODO: WE NEED TO CLEAN IT UP A BIT
64
65
function traverseHooks ( memoizedState ) {
65
66
// Declare variables and assigned to 0th index and an empty object, respectively
66
- let index = 0 ;
67
- const memoizedObj = { } ;
67
+ const memoized = { } ;
68
+ let index = 0 ;
69
+ astHooks = Object . values ( astHooks ) ;
68
70
// while memoizedState is truthy, save the value to the object
69
- while ( memoizedState ) {
71
+ while ( memoizedState && astHooks ) {
70
72
changeUseState ( memoizedState ) ;
71
- // Increment the index by 1
72
- memoizedObj [ `state${ index += 1 } ` ] = memoizedState . memoizedState ;
73
+ memoized [ astHooks [ index ] ] = memoizedState . memoizedState ;
73
74
// Reassign memoizedState to its next value
74
75
memoizedState = memoizedState . next ;
76
+ // Increment the index by 2
77
+ index += 2 ;
75
78
}
76
- return memoizedObj ;
79
+ return memoized ;
77
80
}
78
81
79
82
function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
80
83
if ( ! currentFiber ) return tree ;
81
-
84
+
82
85
const {
83
86
sibling,
84
87
stateNode,
@@ -95,10 +98,9 @@ module.exports = (snap, mode) => {
95
98
changeSetState ( stateNode ) ;
96
99
}
97
100
// Check if the component uses hooks
98
- // TODO: Refactor the conditionals - think about the edge case where a stateful
99
- // component might have a key called 'baseState' in the state
100
101
if ( memoizedState && memoizedState . hasOwnProperty ( 'baseState' ) ) {
101
- console . log ( "I'm not supposed to run" , currentFiber ) ;
102
+ // Add a traversed property and initialize to the evaluated result
103
+ // of invoking traverseHooks, and reassign nextTree
102
104
memoizedState . traversed = traverseHooks ( memoizedState ) ;
103
105
nextTree = tree . appendChild ( memoizedState ) ;
104
106
}
@@ -115,19 +117,20 @@ module.exports = (snap, mode) => {
115
117
snap . tree = createTree ( current ) ;
116
118
}
117
119
118
- return {
119
- _ ( container ) {
120
- const {
121
- _reactRootContainer : { _internalRoot } ,
122
- _reactRootContainer,
123
- } = container ;
124
- // only assign internal root if it actually exists
125
- fiberRoot = _internalRoot || _reactRootContainer ;
126
- updateSnapShotTree ( ) ;
127
- // send the initial snapshot once the content script has started up
128
- window . addEventListener ( 'message' , ( { data : { action } } ) => {
129
- if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
130
- } ) ;
131
- } ,
132
- } ;
120
+ return ( container , entryFile ) => {
121
+ const {
122
+ _reactRootContainer : { _internalRoot } ,
123
+ _reactRootContainer,
124
+ } = container ;
125
+ // only assign internal rootp if it actually exists
126
+ fiberRoot = _internalRoot || _reactRootContainer ;
127
+ // If hooks are implemented, traverse through the source code
128
+ if ( entryFile ) astHooks = astParser ( entryFile ) ;
129
+
130
+ updateSnapShotTree ( ) ;
131
+ // send the initial snapshot once the content script has started up
132
+ window . addEventListener ( 'message' , ( { data : { action } } ) => {
133
+ if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
134
+ } ) ;
135
+ }
133
136
} ;
0 commit comments