Skip to content

Commit 46bbe4b

Browse files
authored
Merge pull request #6 from oslabs-beta/hooks-implementation
Hooks implementation
2 parents 17cf982 + 6f408a0 commit 46bbe4b

File tree

4 files changed

+43
-25
lines changed

4 files changed

+43
-25
lines changed

package/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ window.addEventListener('message', ({ data: { action, payload } }) => {
2525
}
2626
});
2727

28-
module.exports = linkFiber;
28+
module.exports = linkFiber;

package/linkFiber.js

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

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
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
2426
function changeSetState(component) {
2527
// check that setState hasn't been changed yet
2628
if (component.setState.linkFiberChanged) return;
@@ -44,6 +46,8 @@ module.exports = (snap, mode) => {
4446
component.setState.linkFiberChanged = true;
4547
}
4648

49+
// Helper function to
50+
4751
// Helper function to traverse through the memoized state
4852
function traverseHooks(memoizedState) {
4953
// Declare variables and assigned to 0th index and an empty object, respectively
@@ -52,7 +56,7 @@ module.exports = (snap, mode) => {
5256
// while memoizedState is truthy, save the value to the object
5357
while (memoizedState) {
5458
// Increment the index by 1
55-
memoizedObj[`useState${index += 1}`] = memoizedState.memoizedState;
59+
memoizedObj[`state${index += 1}`] = memoizedState.memoizedState;
5660
// Reassign memoizedState to its next value
5761
memoizedState = memoizedState.next;
5862
}
@@ -61,8 +65,7 @@ module.exports = (snap, mode) => {
6165

6266
function createTree(currentFiber, tree = new Tree('root')) {
6367
if (!currentFiber) return tree;
64-
// We have to figure out which properties to destructure from currentFiber
65-
// To support hooks and Context API
68+
6669
const {
6770
sibling,
6871
stateNode,
@@ -82,10 +85,10 @@ module.exports = (snap, mode) => {
8285
// TODO: Refactor the conditionals - think about the edge case where a stateful
8386
// component might have a key called 'baseState' in the state
8487
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);
88+
// console.log('The memoizedState is: ', memoizedState)
89+
90+
const traversed = traverseHooks(memoizedState);
91+
nextTree = tree.appendChild(traversed);
8992
}
9093

9194
// iterate through siblings
@@ -97,21 +100,18 @@ module.exports = (snap, mode) => {
97100
}
98101

99102
function updateSnapShotTree() {
100-
// console.log('fiberRoot', fiberRoot);
101103
const { current } = fiberRoot;
102-
console.log('current', current);
103104
snap.tree = createTree(current);
104105
}
105-
106106
// return container => {
107-
// // console.log('Container', container);
107+
// console.log('this is the container', container)
108108
// const {
109109
// _reactRootContainer: { _internalRoot },
110110
// _reactRootContainer,
111-
// } = container;
112-
// // console.log('Root container', _reactRootContainer);
111+
// } = container;
113112
// // only assign internal root if it actually exists
114113
// fiberRoot = _internalRoot || _reactRootContainer;
114+
// console.log('fiberRoot', fiberRoot);
115115
// updateSnapShotTree();
116116

117117
// // send the initial snapshot once the content script has started up
@@ -121,37 +121,49 @@ module.exports = (snap, mode) => {
121121
// };
122122

123123
return {
124-
main(container) {
125-
// console.log('Container', container);
124+
_(container) {
126125
const {
127126
_reactRootContainer: { _internalRoot },
128127
_reactRootContainer,
129128
} = container;
130-
// console.log('Root container', _reactRootContainer);
131129
// only assign internal root if it actually exists
132130
fiberRoot = _internalRoot || _reactRootContainer;
133131
updateSnapShotTree();
134-
135132
// send the initial snapshot once the content script has started up
136133
window.addEventListener('message', ({ data: { action } }) => {
137134
if (action === 'contentScriptStarted') sendSnapshot();
138135
});
139136
},
140-
changeUseState(useState) {
141-
return function (initial) {
137+
testUseState(useState) {
138+
return function(initial) {
142139
// running the original useState and storing its result (state and dispatch function)
143140
const toReturn = useState(initial);
144-
console.log(toReturn);
145141
// storing the original dispatch function definition somewhere
146142
const oldDispatch = toReturn[1];
147143
// redefining the dispatch function so we can inject our code
148-
toReturn[1] = function (callback) {
149-
oldDispatch(callback);
144+
toReturn[1] = function(newVal) {
145+
oldDispatch(newVal);
150146
updateSnapShotTree();
151147
sendSnapshot();
152148
};
153149
return toReturn;
154150
};
155151
},
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+
},
156168
};
157169
};

package/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@
2222
"react devtool"
2323
],
2424
"author": "Bryan Lee, Josh Kim, Ryan Dang, Sierra Swaby",
25-
"license": "MIT"
25+
"license": "MIT",
26+
"dependencies": {}
2627
}

0 commit comments

Comments
 (0)