Skip to content

Commit 281546c

Browse files
committed
Initial docs commit
1 parent 32e7788 commit 281546c

File tree

4 files changed

+306
-298
lines changed

4 files changed

+306
-298
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,12 @@
4242
"Ergi Shehu",
4343
"Gabriela Jardim Aquino",
4444
"Gregory Panciera",
45+
"Haejin Jo",
46+
"Hien Nguyen",
47+
"Jack Crish",
4548
"Josh Kim",
4649
"Joshua Howard",
50+
"Kevin Fey",
4751
"Nathanael Wa Mwenze",
4852
"Prasanna Malla",
4953
"Rajeeb Banstola",
@@ -121,4 +125,4 @@
121125
"react-select": "^3.1.0",
122126
"recoil": "0.0.10"
123127
}
124-
}
128+
}

src/backend/helpers.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,43 @@
99
// eslint-disable-next-line import/newline-after-import
1010
const acorn = require('acorn');
1111
const jsx = require('acorn-jsx');
12-
// import { acorn } from 'acorn'; // javascript parser
13-
// import { jsx } from 'acorn-jsx';
14-
1512
const JSXParser = acorn.Parser.extend(jsx());
16-
1713
/**
1814
* @method throttle
1915
* @param f A function to throttle
2016
* @param t A number of milliseconds to use as throttling interval
2117
* @returns A function that limits input function, `f`, from being called more than once every `t` milliseconds
18+
*
2219
*/
2320
export const throttle = (f: Function, t: number): Function => {
21+
// Initialize boolean flags for callback, throttledFunc
2422
let isOnCooldown: boolean = false;
2523
let isCallQueued: boolean = false;
24+
25+
// Wrap the passed-in function, f, in a callback function that "throttles"
26+
// (puts a limit on) the number of calls that can be made to function, f
27+
// in a given period of time (ms), t
2628
const throttledFunc = (): any => {
29+
// CASE 1: In cooldown mode and we already have a function waiting to be executed,
30+
// so do nothing
2731
if (isOnCooldown && isCallQueued) return;
32+
33+
// CASE 2: In cooldown mode, but we have no functions waiting to be executed,
34+
// so just make note that we now have a call waiting to be executed and return
2835
if (isOnCooldown) {
2936
isCallQueued = true;
3037
return;
3138
}
39+
40+
// CASE 3: If we are ready to "fire":
41+
// Execute the function, f, immediately
3242
f();
43+
// Initiate a new cooldown period and reset the "call queue"
3344
isOnCooldown = true;
3445
isCallQueued = false;
46+
47+
// Declare a function that checks whether we have
48+
// another function to be executed right after.
3549
const runAfterTimeout = (): any => {
3650
if (isCallQueued) {
3751
isCallQueued = false;
@@ -42,16 +56,17 @@ export const throttle = (f: Function, t: number): Function => {
4256
}
4357
isOnCooldown = false;
4458
};
59+
4560
setTimeout(runAfterTimeout, t);
4661
};
62+
4763
return throttledFunc;
4864
};
4965

5066
// Helper function to grab the getters/setters from `elementType`
51-
5267
/**
5368
* @method getHooksNames
54-
* @param elementType The fiber (whose hooks we want) `type`, A stringified function of the component the Fiber whose hooks we want corresponds to
69+
* @param elementType The fiber `type`, A stringified function of the component the Fiber whose hooks we want corresponds to
5570
* @returns An array of strings
5671
*/
5772
export const getHooksNames = (elementType: string): Array<string> => {
@@ -60,41 +75,39 @@ export const getHooksNames = (elementType: string): Array<string> => {
6075
try {
6176
ast = JSXParser.parse(elementType);
6277
} catch (e) {
63-
console.error(`getHooksNames ERROR: Failed to parse elementType string:\n${elementType}`);
6478
return ['unknown'];
6579
}
6680

6781
const hooksNames: any = {};
6882

83+
// Begin search for hook names, only if ast has a body property.
6984
while (Object.hasOwnProperty.call(ast, 'body')) {
7085
let tsCount: number = 0; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
7186
ast = ast.body;
72-
const statements: Array<string> = [];
7387

88+
// Statements get all the names of the hooks. For example: useCount, useWildcard, ...
89+
const statements: Array<string> = [];
7490
/** All module exports always start off as a single 'FunctionDeclaration' type
7591
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
7692
* Iterate through AST of every function declaration
7793
* Check within each function declaration if there are hook declarations */
7894
ast.forEach((functionDec) => {
7995
let body: any;
80-
if (functionDec.expression && functionDec.expression.body)
81-
body = functionDec.expression.body.body;
96+
if (functionDec.expression && functionDec.expression.body) body = functionDec.expression.body.body;
8297
else body = functionDec.body ? functionDec.body.body : [];
8398
// Traverse through the function's funcDecs and Expression Statements
8499
body.forEach((elem: any) => {
100+
// Hooks will always be contained in a variable declaration
85101
if (elem.type === 'VariableDeclaration') {
86102
elem.declarations.forEach((hook: any) => {
87-
// * TypeScript hooks appear to have no "VariableDeclarator"
88-
// * with id.name of _useState, _useState2, etc...
89-
// * hook.id.type relevant for TypeScript applications
90-
// *
91-
// * Works for useState hooks
103+
// Parse destructured statements pair
92104
if (hook.id.type === 'ArrayPattern') {
93105
hook.id.elements.forEach((hook) => {
94106
statements.push(`_useWildcard${tsCount}`);
95107
statements.push(hook.name);
96108
tsCount += 1;
97109
});
110+
// Process hook function invocation ?
98111
} else {
99112
if (hook.init.object && hook.init.object.name) {
100113
const varName: any = hook.init.object.name;

0 commit comments

Comments
 (0)