Skip to content

Commit 99c4439

Browse files
committed
Documenting linkFiber & tree files
1 parent 39f08ea commit 99c4439

File tree

3 files changed

+100
-16
lines changed

3 files changed

+100
-16
lines changed

src/backend/linkFiber.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,26 @@ function createTree(
391391
}
392392
return tree;
393393
}
394+
type version = undefined | { version: string };
395+
/**
396+
* @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.
397+
*/
398+
interface DevTools {
399+
/**
400+
* @property renderers - an Map object containing information about the React renders that are currently active on the page. The react version being used can be obtained at key = 1.
401+
*/
402+
renderers: Map<1, undefined | { version: string }>;
403+
/**
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.
407+
*/
408+
getFiberRoots: (renderID: number) => Set<number>;
409+
410+
onCommitFiberRoot: any;
411+
}
412+
413+
interface ReactInstance {}
394414

395415
/**
396416
* @method linkFiber
@@ -400,31 +420,34 @@ function createTree(
400420
* linkFiber contains core module functionality, exported as an anonymous function.
401421
*/
402422
export default (snap: Snapshot, mode: Mode): (() => void) => {
403-
// checks for visiblity of document
404-
function onVisibilityChange(): void {
405-
// hidden property = background tab/minimized window
423+
// Checks for visiblity of document
424+
function onVisibilityChange() {
425+
// Hidden property = background tab/minimized window
406426
doWork = !document.hidden;
407427
}
408428
return () => {
409429
// 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
410-
const devTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
430+
const devTools: undefined | DevTools = window.__REACT_DEVTOOLS_GLOBAL_HOOK__;
431+
console.log('linkFiber.ts', { devTools });
411432
// check if reactDev Tools is installed
412433
if (!devTools) {
413434
return;
414435
}
436+
// if reactDev Tools is installed, send a message to front end
415437
window.postMessage(
416438
{
417439
action: 'devToolsInstalled',
418440
payload: 'devToolsInstalled',
419441
},
420442
'*',
421443
);
422-
// reactInstance returns an object of the react, 1st element in map
444+
// Obtain reaction application information. If target application is not a React App, this will return null.
423445
const reactInstance = devTools.renderers.get(1);
424446
// if no React Instance found then target is not a compatible app
425447
if (!reactInstance) {
426448
return;
427449
}
450+
// we may want to add try/catch
428451
window.postMessage(
429452
{
430453
action: 'aReactApp',
@@ -439,7 +462,9 @@ export default (snap: Snapshot, mode: Mode): (() => void) => {
439462
document.addEventListener('visibilitychange', onVisibilityChange);
440463

441464
if (reactInstance && reactInstance.version) {
465+
// Obtain the fiber
442466
fiberRoot = devTools.getFiberRoots(1).values().next().value;
467+
console.log('LinkFiber', { fiberRoot });
443468
// React has inherent methods that are called with react fiber
444469
// we attach new functionality without compromising the original work that onCommitFiberRoot does
445470
const addOneMoreStep = function (original) {

src/backend/tree.ts

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

9-
let copyInstances = 0;
10-
const circularComponentTable = new Set<Tree>();
11-
let componentNames = {};
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
11+
const circularComponentTable = new Set<Tree>(); // Keeps track of the nodes added to the tree
12+
let componentNames = {}; // {componentName: frequency of use} => component name as a key and it's frequency of use as its value
1213

13-
// Removes unserializable state data such as functions
14+
// Functions dont serialize properly so we need to scrub for that
1415
function scrubUnserializableMembers(tree: Tree): Tree {
1516
Object.entries(tree.state).forEach((keyValuePair) => {
1617
if (typeof keyValuePair[1] === 'function') tree.state[keyValuePair[0]] = 'function';
1718
});
1819
return tree;
1920
}
2021

22+
// Making a deep clone of an object
2123
function serializeState(state) {
2224
try {
25+
// makes a deep clone, but this way can be very slow
2326
return JSON.parse(JSON.stringify(state));
2427
} catch (e) {
2528
return 'circularState';
@@ -29,9 +32,11 @@ function serializeState(state) {
2932
/**
3033
* This is the current snapshot that is being sent to the snapshots array.
3134
* Creates a Tree
32-
* @param state : the tree's current state
33-
* @param name : the tree's name
34-
* @param componentData : Data in the component tree
35+
* @param state - {string| {}} - the tree's current state
36+
* @param name - {string} - the tree's name
37+
* @param componentData - {props: {}} - Data in the component tree
38+
* @param chilren - {(Tree | string)[]} - An array of children nodes
39+
* @param parent - {Tree} - the parent node
3540
* @parent generates a new tree (recursive call)
3641
*/
3742
class Tree {
@@ -77,37 +82,54 @@ class Tree {
7782
this.rtid = rtid;
7883
}
7984

80-
// Returns a unique name ready to be used
85+
// Returns a unique name ready to be used for when new components gets added to the tree
8186
checkForDuplicates(name: string): string {
8287
// check for empty name
8388
if (name === '' && typeof this.rtid === 'string') {
8489
name = this.rtid.replace('fromLinkFiber', '');
8590
}
91+
// if we are at root, then make sure componentNames is an empty
8692
if (this.state === 'root') componentNames = {};
8793
// check for duplicate
8894
else if (componentNames[name] !== undefined) {
89-
const count = componentNames[name] + 1;
90-
const newName = name + count;
95+
// if name exists in componentNames object, grab count and add 1
96+
const count: number = componentNames[name] + 1;
97+
// declare a new name variable
98+
const newName: string = name + count;
99+
// update name count in object
91100
componentNames[name] = count;
101+
// return newName
92102
return newName;
93103
}
104+
// add name in componentsName with value of 0
94105
componentNames[name] = 0;
106+
// return name
95107
return name;
96108
}
97109

98110
addChild(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
111+
// gets unique name by calling checkForDuplicates method
99112
const uniqueName = this.checkForDuplicates(name);
113+
// instantiates new child Tree with state, uniqueName, componentData and rtid
100114
const newChild: Tree = new Tree(state, uniqueName, componentData, rtid);
115+
// updating newChild parent to "this"
101116
newChild.parent = this;
117+
// adds newChild to children array
102118
this.children.push(newChild);
119+
// return newChild
103120
return newChild;
104121
}
105122

106123
addSibling(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
124+
// gets unique name by calling checkForDuplicates method
107125
const uniqueName = this.checkForDuplicates(name);
126+
// instantiate new sibilng tree with state, uniqueName, componentName and rtid
108127
const newSibling: Tree = new Tree(state, uniqueName, componentData, rtid);
128+
// updating newSibling parent to be the parent of "this" which refers to sibling node
109129
newSibling.parent = this.parent;
130+
// adds newSibling to children array
110131
this.parent.children.push(newSibling);
132+
// return newSibling
111133
return newSibling;
112134
}
113135

@@ -119,14 +141,18 @@ class Tree {
119141
* @object circularComponentTable : Clears circular component table only on first call, not recursive ones
120142
*
121143
*/
144+
// if we havent made a copy of the tree, increment copyInstances and clear cicularComponentTable set
122145
if (copyInstances === 0) {
123146
copyInstances++;
124147
circularComponentTable.clear();
125148
}
126149
// creates copy of present node
127150
let copy: Tree = new Tree(this.state, this.name, this.componentData, this.rtid);
151+
// you want to get rid of the parentNode
128152
delete copy.parent;
153+
// add to circulareComponentTable
129154
circularComponentTable.add(this);
155+
//
130156
copy = scrubUnserializableMembers(copy);
131157

132158
// creates copy of each child of the present node

src/backend/types/backendTypes.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,56 @@
33
/* eslint-disable @typescript-eslint/no-explicit-any */
44
import Tree from '../tree';
55

6+
/**
7+
* The snapshot of the current tree
8+
* @param tree - {Tree} - The tree structure to send to front end
9+
* @param unfilteredTree - {null} - The current mode (i.e. jumping, time-traveling, or paused)
10+
*/
611
export interface Snapshot {
712
tree: Tree;
813
unfilteredTree: null;
914
}
1015

16+
/**
17+
* Where we
18+
* @param jumping - whether we are jumping steps by
19+
* @param paused - true/false for whether pausing to see the state
20+
*/
1121
export interface Mode {
1222
jumping: boolean;
1323
paused: boolean;
1424
}
1525

26+
/**
27+
*
28+
*/
1629
export interface SnapshotNode {
1730
name: string;
1831
state: {
1932
location?: any;
2033
};
2134
children: any[];
2235
}
23-
36+
/**
37+
* @param data - an object with action & payload properties
38+
*/
2439
export interface MsgData {
2540
data: {
2641
action: string;
2742
payload: any;
2843
};
2944
}
3045

46+
/**
47+
*
48+
* @param index -
49+
* @param hooksIndex -
50+
* @param actualDuration -
51+
* @param actualStartTime -
52+
* @param selfBaseDuration -
53+
* @param treeBaseDuration -
54+
* @param props -
55+
*/
3156
export interface ComponentData {
3257
index?: number;
3358
hooksIndex?: number;
@@ -38,11 +63,19 @@ export interface ComponentData {
3863
props?: any;
3964
}
4065

66+
/**
67+
* @param state -
68+
* @param component -
69+
*/
4170
export interface HookStateItem {
4271
state: any;
4372
component: any;
4473
}
4574

75+
/**
76+
* HookeStates is an array of HookeStateItem
77+
* Each HookStateItem is an object with state & component properties
78+
*/
4679
export type HookStates = Array<HookStateItem>;
4780

4881
export interface State {

0 commit comments

Comments
 (0)