Skip to content

Commit 8b7c2a4

Browse files
authored
Merge pull request #17 from oslabs-beta/acorn-ast-test
Added masterState, helpers for hooks, fixed playback issue
2 parents 7e25ec7 + f1760d4 commit 8b7c2a4

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ coverage
88
src/extension/build.zip
99
src/extension/build.crx
1010
src/extension/build/key.pem
11+
package/__tests__

package/linkFiber.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
// links component state tree to library
55
// changes the setState method to also update our snapshot
66
const Tree = require('./tree');
7-
const astParser = require('./astParser.js');
7+
const astParser = require('./astParser');
8+
const { saveState } = require('./masterState');
89

910
module.exports = (snap, mode) => {
1011
let fiberRoot = null;
11-
let astHooks;
12+
let astHooks;
1213

1314
function sendSnapshot() {
1415
// don't send messages while jumping or while paused
@@ -45,14 +46,15 @@ module.exports = (snap, mode) => {
4546

4647
function changeUseState(component) {
4748
if (component.queue.dispatch.linkFiberChanged) return;
48-
// store the original dispatch function definition
49-
const oldDispatch = component.queue.dispatch.bind(component.queue);;
49+
// store the original dispatch function definition
50+
const oldDispatch = component.queue.dispatch.bind(component.queue);
5051
// redefine the dispatch function so we can inject our code
5152
component.queue.dispatch = (fiber, queue, action) => {
5253
// don't do anything if state is locked
5354
if (mode.locked && !mode.jumping) return;
54-
oldDispatch(fiber, queue, action);
55+
//oldDispatch(fiber, queue, action);
5556
setTimeout(() => {
57+
oldDispatch(fiber, queue, action);
5658
updateSnapShotTree();
5759
sendSnapshot();
5860
}, 100);
@@ -61,16 +63,16 @@ module.exports = (snap, mode) => {
6163
}
6264

6365
// Helper function to traverse through the memoized state
64-
// TODO: WE NEED TO CLEAN IT UP A BIT
6566
function traverseHooks(memoizedState) {
6667
// Declare variables and assigned to 0th index and an empty object, respectively
6768
const memoized = {};
6869
let index = 0;
6970
astHooks = Object.values(astHooks);
7071
// while memoizedState is truthy, save the value to the object
71-
while (memoizedState && astHooks) {
72+
while (memoizedState) {
7273
changeUseState(memoizedState);
73-
memoized[astHooks[index]] = memoizedState.memoizedState;
74+
//memoized[astHooks[index]] = memoizedState.memoizedState;
75+
memoized[astHooks[index]] = memoizedState.memoizedState;
7476
// Reassign memoizedState to its next value
7577
memoizedState = memoizedState.next;
7678
// Increment the index by 2
@@ -98,7 +100,7 @@ module.exports = (snap, mode) => {
98100
changeSetState(stateNode);
99101
}
100102
// Check if the component uses hooks
101-
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
103+
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
102104
// Add a traversed property and initialize to the evaluated result
103105
// of invoking traverseHooks, and reassign nextTree
104106
memoizedState.traversed = traverseHooks(memoizedState);
@@ -111,7 +113,8 @@ module.exports = (snap, mode) => {
111113

112114
return tree;
113115
}
114-
116+
// runs when page initially loads
117+
// but skips 1st hook click
115118
function updateSnapShotTree() {
116119
const { current } = fiberRoot;
117120
snap.tree = createTree(current);
@@ -125,9 +128,12 @@ module.exports = (snap, mode) => {
125128
// only assign internal rootp if it actually exists
126129
fiberRoot = _internalRoot || _reactRootContainer;
127130
// If hooks are implemented, traverse through the source code
128-
if (entryFile) astHooks = astParser(entryFile);
129-
130-
updateSnapShotTree();
131+
// Save the getter/setter combo for timeJump
132+
if (entryFile) {
133+
astHooks = astParser(entryFile);
134+
saveState(astHooks);
135+
}
136+
updateSnapShotTree();
131137
// send the initial snapshot once the content script has started up
132138
window.addEventListener('message', ({ data: { action } }) => {
133139
if (action === 'contentScriptStarted') sendSnapshot();

package/masterState.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Export two functions that either saves the AST state object into an array
2+
// or returns the array for use
3+
const masterState = [];
4+
5+
module.exports = {
6+
saveState: state => {
7+
for (const key in state) {
8+
masterState.push(state[key]);
9+
}
10+
return masterState;
11+
},
12+
returnState: () => masterState
13+
};

package/timeJump.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* eslint-disable no-param-reassign */
22
// traverses given tree by accessing children through coords array
3+
const { returnState } = require('./masterState');
4+
35
function traverseTree(tree, coords) {
46
let curr = tree;
57
coords.forEach(coord => {
@@ -21,13 +23,16 @@ module.exports = (origin, mode) => {
2123
});
2224
});
2325
} else {
24-
// if component uses hooks
26+
// if component uses hooks, traverse through the memoize tree
2527
let current = originNode.component;
26-
let index = 1;
27-
// Iterate through the memoized tree
28+
let index = 0;
29+
const hooks = returnState();
30+
// Iterate through the memoize tree
2831
while (current) {
29-
current.queue.dispatch(target.state[`state${index++}`]);
32+
current.queue.dispatch(target.state[hooks[index]]);
33+
// Reassign the current value
3034
current = current.next;
35+
index += 2;
3136
}
3237
}
3338
}

0 commit comments

Comments
 (0)