Skip to content

Commit b886d12

Browse files
author
Rocky Lin
committed
useState-useContext
1 parent 30feca1 commit b886d12

File tree

3 files changed

+95
-54
lines changed

3 files changed

+95
-54
lines changed

package/astParser.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,43 @@ module.exports = elementType => {
1212
while (Object.hasOwnProperty.call(ast, 'body')) {
1313
// Traverse down .body once before invoking parsing logic and will loop through any .body after
1414
ast = ast.body;
15-
// Iterate through AST of every function declaration
16-
// Check within each function declaration if there are hook declarations
17-
ast.forEach(functionDec => {
18-
const { body } = functionDec.body;
19-
const statements = [];
20-
// Traverse through the function's funcDecs and Expression Statements
21-
body.forEach(program => {
22-
// Hook Declarations will only be under 'VariableDeclaration' type
23-
if (program.type === 'VariableDeclaration') {
24-
program.declarations.forEach(dec => {
25-
statements.push(dec.id.name);
26-
});
27-
}
15+
const statements = [];
16+
17+
// handle useState useContext
18+
if (ast[0].expression.body.body) {
19+
ast = ast[0].expression.body.body;
20+
// Hook Declarations will only be under 'VariableDeclaration' type
21+
hookDeclare(ast);
22+
// Iterate array and determine getter/setters based on pattern
23+
stMent(statements); // add key-value to hookState
24+
} else {
25+
// Iterate through AST of every function declaration
26+
// Check within each function declaration if there are hook declarations
27+
ast.forEach(functionDec => {
28+
const { body } = functionDec.body;
29+
// Traverse through the function's funcDecs and Expression Statements
30+
hookDeclare(body);
31+
// Iterate array and determine getter/setters based on pattern
32+
stMent(statements); // add key-value to hookState
2833
});
29-
// Iterate through the array and determine getter/setters based on pattern
30-
for (let i = 0; i < statements.length; i += 1) {
31-
if (statements[i].match(/_use/)) {
32-
hookState[statements[i]] = statements[i + 2];
33-
}
34+
}
35+
}
36+
37+
function stMent(st) {
38+
st.forEach((el, i) => {
39+
if (el.match(/_use/)) hookState[el] = statements[i + 2];
40+
});
41+
}
42+
43+
function hookDeclare(astVal) {
44+
astVal.forEach(elem => {
45+
if (elem.type === 'VariableDeclaration') {
46+
elem.declarations.forEach(decClar => {
47+
statements.push(decClar.id.name);
48+
});
3449
}
3550
});
3651
}
52+
3753
return hookState;
3854
};

package/linkFiber.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ module.exports = (snap, mode) => {
3131
const oldSetState = component.setState.bind(component);
3232
// replace component's setState so developer doesn't change syntax
3333
// component.setState = newSetState.bind(component);
34-
component.setState = (state, callback = () => { }) => {
34+
component.setState = (state, callback = () => {}) => {
3535
// don't do anything if state is locked
3636
// UNLESS we are currently jumping through time
3737
if (mode.locked && !mode.jumping) return;
@@ -70,10 +70,11 @@ module.exports = (snap, mode) => {
7070
let index = 0;
7171
astHooks = Object.values(astHooks);
7272
// while memoizedState is truthy, save the value to the object
73-
while (memoizedState && memoizedState.queue) { // prevents useEffect from crashing on load
74-
if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
75-
changeUseState(memoizedState);
76-
}
73+
while (memoizedState && memoizedState.queue) {
74+
// prevents useEffect from crashing on load
75+
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
76+
changeUseState(memoizedState);
77+
// }
7778
// memoized[astHooks[index]] = memoizedState.memoizedState;
7879
memoized[astHooks[index]] = memoizedState.memoizedState;
7980
// Reassign memoizedState to its next value
@@ -104,7 +105,10 @@ module.exports = (snap, mode) => {
104105
changeSetState(stateNode);
105106
}
106107
// Check if the component uses hooks
107-
if (memoizedState && Object.hasOwnProperty.call(memoizedState, 'baseState')) {
108+
if (
109+
memoizedState &&
110+
Object.hasOwnProperty.call(memoizedState, 'baseState')
111+
) {
108112
// Traverse through the currentFiber and extract the getters/setters
109113
astHooks = astParser(elementType);
110114
saveState(astHooks);

src/extension/background.js

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ const firstSnapshotReceived = {};
88
// there will be the same number of objects in here as there are reactime tabs open for each user application being worked on
99
const tabsObj = {};
1010

11-
console.log("background scripts");
11+
console.log('background scripts');
1212

1313
function createTabObj(title) {
14-
1514
// update tabsObj
1615
return {
1716
title,
@@ -62,14 +61,17 @@ function changeCurrLocation(tabObj, rootNode, index) {
6261
return;
6362
// if not, recurse on each one of the children
6463
}
65-
rootNode.children.forEach(child => {
66-
changeCurrLocation(tabObj, child, index);
67-
});
64+
// added if (rootNode.children) { <=======
65+
if (rootNode.children) {
66+
rootNode.children.forEach(child => {
67+
changeCurrLocation(tabObj, child, index);
68+
});
69+
}
6870
}
6971

70-
7172
// establishing connection with devtools
72-
chrome.runtime.onConnect.addListener(port => { // port is one end of the connection - an object
73+
chrome.runtime.onConnect.addListener(port => {
74+
// port is one end of the connection - an object
7375

7476
// push every port connected to the ports array
7577
portsArr.push(port);
@@ -92,8 +94,9 @@ chrome.runtime.onConnect.addListener(port => { // port is one end of the connect
9294
}
9395
});
9496

95-
// receive snapshot from devtools and send it to contentScript -
96-
port.onMessage.addListener(msg => { // msg is action denoting a time jump in devtools
97+
// receive snapshot from devtools and send it to contentScript -
98+
port.onMessage.addListener(msg => {
99+
// msg is action denoting a time jump in devtools
97100

98101
// ---------------------------------------------------------------
99102
// message incoming from devTools should look like this:
@@ -143,7 +146,11 @@ chrome.runtime.onMessage.addListener((request, sender) => {
143146
let isReactTimeTravel = false;
144147

145148
// Filter out tabs that don't have reactime
146-
if (action === 'tabReload' || action === 'recordSnap' || action === 'jumpToSnap') {
149+
if (
150+
action === 'tabReload' ||
151+
action === 'recordSnap' ||
152+
action === 'jumpToSnap'
153+
) {
147154
isReactTimeTravel = true;
148155
} else return;
149156

@@ -175,10 +182,12 @@ chrome.runtime.onMessage.addListener((request, sender) => {
175182
tabsObj[tabId].index = 1;
176183

177184
// send a message to devtools
178-
portsArr.forEach(bg => bg.postMessage({
179-
action: 'initialConnectSnapshots',
180-
payload: tabsObj,
181-
}));
185+
portsArr.forEach(bg =>
186+
bg.postMessage({
187+
action: 'initialConnectSnapshots',
188+
payload: tabsObj,
189+
})
190+
);
182191
}
183192
reloaded[tabId] = true;
184193
break;
@@ -191,12 +200,17 @@ chrome.runtime.onMessage.addListener((request, sender) => {
191200
reloaded[tabId] = false;
192201

193202
tabsObj[tabId].snapshots.push(request.payload);
194-
sendToHierarchy(tabsObj[tabId], new Node(request.payload, tabsObj[tabId]));
203+
sendToHierarchy(
204+
tabsObj[tabId],
205+
new Node(request.payload, tabsObj[tabId])
206+
);
195207
if (portsArr.length > 0) {
196-
portsArr.forEach(bg => bg.postMessage({
197-
action: 'initialConnectSnapshots',
198-
payload: tabsObj,
199-
}));
208+
portsArr.forEach(bg =>
209+
bg.postMessage({
210+
action: 'initialConnectSnapshots',
211+
payload: tabsObj,
212+
})
213+
);
200214
}
201215
break;
202216
}
@@ -207,15 +221,20 @@ chrome.runtime.onMessage.addListener((request, sender) => {
207221
} else {
208222
tabsObj[tabId].snapshots.push(request.payload);
209223
//! INVOKING buildHierarchy FIGURE OUT WHAT TO PASS IN!!!!
210-
sendToHierarchy(tabsObj[tabId], new Node(request.payload, tabsObj[tabId]));
224+
sendToHierarchy(
225+
tabsObj[tabId],
226+
new Node(request.payload, tabsObj[tabId])
227+
);
211228
}
212229
// send message to devtools
213230
if (portsArr.length > 0) {
214-
portsArr.forEach(bg => bg.postMessage({
215-
action: 'sendSnapshots',
216-
payload: tabsObj,
217-
sourceTab,
218-
}));
231+
portsArr.forEach(bg =>
232+
bg.postMessage({
233+
action: 'sendSnapshots',
234+
payload: tabsObj,
235+
sourceTab,
236+
})
237+
);
219238
}
220239
break;
221240
}
@@ -228,10 +247,12 @@ chrome.runtime.onMessage.addListener((request, sender) => {
228247
chrome.tabs.onRemoved.addListener(tabId => {
229248
// tell devtools which tab to delete
230249
if (portsArr.length > 0) {
231-
portsArr.forEach(bg => bg.postMessage({
232-
action: 'deleteTab',
233-
payload: tabId,
234-
}));
250+
portsArr.forEach(bg =>
251+
bg.postMessage({
252+
action: 'deleteTab',
253+
payload: tabId,
254+
})
255+
);
235256
}
236257

237258
// delete the tab from the tabsObj
@@ -253,7 +274,7 @@ chrome.runtime.onInstalled.addListener(() => {
253274
// when context menu is clicked, listen for the menuItemId,
254275
// if user clicked on reactime, open the devtools window
255276
chrome.contextMenus.onClicked.addListener(({ menuItemId }) => {
256-
console.log("clicked menu");
277+
console.log('clicked menu');
257278
const options = {
258279
type: 'panel',
259280
left: 0,

0 commit comments

Comments
 (0)