Skip to content

Commit 0dce8f7

Browse files
authored
Merge pull request #9 from oslabs-beta/hooks-implementation
Hooks now will not create new snapshots while time jumping
2 parents 520b0bc + f1a5e38 commit 0dce8f7

File tree

3 files changed

+51
-66
lines changed

3 files changed

+51
-66
lines changed

package/linkFiber.js

Lines changed: 24 additions & 58 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,15 +43,32 @@ module.exports = (snap, mode) => {
4643
component.setState.linkFiberChanged = true;
4744
}
4845

49-
// Helper function to
50-
46+
function changeUseState (component) {
47+
if (component.queue.dispatch.linkFiberChanged) return;
48+
// storing the original dispatch function definition somewhere
49+
const oldDispatch = component.queue.dispatch.bind(component.queue);
50+
// redefining the dispatch function so we can inject our code
51+
component.queue.dispatch = function (fiber, queue, action) {
52+
console.log('mode', mode);
53+
if (mode.locked && !mode.jumping) return;
54+
oldDispatch(fiber, queue, action);
55+
setTimeout(() => {
56+
console.log('Updating the snapshot tree after an action has been dispatched');
57+
updateSnapShotTree();
58+
sendSnapshot();
59+
}, 100);
60+
};
61+
component.queue.dispatch.linkFiberChanged = true;
62+
};
63+
5164
// Helper function to traverse through the memoized state
5265
function traverseHooks(memoizedState) {
5366
// Declare variables and assigned to 0th index and an empty object, respectively
5467
let index = 0;
5568
const memoizedObj = {};
5669
// while memoizedState is truthy, save the value to the object
5770
while (memoizedState) {
71+
changeUseState(memoizedState);
5872
// Increment the index by 1
5973
memoizedObj[`state${index += 1}`] = memoizedState.memoizedState;
6074
// Reassign memoizedState to its next value
@@ -85,12 +99,10 @@ module.exports = (snap, mode) => {
8599
// TODO: Refactor the conditionals - think about the edge case where a stateful
86100
// component might have a key called 'baseState' in the state
87101
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
88-
// console.log('The memoizedState is: ', memoizedState)
89-
90-
const traversed = traverseHooks(memoizedState);
91-
nextTree = tree.appendChild(traversed);
102+
console.log("I'm not supposed to run", currentFiber);
103+
memoizedState.traversed = traverseHooks(memoizedState);
104+
nextTree = tree.appendChild(memoizedState);
92105
}
93-
94106
// iterate through siblings
95107
createTree(sibling, tree);
96108
// iterate through children
@@ -101,24 +113,9 @@ module.exports = (snap, mode) => {
101113

102114
function updateSnapShotTree() {
103115
const { current } = fiberRoot;
116+
console.log('current', current);
104117
snap.tree = createTree(current);
105118
}
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-
// };
122119

123120
return {
124121
_(container) {
@@ -134,36 +131,5 @@ module.exports = (snap, mode) => {
134131
if (action === 'contentScriptStarted') sendSnapshot();
135132
});
136133
},
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-
},
168134
};
169-
};
135+
};

package/timeJump.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,38 @@ module.exports = (origin, mode) => {
1212
// recursively change state of tree
1313
function jump(target, coords = []) {
1414
const originNode = traverseTree(origin.tree, coords);
15-
// set the state of the origin tree
16-
originNode.component.setState(target.state, () => {
17-
// iterate through new children once state has been set
18-
target.children.forEach((child, i) => {
19-
jump(child, coords.concat(i));
15+
// set the state of the origin tree if the component is stateful
16+
if (originNode.component.setState) {
17+
originNode.component.setState(target.state, () => {
18+
// iterate through new children once state has been set
19+
target.children.forEach((child, i) => {
20+
jump(child, coords.concat(i));
21+
});
2022
});
21-
});
23+
}
24+
else {
25+
// if component uses hooks
26+
// variable for current location
27+
let currLocation = originNode.component;
28+
// state no
29+
let stateNum = 1;
30+
console.log('component', originNode.component);
31+
// while loop through the memoize tree
32+
while (currLocation) {
33+
currLocation.queue.dispatch(target.state[`state${stateNum}`]);
34+
currLocation = currLocation.next;
35+
stateNum += 1;
36+
}
37+
}
2238
}
2339

2440
return target => {
41+
console.log('im a target', target);
2542
// setting mode disables setState from posting messages to window
2643
mode.jumping = true;
2744
jump(target);
28-
mode.jumping = false;
45+
setTimeout(() => {
46+
mode.jumping = false;
47+
}, 100);
2948
};
3049
};

package/tree.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Tree {
2828
getCopy(copy = new Tree('root', true)) {
2929
// copy state of children
3030
copy.children = this.children.map(
31-
child => new Tree(child.component.state || child.component, true, child.component.constructor.name),
31+
child => new Tree(child.component.state || child.component.traversed, true, child.component.constructor.name),
3232
);
3333

3434
// copy children's children recursively

0 commit comments

Comments
 (0)