Skip to content

Commit 568881c

Browse files
committed
Adding comments on linkFiber & BackendTypes
1 parent 99c4439 commit 568881c

File tree

4 files changed

+9599
-9168
lines changed

4 files changed

+9599
-9168
lines changed

src/backend/linkFiber.ts

Lines changed: 79 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,11 @@ import { throttle, getHooksNames } from './helpers';
3535
// Set global variables to use in exported module and helper functions
3636
declare global {
3737
interface Window {
38-
__REACT_DEVTOOLS_GLOBAL_HOOK__?: any;
38+
__REACT_DEVTOOLS_GLOBAL_HOOK__?: DevTools;
3939
__REDUX_DEVTOOLS_EXTENSION__?: any;
4040
}
4141
}
42+
4243
let fiberRoot = null;
4344
let doWork = true;
4445
const circularComponentTable = new Set();
@@ -84,7 +85,6 @@ function sendSnapshot(snap: Snapshot, mode: Mode): void {
8485
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
8586
* Middleware: Updates snap object with latest snapshot, using @sendSnapshot
8687
*/
87-
8888
// updating tree depending on current mode on the panel (pause, etc)
8989
function updateSnapShotTree(snap: Snapshot, mode: Mode): void {
9090
// this is the currently active root fiber(the mutable root of the tree)
@@ -135,60 +135,60 @@ function traverseHooks(memoizedState: any): HookStates {
135135
* 3. Build a new state snapshot
136136
*/
137137
// This runs after every Fiber commit. It creates a new snapshot
138-
const exclude = [
138+
const exclude = new Set([
139139
'alternate',
140-
'_owner',
141-
'_store',
142-
'get key',
143-
'ref',
144-
'_self',
145-
'_source',
146-
'firstBaseUpdate',
147-
'updateQueue',
148-
'lastBaseUpdate',
149-
'shared',
150-
'responders',
151-
'pending',
152-
'lanes',
153-
'childLanes',
154-
'effects',
155-
'memoizedState',
156-
'pendingProps',
157-
'lastEffect',
158-
'firstEffect',
159-
'tag',
160-
'baseState',
161140
'baseQueue',
162-
'dependencies',
141+
'baseState',
142+
'child',
143+
'childLanes',
144+
'children',
163145
'Consumer',
164146
'context',
165-
'_currentRenderer',
166-
'_currentRenderer2',
167-
'mode',
168-
'flags',
169-
'nextEffect',
170-
'sibling',
171147
'create',
172148
'deps',
173-
'next',
149+
'dependencies',
174150
'destroy',
175-
'parentSub',
176-
'child',
151+
'effects',
152+
'elementType',
153+
'firstBaseUpdate',
154+
'firstEffect',
155+
'flags',
156+
'get key',
177157
'key',
158+
'lanes',
159+
'lastBaseUpdate',
160+
'lastEffect',
161+
'memoizedState',
162+
'mode',
163+
'next',
164+
'nextEffect',
165+
'pending',
166+
'parentSub',
167+
'pendingProps',
168+
'Provider',
169+
'updateQueue',
170+
'ref',
171+
'responders',
178172
'return',
179-
'children',
180-
'$$typeof',
181-
'_threadCount',
173+
'shared',
174+
'sibling',
175+
'stateNode',
176+
'tag',
177+
'type',
182178
'_calculateChangedBits',
179+
'_context',
180+
'_currentRenderer',
181+
'_currentRenderer2',
183182
'_currentValue',
184183
'_currentValue2',
185-
'Provider',
186-
'_context',
187-
'stateNode',
188-
'elementType',
189-
'type',
190-
];
191-
184+
'_owner',
185+
'_self',
186+
'_source',
187+
'_store',
188+
'_threadCount',
189+
'$$typeof',
190+
]);
191+
// -------------------CONVERT PROPT DATA TO STRING------------------------------
192192
// This recursive function is used to grab the state of children components
193193
// and push them into the parent componenent
194194
// react elements throw errors on client side of application - convert react/functions into string
@@ -197,22 +197,24 @@ function convertDataToString(newObj, oldObj, depth = 0) {
197197
for (const key in newObj) {
198198
if (typeof newObj[key] === 'function') {
199199
newPropData[key] = 'function';
200-
} else if (exclude.includes(key) === true) {
200+
} else if (exclude.has(key)) {
201201
newPropData[key] = 'reactFiber';
202202
return newPropData;
203-
} else if (typeof newObj[key] === 'object' && exclude.includes(key) !== true) {
203+
} else if (typeof newObj[key] === 'object' && !exclude.has(key)) {
204204
newPropData[key] =
205205
depth > 10
206206
? 'convertDataToString reached max depth'
207207
: convertDataToString(newObj[key], null, depth + 1);
208-
} else if (exclude.includes(key) !== true) {
208+
} else if (!exclude.has(key)) {
209209
newPropData[key] = newObj[key];
210210
}
211211
}
212212
return newPropData;
213213
}
214-
// Every time a state change is made in the accompanying app, the extension creates a
215-
// Tree “snapshot” of the current state, and adds it to the current “cache” of snapshots in the extension
214+
// -------------------------CREATE TREE TO SEND TO FRONT END--------------------
215+
/**
216+
* Every time a state change is made in the accompanying app, the extension creates a Tree “snapshot” of the current state, and adds it to the current “cache” of snapshots in the extension
217+
*/
216218
function createTree(
217219
currentFiber: Fiber,
218220
tree: Tree = new Tree('root', 'root'),
@@ -240,14 +242,15 @@ function createTree(
240242
_debugHookTypes,
241243
} = currentFiber;
242244

243-
// check to see if we can get the information we were looking for
244-
// need to figure out what tag is
245+
// Tag === 5 signify this is a React Fragment. Each JSX component return a React fragment
245246
if (tag === 5) {
246247
try {
248+
// Ensure parent component has memoizedProps property
247249
if (
248250
memoizedProps.children &&
249251
memoizedProps.children[0]?._owner?.memoizedProps !== undefined
250-
) {
252+
) {
253+
// Access the memoizedProps of the parent component
251254
const propsData = memoizedProps.children[0]._owner.memoizedProps;
252255
const newPropData = convertDataToString(
253256
propsData,
@@ -391,7 +394,6 @@ function createTree(
391394
}
392395
return tree;
393396
}
394-
type version = undefined | { version: string };
395397
/**
396398
* @interface DevTools - A global object provided by the React Developer Tools extension. It provides a set of methods that allow developers to inspect and manipulate React components in the browser.
397399
*/
@@ -401,17 +403,22 @@ interface DevTools {
401403
*/
402404
renderers: Map<1, undefined | { version: string }>;
403405
/**
404-
* @method getFiberRoots - a method that return a fiber root {set}
405-
* @param renderID - the ID of the node???
406-
* @return A set of fiber root.
406+
* @method getFiberRoots - get the Set of fiber roots that are currently mounted for the given rendererID. If not found, initalize a new empty Set at renderID key.
407+
* @param renderID - a unique identifier for a specific instance of a React renderer. When a React application is first mounted, it will receive a rendererID. This rendererID will remain the same for the entire lifecycle of the application, even if the state is updated and the components are re-rendered/unmounted/added. However, if the application is unmounted and re-mounted again, it will receive a new rendererID.
408+
* @return A set of fiberRoot.
407409
*/
408-
getFiberRoots: (renderID: number) => Set<number>;
410+
getFiberRoots: (rendererID: number) => Set<number>;
409411

410-
onCommitFiberRoot: any;
412+
/**
413+
* @method onCommitFiberRoot - After the state of a component in a React Application is updated, the virtual DOM will be updated. When a render has been commited for a root, onCommitFiberRoot will be invoked to determine if the component is being mounted, updated, or unmounted. After that, this method will send update information to the React DevTools to update its UI to reflect the change.
414+
* @param rendererID - a unique identifier for a specific instance of a React renderer
415+
* @param root - root of the rendered tree (a.k.a the root of the React Application)
416+
* @param priorityLevel
417+
* @return void
418+
*/
419+
onCommitFiberRoot: (rendererID: number, root: any, priorityLevel: any) => void;
411420
}
412421

413-
interface ReactInstance {}
414-
415422
/**
416423
* @method linkFiber
417424
* @param snap The current snapshot
@@ -426,28 +433,33 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
426433
doWork = !document.hidden;
427434
}
428435
return () => {
436+
// -------------------CHECK REACT DEVTOOL INSTALLATION----------------------
429437
// 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
430-
const devTools: undefined | DevTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
438+
// Obtain React Devtools Object:
439+
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
431440
console.log('linkFiber.ts', { devTools });
432-
// check if reactDev Tools is installed
441+
// If React Devtools is not installed, object will be undefined.
433442
if (!devTools) {
434443
return;
435444
}
436-
// if reactDev Tools is installed, send a message to front end
445+
// If React Devtools is installed, send a message to front end.
437446
window.postMessage(
438447
{
439448
action: 'devToolsInstalled',
440449
payload: 'devToolsInstalled',
441450
},
442451
'*',
443452
);
444-
// Obtain reaction application information. If target application is not a React App, this will return null.
453+
454+
// --------------------CHECK VALID REACT APPLICATION------------------------
455+
// Obtain React Application information:
445456
const reactInstance = devTools.renderers.get(1);
446-
// if no React Instance found then target is not a compatible app
457+
// If target application is not a React App, this will return undefined.
447458
if (!reactInstance) {
448459
return;
449460
}
450461
// we may want to add try/catch
462+
// If target application is a React App, send a message to front end.
451463
window.postMessage(
452464
{
453465
action: 'aReactApp',
@@ -462,9 +474,10 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
462474
document.addEventListener('visibilitychange', onVisibilityChange);
463475

464476
if (reactInstance && reactInstance.version) {
465-
// Obtain the fiber
477+
// Obtain the FiberRootNode, which is the first value in the FiberRoot Set:
466478
fiberRoot = devTools.getFiberRoots(1).values().next().value;
467479
console.log('LinkFiber', { fiberRoot });
480+
468481
// React has inherent methods that are called with react fiber
469482
// we attach new functionality without compromising the original work that onCommitFiberRoot does
470483
const addOneMoreStep = function (original) {

src/backend/tree.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
/* eslint-disable no-console */
77
/* eslint-disable no-param-reassign */
88

9-
// ~~ I dont like the fact that this are global variables ~~ - Zack
10-
let copyInstances = 0; // Tells you if we have already made a copy of current tree
9+
// ~~ I dont like the fact that these are global variables ~~ - Zack
10+
let copyInstances = 0; // Tells you if we have already made a copy of current tree??
1111
const circularComponentTable = new Set<Tree>(); // Keeps track of the nodes added to the tree
1212
let componentNames = {}; // {componentName: frequency of use} => component name as a key and it's frequency of use as its value
1313

@@ -88,7 +88,7 @@ class Tree {
8888
if (name === '' && typeof this.rtid === 'string') {
8989
name = this.rtid.replace('fromLinkFiber', '');
9090
}
91-
// if we are at root, then make sure componentNames is an empty
91+
// if we are at root, then make sure componentNames is an empty object
9292
if (this.state === 'root') componentNames = {};
9393
// check for duplicate
9494
else if (componentNames[name] !== undefined) {
@@ -148,9 +148,9 @@ class Tree {
148148
}
149149
// creates copy of present node
150150
let copy: Tree = new Tree(this.state, this.name, this.componentData, this.rtid);
151-
// you want to get rid of the parentNode
151+
// you want to get rid of the parentNode?? not sure why
152152
delete copy.parent;
153-
// add to circulareComponentTable
153+
// add to circularComponentTable
154154
circularComponentTable.add(this);
155155
//
156156
copy = scrubUnserializableMembers(copy);

src/backend/types/backendTypes.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,25 @@ export const Block = 22;
136136
export const OffscreenComponent = 23;
137137
export const LegacyHiddenComponent = 24;
138138

139+
/**
140+
* @type Fiber - The Fiber data structure that React uses to represent a component tree.
141+
* @member actualDuration - The time taken to render the current Fiber node and its descendants during the previous render cycle. This value is used to optimize the rendering of components and to provide performance metrics to developers.
142+
* @member actualStartTime - The time at which the rendering of the current Fiber node started during the previous render cycle.
143+
* @member child - Pointer to the first child.
144+
* @member dependencies - An array of values (such as state or props) that the current Fiber node depends on. This is used to determine whether the node needs to be re-rendered.
145+
* @member elementType - The type of the current Fiber node's element (e.g. the component function or class, or the DOM element type). Example: div, h1,
146+
* @member index - Index of the current Fiber node. Ex: if a div has 3 headings. The first child is heading with index = 0. The next sibling is a heading with index = 1 & the last sibling is a heading with index = 2.
147+
* @member key - Unique identifier of this child, used to identify the node when rendering lists of components.
148+
* @member memoizedProps - The current props of the component associated with the current Fiber node.
149+
* @member memoizedState - The current state of the component associated with the current Fiber node.
150+
* @member selfBaseDuration - The base duration of the current Fiber node's render phase (excluding the time taken to render its children). This field is only set when the enableProfilerTimer flag is enabled.
151+
* @member sibling - Pointer to next sibling
152+
* @member stateNode - The local state associated with this fiber.
153+
* @member tag - The type of the current Fiber node, such as FunctionComponent, ClassComponent, or HostComponent (for DOM elements).
154+
* @member treeBaseDuration - The total base duration of the current Fiber node's subtree. This field is only set when the enableProfilerTimer flag is enabled.
155+
* @member type - Same as elementType.
156+
* @member _debugHookTypes - An array of hooks used for debugging purposes.
157+
*/
139158
export type Fiber = {
140159
// Tag identifying the type of fiber.
141160
tag: WorkTag;

0 commit comments

Comments
 (0)