Skip to content

Commit

Permalink
Resolve merge conflicts in pull #6
Browse files Browse the repository at this point in the history
  • Loading branch information
haejinjo committed Aug 18, 2020
1 parent 5018666 commit 3eb543b
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 229 deletions.
65 changes: 0 additions & 65 deletions src/backend/astParser.js

This file was deleted.

222 changes: 112 additions & 110 deletions src/backend/helpers.js → src/backend/helpers.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,110 +1,112 @@
/* eslint-disable linebreak-style */
/* eslint-disable no-shadow */
/* eslint-disable max-len */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable linebreak-style */
/* eslint-disable no-inner-declarations, no-loop-func */
// eslint-disable-next-line import/newline-after-import
const acorn = require('acorn');
const jsx = require('acorn-jsx');
// import { acorn } from 'acorn'; // javascript parser
// import { jsx } from 'acorn-jsx';

const JSXParser = acorn.Parser.extend(jsx());

// Returns a throttled version of an input function
// The returned throttled function only executes at most once every t milliseconds
export const throttle = (f, t) => {
let isOnCooldown = false;
let isCallQueued = false;
const throttledFunc = () => {
if (isOnCooldown && isCallQueued) return;
if (isOnCooldown) {
isCallQueued = true;
return;
}
f();
isOnCooldown = true;
isCallQueued = false;

const runAfterTimeout = () => {
if (isCallQueued) {
isCallQueued = false;
isOnCooldown = true; // not needed I think
f();
setTimeout(runAfterTimeout, t);
return;
}
isOnCooldown = false;
};
setTimeout(runAfterTimeout, t);
};
return throttledFunc;
};

// Helper function to grab the getters/setters from `elementType`
export const getHooksNames = (elementType) => {
// Initialize empty object to store the setters and getter
let ast;
try {
ast = JSXParser.parse(elementType);
} catch (e) {
return ['unknown'];
}
const hookState = {};
const hooksNames = {};

while (Object.hasOwnProperty.call(ast, 'body')) {
let tsCount = 0; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
ast = ast.body;
const statements = [];

/** All module exports always start off as a single 'FunctionDeclaration' type
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
* Iterate through AST of every function declaration
* Check within each function declaration if there are hook declarations */
ast.forEach((functionDec) => {
let body;
if (functionDec.expression && functionDec.expression.body)
body = functionDec.expression.body.body;
else body = functionDec.body ? functionDec.body.body : [];
// Traverse through the function's funcDecs and Expression Statements
body.forEach((elem) => {
if (elem.type === 'VariableDeclaration') {
elem.declarations.forEach((hook) => {
// * TypeScript hooks appear to have no "VariableDeclarator"
// * with id.name of _useState, _useState2, etc...
// * hook.id.type relevant for TypeScript applications
// *
// * Works for useState hooks
if (hook.id.type === 'ArrayPattern') {
hook.id.elements.forEach((hook) => {
statements.push(hook.name);
// * Unshift a wildcard name to achieve similar functionality as before
statements.unshift(`_useWildcard${tsCount}`);
tsCount += 1;
});
} else {
if (hook.init.object && hook.init.object.name) {
const varName = hook.init.object.name;
if (!hooksNames[varName] && varName.match(/_use/)) {
hooksNames[varName] = hook.id.name;
}
}
if (hook.id.name !== undefined) {
statements.push(hook.id.name);
}
}
});
}
});

statements.forEach((el, i) => {
if (el.match(/_use/)) hookState[el] = statements[i + 2];
});
});
}
return Object.values(hooksNames);
};
/* eslint-disable linebreak-style */
/* eslint-disable no-shadow */
/* eslint-disable max-len */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable linebreak-style */
/* eslint-disable no-inner-declarations, no-loop-func */
// eslint-disable-next-line import/newline-after-import
const acorn = require('acorn');
const jsx = require('acorn-jsx');
// import { acorn } from 'acorn'; // javascript parser
// import { jsx } from 'acorn-jsx';

const JSXParser = acorn.Parser.extend(jsx());

// Returns a throttled version of an input function
// The returned throttled function only executes at most once every t milliseconds
export const throttle = (f : Function, t: number) : Function => {
let isOnCooldown : boolean = false;
let isCallQueued : boolean = false;
const throttledFunc = () : any => {
if (isOnCooldown && isCallQueued) return;
if (isOnCooldown) {
isCallQueued = true;
return;
}
f();
isOnCooldown = true;
isCallQueued = false;
const runAfterTimeout = () : any => {
if (isCallQueued) {
isCallQueued = false;
isOnCooldown = true; // not needed I think
f();
setTimeout(runAfterTimeout, t);
return;
}
isOnCooldown = false;
};
setTimeout(runAfterTimeout, t);
};
return throttledFunc;
};

