Skip to content

Commit 2d50d8d

Browse files
committed
Restructure backend folder
1 parent f597535 commit 2d50d8d

File tree

11 files changed

+168
-171
lines changed

11 files changed

+168
-171
lines changed

src/backend/linkFiber.ts renamed to src/backend/controllers/createTree/index.ts

Lines changed: 6 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
1-
/* eslint-disable guard-for-in */
2-
/* eslint-disable no-restricted-syntax */
3-
/* eslint-disable @typescript-eslint/no-explicit-any */
4-
/* eslint-disable max-len */
5-
/* eslint-disable indent */
6-
/* eslint-disable brace-style */
7-
/* eslint-disable comma-dangle */
8-
/* eslint-disable no-underscore-dangle */
9-
/* eslint-disable func-names */
10-
/* eslint-disable no-use-before-define */
11-
/* eslint-disable no-param-reassign */
12-
/* eslint-disable-next-line no-mixed-operators */
13-
141
// import typescript types
152
import {
16-
// tree
17-
Snapshot,
18-
// jump, pause
19-
Status,
20-
// array of state and component
21-
HookStates,
223
// object with tree structure
234
Fiber,
24-
// object with tree structure
25-
FiberRoot,
26-
} from './types/backendTypes';
27-
import { DevTools } from './types/linkFiberTypes';
5+
} from '../../types/backendTypes';
286
import {
297
FunctionComponent,
308
ClassComponent,
@@ -54,30 +32,17 @@ import {
5432
Block,
5533
OffscreenComponent,
5634
LegacyHiddenComponent,
57-
} from './types/backendTypes';
35+
} from '../../types/backendTypes';
5836
// import function that creates a tree
59-
import Tree from './tree';
37+
import Tree from '../../models/tree';
6038
// passes the data down to its components
61-
import componentActionsRecord from './masterState';
62-
import updateSnapShotTree from './snapShot';
63-
64-
// throttle returns a function that can be called any number of times (possibly in quick succession) but will only invoke the callback at most once every x ms
65-
// getHooksNames - helper function to grab the getters/setters from `elementType`
66-
import { throttle } from './helpers';
39+
import componentActionsRecord from '../../models/masterState';
6740
import {
6841
getHooksNames,
6942
getHooksStateAndUpdateMethod,
7043
getStateAndContextData,
7144
filterAndFormatData,
72-
} from './statePropsExtractors';
73-
74-
// Set global variables to use in exported module and helper functions
75-
declare global {
76-
interface Window {
77-
__REACT_DEVTOOLS_GLOBAL_HOOK__?: DevTools;
78-
__REDUX_DEVTOOLS_EXTENSION__?: any;
79-
}
80-
}
45+
} from '../../controllers/createTree/statePropExtractors';
8146

