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