1
+ /* eslint-disable func-names */
1
2
/* eslint-disable no-use-before-define */
2
3
/* eslint-disable no-param-reassign */
3
4
// links component state tree to library
@@ -9,14 +10,17 @@ module.exports = (snap, mode) => {
9
10
10
11
function sendSnapshot ( ) {
11
12
// don't send messages while jumping or while paused
13
+ // DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
12
14
if ( mode . jumping || mode . paused ) return ;
13
15
const payload = snap . tree . getCopy ( ) ;
16
+ // console.log('payload', payload);
14
17
window . postMessage ( {
15
18
action : 'recordSnap' ,
16
19
payload,
17
20
} ) ;
18
21
}
19
22
23
+ // DEV: This is how we know when a change has happened (by injecting an event listener to every component's setState functionality). Will need to create a separate one for useState components
20
24
function changeSetState ( component ) {
21
25
// check that setState hasn't been changed yet
22
26
if ( component . setState . linkFiberChanged ) return ;
@@ -40,11 +44,31 @@ module.exports = (snap, mode) => {
40
44
component . setState . linkFiberChanged = true ;
41
45
}
42
46
47
+ // Helper function to traverse through the memoized state
48
+ function traverseHooks ( memoizedState ) {
49
+ // Declare variables and assigned to 0th index and an empty object, respectively
50
+ let index = 0 ;
51
+ const memoizedObj = { } ;
52
+ // while memoizedState is truthy, save the value to the object
53
+ while ( memoizedState ) {
54
+ // Increment the index by 1
55
+ memoizedObj [ `useState${ index += 1 } ` ] = memoizedState . memoizedState ;
56
+ // Reassign memoizedState to its next value
57
+ memoizedState = memoizedState . next ;
58
+ }
59
+ return memoizedObj ;
60
+ }
61
+
43
62
function createTree ( currentFiber , tree = new Tree ( 'root' ) ) {
44
63
if ( ! currentFiber ) return tree ;
45
64
// We have to figure out which properties to destructure from currentFiber
46
- // To support hooks and Context API
47
- const { sibling, stateNode, child } = currentFiber ;
65
+ // To support hooks and Context API
66
+ const {
67
+ sibling,
68
+ stateNode,
69
+ child,
70
+ memoizedState,
71
+ } = currentFiber ;
48
72
49
73
let nextTree = tree ;
50
74
// check if stateful component
@@ -54,8 +78,16 @@ module.exports = (snap, mode) => {
54
78
// change setState functionality
55
79
changeSetState ( stateNode ) ;
56
80
}
57
- // Need to check if functional component AND uses hooks
58
-
81
+ // Check if the component uses hooks
82
+ // TODO: Refactor the conditionals - think about the edge case where a stateful
83
+ // component might have a key called 'baseState' in the state
84
+ if ( memoizedState && memoizedState . hasOwnProperty ( 'baseState' ) ) {
85
+ // console.log('The memoizedState is: ', memoizedState);
86
+ const result = traverseHooks ( memoizedState ) ;
87
+ console . log ( 'This is the result of calling traverseHooks:' , result ) ;
88
+ nextTree = tree . appendChild ( result ) ;
89
+ }
90
+
59
91
// iterate through siblings
60
92
createTree ( sibling , tree ) ;
61
93
// iterate through children
@@ -65,22 +97,61 @@ module.exports = (snap, mode) => {
65
97
}
66
98
67
99
function updateSnapShotTree ( ) {
100
+ // console.log('fiberRoot', fiberRoot);
68
101
const { current } = fiberRoot ;
102
+ console . log ( 'current' , current ) ;
69
103
snap . tree = createTree ( current ) ;
70
104
}
71
105
72
- return container => {
73
- const {
74
- _reactRootContainer : { _internalRoot } ,
75
- _reactRootContainer,
76
- } = container ;
77
- // only assign internal root if it actually exists
78
- fiberRoot = _internalRoot || _reactRootContainer ;
79
- updateSnapShotTree ( ) ;
106
+ // return container => {
107
+ // // console.log('Container', container);
108
+ // const {
109
+ // _reactRootContainer: { _internalRoot },
110
+ // _reactRootContainer,
111
+ // } = container;
112
+ // // console.log('Root container', _reactRootContainer);
113
+ // // only assign internal root if it actually exists
114
+ // fiberRoot = _internalRoot || _reactRootContainer;
115
+ // updateSnapShotTree();
80
116
81
- // send the initial snapshot once the content script has started up
82
- window . addEventListener ( 'message' , ( { data : { action } } ) => {
83
- if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
84
- } ) ;
117
+ // // send the initial snapshot once the content script has started up
118
+ // window.addEventListener('message', ({ data: { action } }) => {
119
+ // if (action === 'contentScriptStarted') sendSnapshot();
120
+ // });
121
+ // };
122
+
123
+ return {
124
+ main ( container ) {
125
+ // console.log('Container', container);
126
+ const {
127
+ _reactRootContainer : { _internalRoot } ,
128
+ _reactRootContainer,
129
+ } = container ;
130
+ // console.log('Root container', _reactRootContainer);
131
+ // only assign internal root if it actually exists
132
+ fiberRoot = _internalRoot || _reactRootContainer ;
133
+ updateSnapShotTree ( ) ;
134
+
135
+ // send the initial snapshot once the content script has started up
136
+ window . addEventListener ( 'message' , ( { data : { action } } ) => {
137
+ if ( action === 'contentScriptStarted' ) sendSnapshot ( ) ;
138
+ } ) ;
139
+ } ,
140
+ changeUseState ( useState ) {
141
+ return function ( initial ) {
142
+ // running the original useState and storing its result (state and dispatch function)
143
+ const toReturn = useState ( initial ) ;
144
+ console . log ( toReturn ) ;
145
+ // storing the original dispatch function definition somewhere
146
+ const oldDispatch = toReturn [ 1 ] ;
147
+ // redefining the dispatch function so we can inject our code
148
+ toReturn [ 1 ] = function ( callback ) {
149
+ oldDispatch ( callback ) ;
150
+ updateSnapShotTree ( ) ;
151
+ sendSnapshot ( ) ;
152
+ } ;
153
+ return toReturn ;
154
+ } ;
155
+ } ,
85
156
} ;
86
- } ;
157
+ } ;
0 commit comments