8247
// -------------------------CREATE TREE TO SEND TO FRONT END--------------------
8348
/**
@@ -95,7 +60,7 @@ declare global {
9560
// TODO: Not sure why the ritd need to be outside of the createTree function. Want to put inside, but in case this need to be keep track for front end.
9661
let rtidCounter = 0;
9762

98-
export function createTree(
63+
export default function createTree(
9964
currentFiberNode: Fiber,
10065
circularComponentTable: Set<Fiber> = new Set(),
10166
tree: Tree = new Tree('root', 'root'),
@@ -366,114 +331,3 @@ export function createTree(
366331
// -------------RETURN THE TREE OUTPUT & PASS TO FRONTEND FOR RENDERING-------
367332
return tree;
368333
}
369-
370-
/**
371-
* linkFiber contains core module functionality, exported as an anonymous function, perform the following logic:
372-
* 1. Check if React Dev Tool is installed.
373-
* 2. Check if the target application (on the browser) is a valid react application.
374-
* 3. Initiate a event listener for visibility update of the target React Applicaiton.
375-
* 4. Obtain the initial fiberRootNode, which is the root node of a tree of React component.
376-
* 5. Initialize the tree snapShot on Chrome Extension.
377-
* 6. Monkey patching the onCommitFiberRoot from REACT DEV TOOL to obtain updated data after React Applicaiton is re-rendered.
378-
* @param snapShot The current snapshot
379-
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
380-
* @return a function to be invoked by index.js that initiates snapshot monitoring
381-
*/
382-
export default function linkFiber(snapShot: Snapshot, mode: Status): () => void {
383-
/**
384-
* A boolean value indicate if the target React Application is visible
385-
*/
386-
let isVisible: boolean = true;
387-
/**
388-
* The `fiberRootNode`, which is the root node of a tree of React component.
389-
* The `current` property of `fiberRoot` has data structure of a Tree, which can be used to traverse and obtain all child component data.
390-
*/
391-
let fiberRoot: FiberRoot;
392-
/**
393-
* @constant MIN_TIME_BETWEEN_UPDATE - The minimum time (ms) between each re-render/update of the snapShot tree being displayed on the Chrome Extension.
394-
*/
395-
const MIN_TIME_BETWEEN_UPDATE = 10;
396-
/**
397-
* @function throttledUpdateSnapshot - a function that will wait for at least MIN_TIME_BETWEEN_UPDATE ms, before updating the tree snapShot being displayed on the Chrome Extension.
398-
*/
399-
const throttledUpdateSnapshot = throttle((fiberRoot) => {
400-
updateSnapShotTree(snapShot, mode, fiberRoot);
401-
}, MIN_TIME_BETWEEN_UPDATE);
402-
403-
// Return a function to be invoked by index.js that initiates snapshot monitoring
404-
// TODO: Convert this into async/await & add try/catch
405-
406-
return () => {
407-
// -------------------CHECK REACT DEVTOOL INSTALLATION----------------------
408-
// react devtools global hook is a global object that was injected by the React Devtools content script, allows access to fiber nodes and react version
409-
// Obtain React Devtools Object:
410-
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
411-
// If React Devtools is not installed, object will be undefined.
412-
if (!devTools) {
413-
return;
414-
}
415-
// If React Devtools is installed, send a message to front end.
416-
window.postMessage(
417-
{
418-
action: 'devToolsInstalled',
419-
payload: 'devToolsInstalled',
420-
},
421-
'*',
422-
);
423-
424-
// --------------------CHECK VALID REACT APPLICATION------------------------
425-
// Obtain React Application information:
426-
const reactInstance = devTools.renderers.get(1);
427-
// If target application is not a React App, this will return undefined.
428-
if (!reactInstance) {
429-
return;
430-
}
431-
// If target application is a React App, send a message to front end.
432-
window.postMessage(
433-
{
434-
action: 'aReactApp',
435-
payload: 'aReactApp',
436-
},
437-
'*',
438-
);
439-
// --------------INITIATE EVENT LISTENER FOR VISIBILITY CHANGE--------------
440-
/**
441-
* Initiate an event listener for when there is a change to the visibility of the react target application (the browser tab)
442-
* @example If tic-tac-toe demo app is loaded on a tab with localhost:8080, whenever user switch tab or switch to another software => 'visibilityChange' => invoke the callback to update doWork boolean value
443-
*/
444-
document.addEventListener('visibilitychange', () => {
445-
// Hidden property = background tab/minimized window
446-
isVisible = !document.hidden;
447-
});
448-
449-
// ---------OBTAIN THE INITIAL FIBEROOTNODE FROM REACT DEV TOOL-------------
450-
// Obtain the FiberRootNode, which is the first value in the FiberRoot Set:
451-
fiberRoot = devTools.getFiberRoots(1).values().next().value;
452-
console.log('linkFiber', { fiberRoot });
453-
454-
// ----------INITIALIZE THE TREE SNAP SHOT ON CHROME EXTENSION--------------
455-
throttledUpdateSnapshot(fiberRoot); // only runs on start up
456-
457-
// --------MONKEY PATCHING THE onCommitFiberRoot FROM REACT DEV TOOL--------
458-
// React has inherent methods that are called with react fiber
459-
// One of which is the onCommitFiberRoot method, which is invoked after the React application re-render its component(s).
460-
// we attach new functionality without compromising the original work that onCommitFiberRoot does
461-
/**
462-
* @param onCommitFiberRoot - is a callback provided by React that is automatically invoked by React Fiber after the target React application re-renders its components. This callback is used by REACT DEV TOOL to receive updated data about the component tree and its state. See {@link https://medium.com/@aquinojardim/react-fiber-reactime-4-0-f200f02e7fa8}
463-
* @returns an anonymous function, which will have the same parameters as onCommitFiberRoot and when invoked will update the fiberRoot value & post a request to update the snapShot tree on Chrome Extension
464-
*/
465-
function addOneMoreStep(onCommitFiberRoot: DevTools['onCommitFiberRoot']) {
466-
return function (...args: Parameters<typeof onCommitFiberRoot>) {
467-
// eslint-disable-next-line prefer-destructuring
468-
// Obtain the updated FiberRootNode, after the target React application re-renders
469-
fiberRoot = args[1];
470-
// If the target React application is visible, send a request to update the snapShot tree displayed on Chrome Extension
471-
if (isVisible) throttledUpdateSnapshot(fiberRoot);
472-
// After our added work is completed we invoke the original onComitFiberRoot function
473-
return onCommitFiberRoot(...args);
474-
};
475-
}
476-
// Money Patching the onCommitFiberRoot method from REACT DEV TOOL
477-
devTools.onCommitFiberRoot = addOneMoreStep(devTools.onCommitFiberRoot);
478-
};
479-
}