// Helper function to grab the getters/setters from `elementType`
export const getHooksNames = (elementType : string) : Array<string> => {
// Initialize empty object to store the setters and getter
let ast : any;
try {
ast = JSXParser.parse(elementType);
} catch (e) {
return ['unknown'];
}

// const hookState = {};
// const hooksNames = {};
const hookState: any = {};
const hooksNames: any = {};

while (Object.hasOwnProperty.call(ast, 'body')) {
let tsCount : number = 0; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
ast = ast.body;
const statements : Array<string> = [];

/** All module exports always start off as a single 'FunctionDeclaration' type
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
* Iterate through AST of every function declaration
* Check within each function declaration if there are hook declarations */
ast.forEach((functionDec) => {
let body : any;
if (functionDec.expression && functionDec.expression.body)
body = functionDec.expression.body.body;
else body = functionDec.body ? functionDec.body.body : [];
// Traverse through the function's funcDecs and Expression Statements
body.forEach((elem : any) => {
if (elem.type === 'VariableDeclaration') {
elem.declarations.forEach((hook : any) => {
// * TypeScript hooks appear to have no "VariableDeclarator"
// * with id.name of _useState, _useState2, etc...
// * hook.id.type relevant for TypeScript applications
// *
// * Works for useState hooks
if (hook.id.type === 'ArrayPattern') {
hook.id.elements.forEach((hook) => {
statements.push(hook.name);
// * Unshift a wildcard name to achieve similar functionality as before
statements.unshift(`_useWildcard${tsCount}`);
tsCount += 1;
});
} else {
if (hook.init.object && hook.init.object.name) {
const varName : any = hook.init.object.name;
if (!hooksNames[varName] && varName.match(/_use/)) {
hooksNames[varName] = hook.id.name;
}
}
if (hook.id.name !== undefined) {
statements.push(hook.id.name);
}
}
});
}
});

statements.forEach((el, i) => {
if (el.match(/_use/)) hookState[el] = statements[i + 2];
});
});
}
return Object.values(hooksNames);
};
11 changes: 0 additions & 11 deletions src/backend/linkFiber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import 'core-js';

// const Tree = require('./tree').default;
// const componentActionsRecord = require('./masterState');
import { useGotoRecoilSnapshot, RecoilRoot, useRecoilSnapshot } from 'recoil';
import {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Snapshot,
Expand All @@ -48,7 +47,6 @@ import {
import Tree from './tree';
import componentActionsRecord from './masterState';
import { throttle, getHooksNames } from './helpers';
import ReactDOM from 'react-dom';

declare global {
interface Window {
Expand All @@ -61,7 +59,6 @@ const circularComponentTable = new Set();

export default (snap: Snapshot, mode: Mode): (() => void) => {
let fiberRoot = null;

function sendSnapshot(): void {
// Don't send messages while jumping or while paused
if (mode.jumping || mode.paused) return;
Expand Down Expand Up @@ -167,9 +164,6 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
// });
atomArray.push(memoizedProps);


// console.log('1st ATOM ARRAY', atomArray);

function traverseRecoilHooks(memoizedState: any): HookStates {
const hooksStates: HookStates = [];
while (memoizedState && memoizedState.queue) {
Expand Down Expand Up @@ -203,10 +197,7 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
// so we must traverse through the list and get the states.
// We then store them along with the corresponding memoizedState.queue,
// which includes the dispatch() function we use to change their state.

const hooksStates = traverseRecoilHooks(memoizedState);

const hooksNames = getHooksNames(elementType.toString());
hooksStates.forEach((state, i) => {

hooksIndex = componentActionsRecord.saveNew(
Expand Down Expand Up @@ -354,8 +345,6 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
const reactInstance = devTools ? devTools.renderers.get(1) : null;
fiberRoot = devTools.getFiberRoots(1).values().next().value;

// console.log('FIBER ROOT', fiberRoot.current);

const throttledUpdateSnapshot = throttle(updateSnapShotTree, 70);
document.addEventListener('visibilitychange', onVisibilityChange);
if (reactInstance && reactInstance.version) {
Expand Down
41 changes: 0 additions & 41 deletions src/backend/masterState.js

This file was deleted.

Loading

0 comments on commit 3eb543b

Please sign in to comment.