Skip to content

Commit 621777e

Browse files
committed
Merge branch 'dev' into d3-test
2 parents 9e76a68 + fa27eb2 commit 621777e

File tree

13 files changed

+201
-76
lines changed

13 files changed

+201
-76
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
FROM node:10.16.2
2-
WORKDIR /usr/src/app
2+
WORKDIR /usr/src/app
33
COPY package*.json ./
44
RUN npm i

package-lock.json

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

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "reacttime-extension",
2+
"name": "reactime",
33
"description": "build web extension bundle.js",
44
"scripts": {
55
"build": "webpack --mode production",
@@ -26,7 +26,7 @@
2626
"David Chai",
2727
"Josh Kim",
2828
"Ruthba Anam",
29-
"Ryan Dang",
29+
"Ryan Dang",
3030
"Sierra Swaby",
3131
"Yujin Kang"
3232
],
@@ -59,6 +59,8 @@
5959
"webpack-cli": "^3.3.6"
6060
},
6161
"dependencies": {
62+
"acorn": "^7.1.0",
63+
"acorn-jsx": "^5.0.2",
6264
"d3": "^3.5.17",
6365
"immer": "^3.2.0",
6466
"jsondiffpatch": "^0.3.11",

package/astParser.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const acorn = require('acorn');
2+
const jsx = require('acorn-jsx');
3+
const JSXParser = acorn.Parser.extend(jsx());
4+
5+
// Helper function to recursively traverse through the user's codebase
6+
// INSERT HERE
7+
8+
module.exports = file => {
9+
// Initialize empty object to store the setters and getter
10+
const hookState = {};
11+
const ast = JSXParser.parse(file).body;
12+
// Iterate through AST of every function declaration
13+
// Check within each function declaration if there are hook declarations
14+
ast.forEach(func => {
15+
const { body } = func.body;
16+
const statements = [];
17+
// Traverse through the function's funcDecs and Expression Statements
18+
body.forEach(program => {
19+
if (program.type === 'VariableDeclaration') {
20+
program.declarations.forEach(dec => {
21+
statements.push(dec.id.name);
22+
});
23+
}
24+
});
25+
// Iterate through the array and determine getter/setters based on pattern
26+
for (let i = 0; i < statements.length; i += 1) {
27+
if (statements[i].match(/_use/)) {
28+
hookState[statements[i]] = statements[i + 2];
29+
}
30+
}
31+
});
32+
// Return the object with setters and getters
33+
return hookState;
34+
};

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: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
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');
78

89
module.exports = (snap, mode) => {
910
let fiberRoot = null;
11+
let astHooks;
1012

1113
function sendSnapshot() {
1214
// don't send messages while jumping or while paused
@@ -23,14 +25,12 @@ module.exports = (snap, mode) => {
2325
function changeSetState(component) {
2426
// check that setState hasn't been changed yet
2527
if (component.setState.linkFiberChanged) return;
26-
2728
// make a copy of setState
2829
const oldSetState = component.setState.bind(component);
29-
3030
// replace component's setState so developer doesn't change syntax
3131
// component.setState = newSetState.bind(component);
32-
component.setState = (state, callback = () => {}) => {
33-
// dont do anything if state is locked
32+
component.setState = (state, callback = () => { }) => {
33+
// don't do anything if state is locked
3434
// UNLESS we are currently jumping through time
3535
if (mode.locked && !mode.jumping) return;
3636
// continue normal setState functionality, except add sending message middleware
@@ -43,43 +43,45 @@ module.exports = (snap, mode) => {
4343
component.setState.linkFiberChanged = true;
4444
}
4545

46-
function changeUseState (component) {
46+
function changeUseState(component) {
4747
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;
48+
// store the original dispatch function definition
49+
const oldDispatch = component.queue.dispatch.bind(component.queue);;
50+
// redefine the dispatch function so we can inject our code
51+
component.queue.dispatch = (fiber, queue, action) => {
52+
// don't do anything if state is locked
53+
if (mode.locked && !mode.jumping) return;
5454
oldDispatch(fiber, queue, action);
5555
setTimeout(() => {
56-
console.log('Updating the snapshot tree after an action has been dispatched');
5756
updateSnapShotTree();
5857
sendSnapshot();
5958
}, 100);
6059
};
6160
component.queue.dispatch.linkFiberChanged = true;
62-
};
63-
61+
}
62+
6463
// Helper function to traverse through the memoized state
64+
// TODO: WE NEED TO CLEAN IT UP A BIT
6565
function traverseHooks(memoizedState) {
6666
// Declare variables and assigned to 0th index and an empty object, respectively
67-
let index = 0;
68-
const memoizedObj = {};
67+
const memoized = {};
68+
let index = 0;
69+
astHooks = Object.values(astHooks);
6970
// while memoizedState is truthy, save the value to the object
70-
while (memoizedState) {
71+
while (memoizedState && astHooks) {
7172
changeUseState(memoizedState);
72-
// Increment the index by 1
73-
memoizedObj[`state${index += 1}`] = memoizedState.memoizedState;
73+
memoized[astHooks[index]] = memoizedState.memoizedState;
7474
// Reassign memoizedState to its next value
7575
memoizedState = memoizedState.next;
76+
// Increment the index by 2
77+
index += 2;
7678
}
77-
return memoizedObj;
79+
return memoized;
7880
}
7981

8082
function createTree(currentFiber, tree = new Tree('root')) {
8183
if (!currentFiber) return tree;
82-
84+
8385
const {
8486
sibling,
8587
stateNode,
@@ -96,10 +98,9 @@ module.exports = (snap, mode) => {
9698
changeSetState(stateNode);
9799
}
98100
// Check if the component uses hooks
99-
// TODO: Refactor the conditionals - think about the edge case where a stateful
100-
// component might have a key called 'baseState' in the state
101101
if (memoizedState && memoizedState.hasOwnProperty('baseState')) {
102-
console.log("I'm not supposed to run", currentFiber);
102+
// Add a traversed property and initialize to the evaluated result
103+
// of invoking traverseHooks, and reassign nextTree
103104
memoizedState.traversed = traverseHooks(memoizedState);
104105
nextTree = tree.appendChild(memoizedState);
105106
}
@@ -113,23 +114,23 @@ module.exports = (snap, mode) => {
113114

114115
function updateSnapShotTree() {
115116
const { current } = fiberRoot;
116-
console.log('current', current);
117117
snap.tree = createTree(current);
118118
}
119119

120-
return {
121-
_(container) {
122-
const {
123-
_reactRootContainer: { _internalRoot },
124-
_reactRootContainer,
125-
} = container;
126-
// only assign internal root if it actually exists
127-
fiberRoot = _internalRoot || _reactRootContainer;
128-
updateSnapShotTree();
129-
// send the initial snapshot once the content script has started up
130-
window.addEventListener('message', ({ data: { action } }) => {
131-
if (action === 'contentScriptStarted') sendSnapshot();
132-
});
133-
},
134-
};
120+
return (container, entryFile) => {
121+
const {
122+
_reactRootContainer: { _internalRoot },
123+
_reactRootContainer,
124+
} = container;
125+
// only assign internal rootp if it actually exists
126+
fiberRoot = _internalRoot || _reactRootContainer;
127+
// If hooks are implemented, traverse through the source code
128+
if (entryFile) astHooks = astParser(entryFile);
129+
130+
updateSnapShotTree();
131+
// send the initial snapshot once the content script has started up
132+
window.addEventListener('message', ({ data: { action } }) => {
133+
if (action === 'contentScriptStarted') sendSnapshot();
134+
});
135+
}
135136
};

package/package-lock.json

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

package/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,13 @@
2727
"David Chai",
2828
"Josh Kim",
2929
"Ruthba Anam",
30-
"Ryan Dang",
30+
"Ryan Dang",
3131
"Sierra Swaby",
3232
"Yujin Kang"
3333
],
3434
"license": "MIT",
35-
"dependencies": {}
35+
"dependencies": {
36+
"acorn": "^7.1.0",
37+
"acorn-jsx": "^5.0.2"
38+
}
3639
}

0 commit comments

Comments
 (0)