Skip to content

Commit 8a83b97

Browse files
committed
Working on linkFiber documentation
1 parent 09ca488 commit 8a83b97

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

src/backend/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ const linkFiber = linkFiberInitialization(snapShot, mode);
2828
const timeJump = timeJumpInitialization(mode);
2929

3030
/**
31-
* Invoke linkFiber to perform the follwoing:
31+
* Invoke linkFiber to perform the following:
3232
* 1. Check for ReactDev installation, valid target React App
33-
* 2. Obtain the intial ReactFiber Tree from target React App
33+
* 2. Obtain the initial ReactFiber Tree from target React App
3434
* 3. Send a snapshot of ReactFiber Tree to frontend/Chrome Extension
3535
*/
3636
linkFiber();
3737

38-
// -----------------SET UP EVENT LISTENER FOR TIME TRAVEL-----------------------
38+
// --------------INITIALIZE EVENT LISTENER FOR TIME TRAVEL----------------------
3939
/**
40-
* On the chrome extension, if user click left/right arrow or the play button (a.k.a time travel functionality), frontend will send a message jumpToSnap with payload of the cached snapShot tree at the current step
40+
* On the chrome extension, if user click left/right arrow or the play button (a.k.a time travel functionality), frontend will send a message `jumpToSnap` with payload of the cached snapShot tree at the current step
4141
* 1. Set jumping mode to true => dictate we are jumping => no new snapshot will be sent to frontend
4242
* 2. If navigate to a new route during jumping => cache timeJump in navigate.
43-
* Otherwise, invoke timeJump to update ReactFiber tree with cached data from the snapshot payload
43+
* 3. If not navigate during jumping => invoke timeJump to update ReactFiber tree with cached data from the snapshot payload
4444
*/
4545
window.addEventListener('message', async ({ data: { action, payload } }: MsgData) => {
4646
switch (action) {

src/backend/routers/linkFiber.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Snapshot, Status, FiberRoot } from '../types/backendTypes';
22
import { DevTools } from '../types/linkFiberTypes';
33
import updateAndSendSnapShotTree from './snapShot';
4-
5-
// 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
6-
// getHooksNames - helper function to grab the getters/setters from `elementType`
74
import throttle from '../controllers/throttle';
85
import componentActionsRecord from '../models/masterState';
96
import createComponentActionsRecord from '../controllers/createComponentActionsRecord';
@@ -19,23 +16,23 @@ declare global {
1916
/**
2017
* @constant MIN_TIME_BETWEEN_UPDATE - The minimum time (ms) between each re-render/update of the snapShot tree being displayed on the Chrome Extension.
2118
*/
22-
const MIN_TIME_BETWEEN_UPDATE = 10;
19+
const MIN_TIME_BETWEEN_UPDATE = 70;
2320
/**
2421
* @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.
22+
* @param fiberRoot - the root of ReactFiber Tree
23+
* @param mode - mode is jumping/not jumping or navigating during jumping
24+
* @param snapShot - the tree snapshot to send to Front End or obtained from Front End during timeJump
2525
*/
2626
const throttledUpdateSnapshot = throttle(
2727
async (fiberRoot: FiberRoot, mode: Status, snapShot: Snapshot) => {
28-
// console.log('linkFiber - RERENDER');
2928
// If not jumping
3029
if (!mode.jumping) {
31-
// console.log('linkFiber - SEND SNAPSHOT');
3230
// Update and Send SnapShot tree to front end
3331
updateAndSendSnapShotTree(snapShot, fiberRoot);
3432
}
3533

3634
// If navigating to another route during jumping:
3735
else if (mode.navigating) {
38-
// console.log('linkFiber - NAVIGATING');
3936
// Reset the array containing update methods:
4037
componentActionsRecord.clear();
4138
// Obtain new update methods for the current route:
@@ -44,11 +41,7 @@ const throttledUpdateSnapshot = throttle(
4441
// Invoke timeJump, which is stored in mode.navigating, to update React Application FiberTree based on the snapshotTree
4542
await mode.navigating();
4643
}
47-
48-
// Else:
49-
// else {
50-
// console.log('linkFiber - REACT FIBER TREE UPDATED');
51-
// }
44+
// NOTE: if not navigating during jumping, timeJump is invoked in index.ts file.
5245
},
5346
MIN_TIME_BETWEEN_UPDATE,
5447
);
@@ -57,18 +50,16 @@ const throttledUpdateSnapshot = throttle(
5750
* @function linkFiber - linkFiber contains core module functionality, exported as an anonymous function, perform the following logic:
5851
* 1. Check if React Dev Tool is installed.
5952
* 2. Check if the target application (on the browser) is a valid react application.
60-
* 3. Initiate a event listener for visibility update of the target React Applicaiton.
53+
* 3. Initiate a event listener for visibility update of the target React Application.
6154
* 4. Obtain the initial fiberRootNode, which is the root node of the fiber tree
62-
* 5. Initialize the fiber tree snapShot on Chrome Extension.
55+
* 5. Initialize the fiber tree snapShot to send to Front End, later rendered on Chrome Extension.
6356
* 6. Monkey patching the onCommitFiberRoot from REACT DEV TOOL to obtain updated data after React Applicaiton is re-rendered.
6457
* @param snapShot The current snapshot (i.e fiber tree)
6558
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
6659
* @return a function to be invoked by index.js that initiates snapshot monitoring
6760
*/
6861
export default function linkFiber(snapShot: Snapshot, mode: Status): () => Promise<void> {
69-
/**
70-
* A boolean value indicate if the target React Application is visible
71-
*/
62+
/** A boolean value indicate if the target React Application is visible */
7263
let isVisible: boolean = true;
7364
/**
7465
* Every React application has one or more DOM elements that act as containers. React creates a fiber root object for each of those containers.
@@ -78,18 +69,13 @@ export default function linkFiber(snapShot: Snapshot, mode: Status): () => Promi
7869
let fiberRoot: FiberRoot;
7970

8071
// Return a function to be invoked by index.js that initiates snapshot monitoring
81-
// TODO: Convert this into async/await & add try/catch
82-
83-
return async () => {
72+
return async function linkFiberInitialization() {
8473
// -------------------CHECK REACT DEVTOOL INSTALLATION----------------------
8574
// 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
8675
// Obtain React Devtools Object:
8776
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
88-
// console.log('React Dev Tools:', devTools);
8977
// If React Devtools is not installed, object will be undefined.
90-
if (!devTools) {
91-
return;
92-
}
78+
if (!devTools) return;
9379
// If React Devtools is installed, send a message to front end.
9480
window.postMessage(
9581
{
@@ -127,7 +113,7 @@ export default function linkFiber(snapShot: Snapshot, mode: Status): () => Promi
127113
// ---------OBTAIN THE INITIAL FIBEROOTNODE FROM REACT DEV TOOL-------------
128114
// Obtain the FiberRootNode, which is the first value in the FiberRoot Set:
129115
fiberRoot = devTools.getFiberRoots(1).values().next().value;
130-
// console.log('fiberRoot', fiberRoot);
116+
131117
// ----------INITIALIZE THE TREE SNAP SHOT ON CHROME EXTENSION--------------
132118
await throttledUpdateSnapshot(fiberRoot, mode, snapShot); // only runs on start up
133119

0 commit comments

Comments
 (0)