Skip to content

Commit 2267c89

Browse files
author
Rocky Lin
committed
refactor
1 parent 10cb04b commit 2267c89

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

package/astParser.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ module.exports = elementType => {
1515
ast = ast.body;
1616
const statements = [];
1717

18-
function stMent(st) {
18+
function saveAstHooks(st) {
1919
st.forEach((el, i) => {
2020
if (el.match(/_use/)) hookState[el] = statements[i + 2];
2121
});
2222
}
2323

24-
function hookDeclare(astVal) {
24+
function findHookDeclarations(astVal) {
2525
astVal.forEach(elem => {
2626
if (elem.type === 'VariableDeclaration') {
2727
elem.declarations.forEach(decClar => {
@@ -35,18 +35,19 @@ module.exports = elementType => {
3535
if (ast[0].expression.body.body) {
3636
ast = ast[0].expression.body.body;
3737
// Hook Declarations will only be under 'VariableDeclaration' type
38-
hookDeclare(ast);
38+
findHookDeclarations(ast);
3939
// Iterate array and determine getter/setters based on pattern
40-
stMent(statements); // add key-value to hookState
40+
saveAstHooks(statements); // add key-value to hookState
4141
} else {
42+
// TODO test if this is needed, backward compatibility?
4243
// Iterate through AST of every function declaration
4344
// Check within each function declaration if there are hook declarations
4445
ast.forEach(functionDec => {
4546
const { body } = functionDec.body;
4647
// Traverse through the function's funcDecs and Expression Statements
47-
hookDeclare(body);
48+
findHookDeclarations(body);
4849
// Iterate array and determine getter/setters based on pattern
49-
stMent(statements); // add key-value to hookState
50+
saveAstHooks(statements); // add key-value to hookState
5051
});
5152
}
5253
}

package/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,17 @@ const mode = {
99
const linkFiber = require('./linkFiber')(snapShot, mode);
1010
const timeJump = require('./timeJump')(snapShot, mode);
1111

12-
window.addEventListener('message', ({ data: { action, payload } }) => { //runs automatically twice per second with inspectedElement
12+
window.addEventListener('message', ({ data: { action, payload } }) => {
13+
//runs automatically twice per second with inspectedElement
1314
switch (action) {
1415
case 'jumpToSnap':
1516
timeJump(payload);
17+
// Get the pathname from payload and add new entry to browser history
18+
// MORE: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
19+
if (payload.children[0].state && payload.children[0].state.location) {
20+
const route = payload.children[0].state.location.pathname;
21+
window.history.pushState('', '', route);
22+
}
1623
break;
1724
case 'setLock':
1825
mode.locked = payload;

package/linkFiber.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,12 @@ module.exports = (snap, mode) => {
7171
astHooks = Object.values(astHooks);
7272
// while memoizedState is truthy, save the value to the object
7373
while (memoizedState && memoizedState.queue) {
74-
// prevents useEffect from crashing on load
75-
// if (memoizedState.next.queue === null) { // prevents double pushing snapshot updates
7674
changeUseState(memoizedState);
77-
// }
78-
// memoized[astHooks[index]] = memoizedState.memoizedState;
75+
7976
memoized[astHooks[index]] = memoizedState.memoizedState;
8077
// Reassign memoizedState to its next value
8178
memoizedState = memoizedState.next;
82-
// Increment the index by 2
83-
index += 2;
79+
index += 2; // Increment the index by 2
8480
}
8581
return memoized;
8682
}

0 commit comments

Comments
 (0)