src/backend/statePropsExtractors.ts renamed to src/backend/controllers/createTree/statePropExtractors.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
HookStates,
77
// object with tree structure
88
Fiber,
9-
} from './types/backendTypes';
9+
} from '../../types/backendTypes';
1010
// TODO: Determine what Component Data Type we are sending back for state, context, & props
1111
type ReactimeData = {
1212
[key: string]: any;
@@ -126,8 +126,8 @@ export function filterAndFormatData(
126126
}
127127
// ------------------------FILTER STATE & CONTEXT DATA--------------------------
128128
/**
129-
* This function is used to trim the state data of a component.
130-
* All propperty name that are in the central exclude list would be trimmed off.
129+
* This function is used to filter the state data of a component.
130+
* All propperty name that are in the central `exclude` list would be filtered out.
131131
* If passed in memoizedState is a not an object (a.k.a a primitive type), a default name would be provided.
132132
* @param memoizedState - The current state of the component associated with the current Fiber node.
133133
* @param _debugHookTypes - An array of hooks used for debugging purposes.

src/backend/helpers.ts renamed to src/backend/controllers/throttle.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77
/* eslint-disable linebreak-style */
88
/* eslint-disable no-inner-declarations, no-loop-func */
99

10-
import { string } from 'prop-types';
11-
1210
// eslint-disable-next-line import/newline-after-import
1311

14-
1512
/**
1613
* @method throttle
1714
* @param callback A function to throttle
1815
* @param MIN_TIME_BETWEEN_UPDATE A number of milliseconds to use as throttling interval
1916
* @returns A function that limits input function, `callback`, from being called more than once every `MIN_TIME_BETWEEN_UPDATE` milliseconds
2017
*
2118
*/
22-
export const throttle = (
19+
export default function throttle(
2320
callback: (...args: any) => void,
2421
MIN_TIME_BETWEEN_UPDATE: number,
25-
): Function => {
22+
): Function {
2623
// Initialize boolean flags for callback, throttledFunc
2724
/**
2825
* A boolean variable tracking if MIN_TIME_BETWEEN_UPDATE has passed
@@ -91,5 +88,4 @@ export const throttle = (
9188
}
9289
}
9390
};
94-
};
95-
91+
}

src/backend/timeJump.ts renamed to src/backend/controllers/timeJump.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import routes from './routes';
1+
import routes from '../models/routes';
22

33
/* eslint-disable @typescript-eslint/no-unused-vars */
44
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
55
/* eslint-disable max-len */
66
/* eslint-disable no-param-reassign */
7-
import componentActionsRecord from './masterState';
7+
import componentActionsRecord from '../models/masterState';
88
const circularComponentTable = new Set();
99

1010
/**

src/backend/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
*/
1010
// regenerator runtime supports async functionality
1111
import 'regenerator-runtime/runtime';
12-
import linkFiberStart from './linkFiber';
13-
import timeJumpStart from './timeJump';
12+
import linkFiberStart from './routers/linkFiber';
13+
import timeJumpStart from './controllers/timeJump';
1414
import { Snapshot, Status, MsgData } from './types/backendTypes';
1515

1616
// * State snapshot object initialized here
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)