Skip to content

Commit 38201b8

Browse files
committed
creating dummy component for d3 node tree graph testing
1 parent c685cd5 commit 38201b8

18 files changed

+502
-125
lines changed

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: 51 additions & 84 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
@@ -20,20 +22,15 @@ module.exports = (snap, mode) => {
2022
});
2123
}
2224

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
2625
function changeSetState(component) {
2726
// check that setState hasn't been changed yet
2827
if (component.setState.linkFiberChanged) return;
29-
3028
// make a copy of setState
3129
const oldSetState = component.setState.bind(component);
32-
3330
// replace component's setState so developer doesn't change syntax
3431
// component.setState = newSetState.bind(component);
35-
component.setState = (state, callback = () => {}) => {
36-
// dont do anything if state is locked
32+
component.setState = (state, callback = () => { }) => {
33+
// don't do anything if state is locked
3734
// UNLESS we are currently jumping through time
3835
if (mode.locked && !mode.jumping) return;
3936
// continue normal setState functionality, except add sending message middleware
@@ -46,26 +43,45 @@ module.exports = (snap, mode) => {
4643
component.setState.linkFiberChanged = true;
4744
}
4845

49-
// Helper function to
46+
function changeUseState(component) {
47+
if (component.queue.dispatch.linkFiberChanged) 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;
54+
oldDispatch(fiber, queue, action);
55+
setTimeout(() => {
56+
updateSnapShotTree();
57+
sendSnapshot();
58+
}, 100);
59+
};
60+
component.queue.dispatch.linkFiberChanged = true;
61+
}
5062

5163
// Helper function to traverse through the memoized state
64+
// TODO: WE NEED TO CLEAN IT UP A BIT
5265
function traverseHooks(memoizedState) {
5366
// Declare variables and assigned to 0th index and an empty object, respectively
54-
let index = 0;
55-
const memoizedObj = {};
67+
const memoized = {};
68+
let index = 0;
69+
astHooks = Object.values(astHooks);
5670
// while memoizedState is truthy, save the value to the object
57-
while (memoizedState) {
58-
// Increment the index by 1
59-
memoizedObj[`state${index += 1}`] = memoizedState.memoizedState;
71+
while (memoizedState && astHooks) {
72+
changeUseState(memoizedState);
73+
memoized[astHooks[index]] = memoizedState.memoizedState;
6074
// Reassign memoizedState to its next value
6175
memoizedState = memoizedState.next;
76+
// Increment the index by 2
77+
index += 2;
6278
}
63-
return memoizedObj;
79+
return memoized;
6480
}
6581

6682
function createTree(currentFiber, tree = new Tree('root')) {
6783
if (!currentFiber) return tree;
68-
84+
6985
const {
7086
sibling,
7187
stateNode,
@@ -82,15 +98,12 @@ module.exports = (snap, mode) => {
8298
changeSetState(stateNode);
8399
}
84100
// Check if the component uses hooks
85-
// TODO: Refactor the conditionals - think about the edge case where a stateful
86-
// 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+
// Add a traversed property and initialize to the evaluated result
103+
// of invoking traverseHooks, and reassign nextTree
104+
memoizedState.traversed = traverseHooks(memoizedState);
105+
nextTree = tree.appendChild(memoizedState);
92106
}
93-
94107
// iterate through siblings
95108
createTree(sibling, tree);
96109
// iterate through children
@@ -103,67 +116,21 @@ module.exports = (snap, mode) => {
103116
const { current } = fiberRoot;
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();
116119

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-
// };
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);
122129

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-
},
168-
};
169-
};
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+
}
136+
};

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.

0 commit comments

Comments
 (0)