Skip to content

Commit 69dd2bc

Browse files
committed
refactored hooks helper functions
1 parent 6f408a0 commit 69dd2bc

File tree

1 file changed

+60
-36
lines changed

1 file changed

+60
-36
lines changed

package/linkFiber.js

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ module.exports = (snap, mode) => {
2020
});
2121
}
2222

23-
// DEV: This is how we know when a change has happened
24-
// (by injecting an event listener to every component's setState functionality).
25-
// Will need to create a separate one for useState components
2623
function changeSetState(component) {
2724
// check that setState hasn't been changed yet
2825
if (component.setState.linkFiberChanged) return;
@@ -46,8 +43,6 @@ module.exports = (snap, mode) => {
4643
component.setState.linkFiberChanged = true;
4744
}
4845

49-
// Helper function to
50-
5146
// Helper function to traverse through the memoized state
5247
function traverseHooks(memoizedState) {
5348
// Declare variables and assigned to 0th index and an empty object, respectively
@@ -87,7 +82,7 @@ module.exports = (snap, mode) => {
8782
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
8883
// console.log('The memoizedState is: ', memoizedState)
8984

90-
const traversed = traverseHooks(memoizedState);
85+
const traversed = traverseHooks(memoizedState);
9186
nextTree = tree.appendChild(traversed);
9287
}
9388

@@ -103,22 +98,6 @@ module.exports = (snap, mode) => {
10398
const { current } = fiberRoot;
10499
snap.tree = createTree(current);
105100
}
106-
// return container => {
107-
// console.log('this is the container', container)
108-
// const {
109-
// _reactRootContainer: { _internalRoot },
110-
// _reactRootContainer,
111-
// } = container;
112-
// // only assign internal root if it actually exists
113-
// fiberRoot = _internalRoot || _reactRootContainer;
114-
// console.log('fiberRoot', fiberRoot);
115-
// updateSnapShotTree();
116-
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-
// };
122101

123102
return {
124103
_(container) {
@@ -134,35 +113,80 @@ module.exports = (snap, mode) => {
134113
if (action === 'contentScriptStarted') sendSnapshot();
135114
});
136115
},
137-
testUseState(useState) {
138-
return function(initial) {
139-
// running the original useState and storing its result (state and dispatch function)
140-
const toReturn = useState(initial);
141-
// storing the original dispatch function definition somewhere
142-
const oldDispatch = toReturn[1];
143-
// redefining the dispatch function so we can inject our code
144-
toReturn[1] = function(newVal) {
145-
oldDispatch(newVal);
116+
// testUseState(useState) {
117+
// return initial => {
118+
// // running the original useState and storing its result (state and dispatch function)
119+
// const toReturn = useState(initial);
120+
// // storing the original dispatch function definition somewhere
121+
// const oldDispatch = toReturn[1];
122+
// // redefining the dispatch function so we can inject our code
123+
// toReturn[1] = function (newVal) {
124+
// console.log('dispatch:', oldDispatch(newVal));
125+
// setTimeout(() => {
126+
// updateSnapShotTree();
127+
// sendSnapshot();
128+
// }, 100);
129+
// };
130+
// return toReturn;
131+
// };
132+
// },
133+
testHooks(react) {
134+
const reducerInjection = reducer => {
135+
return (state, action) => {
136+
reducer(state, action);
146137
updateSnapShotTree();
147138
sendSnapshot();
148139
};
149-
return toReturn;
140+
};
141+
return {
142+
useState: initial => {
143+
const basicStateReducerClone = (state, action) => {
144+
return typeof action === 'function' ? action(state) : action;
145+
};
146+
// running the original useState and storing its result (state and dispatch function)
147+
const toReturn = react.useReducer(initial, reducerInjection(basicStateReducerClone));
148+
// // storing the original dispatch function definition somewhere
149+
// const oldDispatch = toReturn[1];
150+
// // redefining the dispatch function so we can inject our code
151+
// toReturn[1] = function (newVal) {
152+
// console.log('dispatch:', oldDispatch(newVal));
153+
// setTimeout(() => {
154+
// updateSnapShotTree();
155+
// sendSnapshot();
156+
// }, 100);
157+
// };
158+
return toReturn;
159+
},
160+
useReducer: (reducer, initialState, init) => {
161+
// Declare a constant and initialize to the built-in useReducer method
162+
// Which returns an array with the state and dispatch
163+
const reduced = react.useReducer(reducer, initialState, init);
164+
// Save the dispatch method
165+
const oldDispatch = reduced[1];
166+
// reassign the dispatch method with the additional methods
167+
reduced[1] = function (type) {
168+
oldDispatch(type);
169+
updateSnapShotTree();
170+
sendSnapshot();
171+
};
172+
return reduced;
173+
},
150174
};
151175
},
152176
testUseReducer(useReducer) {
153-
return function(reducer, initialState, init) {
177+
return (reducer, initialState, init) => {
154178
// Declare a constant and initialize to the built-in useReducer method
155179
// Which returns an array with the state and dispatch
156180
const reduced = useReducer(reducer, initialState, init);
157181
// Save the dispatch method
158182
const oldDispatch = reduced[1];
159183
// reassign the dispatch method with the additional methods
160-
reduced[1] = function(type) {
184+
reduced[1] = function (type) {
161185
oldDispatch(type);
162186
updateSnapShotTree();
163-
sendSnapshot();
187+
sendSnapshot();
164188
}
165-
return reduced;
189+
return reduced;
166190
}
167191
},
168192
};

0 commit comments

Comments
 (0)