Skip to content

Commit 19c3721

Browse files
committed
Add documentation for snapShot file
1 parent 4f5e617 commit 19c3721

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

src/backend/linkFiber.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ type ComponentData = {
8282

8383
let rtidCounter = 0;
8484

85-
8685
/**
8786
* @function traverseHooks
8887
* @param memoizedState - The current state of the component associated with the current Fiber node.
@@ -106,7 +105,6 @@ function traverseHooks(memoizedState: any): HookStates {
106105
return hooksStates;
107106
}
108107

109-
// This runs after every Fiber commit. It creates a new snapshot
110108
const exclude = new Set([
111109
'alternate',
112110
'basename',
@@ -365,7 +363,7 @@ export function createTree(
365363
context: {},
366364
state: {},
367365
};
368-
let componentFound = false;
366+
let isStatefulComponent = false;
369367

370368
// ----------------APPEND PROP DATA FROM REACT DEV TOOL-----------------------
371369
// check to see if the parent component has any state/props
@@ -431,7 +429,7 @@ export function createTree(
431429
componentData.index = componentActionsRecord.saveNew(stateNode.state, stateNode);
432430
// passess to front end
433431
newState = stateNode.state;
434-
componentFound = true;
432+
isStatefulComponent = true;
435433
}
436434

437435
// REGULAR REACT HOOKS
@@ -463,15 +461,15 @@ export function createTree(
463461
newState.hooksState = [];
464462
}
465463
newState.hooksState.push({ [hooksNames[i]]: state.state });
466-
componentFound = true;
464+
isStatefulComponent = true;
467465
});
468466
// console.log({ newState: newState.hooksState });
469467
}
470468
}
471469

472470
// This grabs stateless components
473471
if (
474-
!componentFound &&
472+
!isStatefulComponent &&
475473
(tag === FunctionComponent || tag === ClassComponent || tag === IndeterminateComponent)
476474
) {
477475
newState = 'stateless';
@@ -500,7 +498,7 @@ export function createTree(
500498
let rtid: string | null = null;
501499
// We want to add this fiber node to the snapshot
502500
// if (componentFound || (newState === 'stateless' && !newState.hooksState)) {
503-
if (componentFound || newState === 'stateless') {
501+
if (isStatefulComponent || newState === 'stateless') {
504502
// Grab JSX Component & replace the 'fromLinkFiber' class value
505503
if (currentFiberNode.child?.stateNode?.setAttribute) {
506504
rtid = `fromLinkFiber${rtidCounter}`;

src/backend/snapShot.ts

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,43 @@ import Tree from './tree';
55
import componentActionsRecord from './masterState';
66
import routes from './routes';
77
import { createTree } from './linkFiber';
8+
9+
// ---------------------------UPDATE TREE SNAP SHOT-----------------------------
10+
/**
11+
* - Create a new `snapShot` tree with the provided `fiberRoot`. This runs after every Fiber commit.
12+
* - Middleware: Updates snapShot object with latest snapshot, using `sendSnapshot`
13+
* @param snapShot The current snapshot
14+
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
15+
* @param fiberRoot The `fiberRootNode`, which is the root node of a tree of React component. The `current` property of `fiberRoot` has data structure of a Tree, which can be used to traverse and obtain all child component data.
16+
*/
17+
// updating tree depending on current mode on the panel (pause, etc)
18+
export default function updateSnapShotTree(
19+
snapShot: Snapshot,
20+
mode: Status,
21+
fiberRoot: FiberRoot,
22+
): void {
23+
console.log('snapShot.ts - Update');
24+
// this is the currently active root fiber(the mutable root of the tree)
25+
const { current } = fiberRoot;
26+
componentActionsRecord.clear();
27+
// creates snapshot that is a tree based on properties in fiberRoot object
28+
snapShot.tree = createTree(current);
29+
// sends the updated tree back
30+
sendSnapshot(snapShot, mode);
31+
}
32+
33+
// -------------------SEND TREE SNAP SHOT TO FRONT END--------------------------
834
/**
9-
* @method sendSnapshot
35+
* Gets a copy of the current snapShot.tree and posts a recordSnap message to the window
1036
* @param snapShot The current snapshot
1137
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
12-
* @return Nothing.
38+
* @return void
1339
*
14-
* Middleware: Gets a copy of the current snapShot.tree and posts a recordSnap message to the window
1540
*/
1641
function sendSnapshot(snapShot: Snapshot, mode: Status): void {
1742
// Don't send messages while jumping or while paused
1843
if (mode.jumping || mode.paused) return;
19-
// If there is no current tree creates a new one
20-
if (!snapShot.tree) {
21-
snapShot.tree = new Tree('root', 'root');
22-
}
44+
2345
// Make a deep copy of the tree:
2446
const payload = snapShot.tree.cleanTreeCopy();
2547
payload.route = routes.addRoute(window.location.href);
@@ -38,27 +60,3 @@ function sendSnapshot(snapShot: Snapshot, mode: Status): void {
3860
'*',
3961
);
4062
}
41-
42-
/**
43-
* @function updateSnapShotTree
44-
* @param snapShot The current snapshot
45-
* @param mode The current mode (i.e. jumping, time-traveling, or paused)
46-
* Middleware: Updates snapShot object with latest snapshot, using @sendSnapshot
47-
*/
48-
// updating tree depending on current mode on the panel (pause, etc)
49-
export default function updateSnapShotTree(
50-
snapShot: Snapshot,
51-
mode: Status,
52-
fiberRoot: FiberRoot,
53-
): void {
54-
console.log('snapShot - Update');
55-
// this is the currently active root fiber(the mutable root of the tree)
56-
if (fiberRoot) {
57-
const { current } = fiberRoot;
58-
// creates snapshot that is a tree based on properties in fiberRoot object
59-
componentActionsRecord.clear();
60-
snapShot.tree = createTree(current);
61-
}
62-
// sends the updated tree back
63-
sendSnapshot(snapShot, mode);
64-
}

0 commit comments

Comments
 (0)