Skip to content

Commit 6f408a0

Browse files
Yu Jin KangYu Jin Kang
authored andcommitted
refactored linkFiber and added testUseReducer method
2 parents f044270 + 7c5ad42 commit 6f408a0

File tree

1 file changed

+66
-20
lines changed

1 file changed

+66
-20
lines changed

package/linkFiber.js

Lines changed: 66 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
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
@@ -12,7 +13,6 @@ module.exports = (snap, mode) => {
1213
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
1314
if (mode.jumping || mode.paused) return;
1415
const payload = snap.tree.getCopy();
15-
console.log('Here is the payload:', payload);
1616
// console.log('payload', payload);
1717
window.postMessage({
1818
action: 'recordSnap',
@@ -56,7 +56,7 @@ module.exports = (snap, mode) => {
5656
// while memoizedState is truthy, save the value to the object
5757
while (memoizedState) {
5858
// Increment the index by 1
59-
memoizedObj[`useState${index += 1}`] = memoizedState.memoizedState;
59+
memoizedObj[`state${index += 1}`] = memoizedState.memoizedState;
6060
// Reassign memoizedState to its next value
6161
memoizedState = memoizedState.next;
6262
}
@@ -87,37 +87,83 @@ module.exports = (snap, mode) => {
8787
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
8888
// console.log('The memoizedState is: ', memoizedState)
8989

90-
const result = traverseHooks(memoizedState);
91-
nextTree = tree.appendChild(result);
90+
const traversed = traverseHooks(memoizedState);
91+
nextTree = tree.appendChild(traversed);
9292
}
9393

9494
// iterate through siblings
9595
createTree(sibling, tree);
9696
// iterate through children
9797
createTree(child, nextTree);
98-
console.log('this is the tree after being traversed', tree)
98+
9999
return tree;
100100
}
101101

102102
function updateSnapShotTree() {
103-
104103
const { current } = fiberRoot;
105104
snap.tree = createTree(current);
106105
}
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();
107116

108-
return container => {
109-
const {
110-
_reactRootContainer: { _internalRoot },
111-
_reactRootContainer,
112-
} = container;
113-
// only assign internal root if it actually exists
114-
fiberRoot = _internalRoot || _reactRootContainer;
115-
console.log('here is the fiberRoot', fiberRoot)
116-
updateSnapShotTree();
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+
// };
117122

118-
// send the initial snapshot once the content script has started up
119-
window.addEventListener('message', ({ data: { action } }) => {
120-
if (action === 'contentScriptStarted') sendSnapshot();
121-
});
123+
return {
124+
_(container) {
125+
const {
126+
_reactRootContainer: { _internalRoot },
127+
_reactRootContainer,
128+
} = container;
129+
// only assign internal root if it actually exists
130+
fiberRoot = _internalRoot || _reactRootContainer;
131+
updateSnapShotTree();
132+
// send the initial snapshot once the content script has started up
133+
window.addEventListener('message', ({ data: { action } }) => {
134+
if (action === 'contentScriptStarted') sendSnapshot();
135+
});
136+
},
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);
146+
updateSnapShotTree();
147+
sendSnapshot();
148+
};
149+
return toReturn;
150+
};
151+
},
152+
testUseReducer(useReducer) {
153+
return function(reducer, initialState, init) {
154+
// Declare a constant and initialize to the built-in useReducer method
155+
// Which returns an array with the state and dispatch
156+
const reduced = useReducer(reducer, initialState, init);
157+
// Save the dispatch method
158+
const oldDispatch = reduced[1];
159+
// reassign the dispatch method with the additional methods
160+
reduced[1] = function(type) {
161+
oldDispatch(type);
162+
updateSnapShotTree();
163+
sendSnapshot();
164+
}
165+
return reduced;
166+
}
167+
},
122168
};
123-
};
169+
};

0 commit comments

Comments
 (0)