Skip to content

Commit 947a3aa

Browse files
authored
Merge pull request #2 from oslabs-beta/chris-10-29
10/31 added helpful comments to LinkFiber and Tree
2 parents d6f4c40 + cf1461f commit 947a3aa

File tree

4 files changed

+53
-32
lines changed

4 files changed

+53
-32
lines changed

package/astParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const acorn = require('acorn');
1+
const acorn = require('acorn'); // javascript parser
22
// eslint-disable-next-line import/newline-after-import
33
const jsx = require('acorn-jsx');
44
const JSXParser = acorn.Parser.extend(jsx());

package/linkFiber.js

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,40 @@
11
/* eslint-disable func-names */
22
/* eslint-disable no-use-before-define */
33
/* eslint-disable no-param-reassign */
4+
45
// links component state tree to library
56
// changes the setState method to also update our snapshot
7+
68
const Tree = require('./tree');
79
const astParser = require('./astParser');
8-
const { saveState } = require('./masterState');
10+
const { saveState } = require('./masterState'); // saves AST state as array for later use
911

1012
module.exports = (snap, mode) => {
13+
// snap is the current tree
14+
// mode is {jumping: bool, locked: bool, paused: bool}
15+
1116
let fiberRoot = null;
1217
let astHooks;
1318

14-
function sendSnapshot() {
19+
function sendSnapshot() { // send snapshot of current fiber tree to chrome extension ?
1520
// don't send messages while jumping or while paused
16-
// DEV: So that when we are jumping to an old snapshot it
17-
// wouldn't think we want to create new snapshots
21+
// DEV: So that when we are jumping to an old snapshot it wouldn't think we want to create new snapshots
1822
if (mode.jumping || mode.paused) return;
19-
const payload = snap.tree.getCopy();
23+
const payload = snap.tree.getCopy(); // copy of current react fiber tree
2024
// console.log('payload', payload);
21-
window.postMessage({
25+
window.postMessage({ // send to window
2226
action: 'recordSnap',
2327
payload,
2428
});
2529
}
2630

27-
function changeSetState(component) {
31+
function changeSetState(component) { // if invoked, change setState functionality so that it also updates our snapshot
32+
// console.log("what is component?", component);
2833
// check that setState hasn't been changed yet
29-
if (component.setState.linkFiberChanged) return;
34+
if (component.setState.linkFiberChanged) {
35+
// console.log("setState has already been changed for", component);
36+
return;
37+
};
3038
// make a copy of setState
3139
const oldSetState = component.setState.bind(component);
3240
// replace component's setState so developer doesn't change syntax
@@ -35,23 +43,24 @@ module.exports = (snap, mode) => {
3543
// don't do anything if state is locked
3644
// UNLESS we are currently jumping through time
3745
if (mode.locked && !mode.jumping) return;
38-
// continue normal setState functionality, except add sending message middleware
46+
// continue normal setState functionality, except add sending message (to chrome extension) middleware
3947
oldSetState(state, () => {
40-
updateSnapShotTree();
41-
sendSnapshot();
42-
callback.bind(component)();
48+
updateSnapShotTree(); // this doubles the actions in reactime for star wars app, also invokes changeSetState twice, also invokes changeSetState with Route and Characters
49+
sendSnapshot(); //runs once on page load, after event listener: message (line 145)
50+
callback.bind(component)(); // WHY DO WE NEED THIS ?
4351
});
4452
};
45-
component.setState.linkFiberChanged = true;
53+
component.setState.linkFiberChanged = true; // we changed setState.
4654
}
4755

48-
function changeUseState(component) {
56+
function changeUseState(component) { // if invoked, change useState dispatch functionality so that it also updates our snapshot
57+
//check that changeUseState hasn't been changed yet
4958
if (component.queue.dispatch.linkFiberChanged) return;
5059
// store the original dispatch function definition
5160
const oldDispatch = component.queue.dispatch.bind(component.queue);
5261
// redefine the dispatch function so we can inject our code
5362
component.queue.dispatch = (fiber, queue, action) => {
54-
// don't do anything if state is locked
63+
// don't do anything if state is locked, UNLESS we are currently jumping through time
5564
if (mode.locked && !mode.jumping) return;
5665
oldDispatch(fiber, queue, action);
5766
setTimeout(() => {
@@ -83,21 +92,22 @@ module.exports = (snap, mode) => {
8392
}
8493

8594
function createTree(currentFiber, tree = new Tree('root')) {
95+
// if there is no current fiber just return the new tree as-is
8696
if (!currentFiber) return tree;
87-
97+
// console.log("what is currentFiber", currentFiber);
8898
const {
8999
sibling,
90100
stateNode,
91101
child,
92102
memoizedState,
93103
elementType,
94-
} = currentFiber;
104+
} = currentFiber; // extract properties of current fiber
95105

96-
let nextTree = tree;
106+
let childTree = tree; // initialize child fiber tree as current fiber tree
97107
// check if stateful component
98108
if (stateNode && stateNode.state) {
99109
// add component to tree
100-
nextTree = tree.appendChild(stateNode);
110+
childTree = tree.appendChild(stateNode); // returns newly appended tree
101111
// change setState functionality
102112
changeSetState(stateNode);
103113
}
@@ -109,34 +119,43 @@ module.exports = (snap, mode) => {
109119
// Create a traversed property and assign to the evaluated result of
110120
// invoking traverseHooks with memoizedState
111121
memoizedState.traversed = traverseHooks(memoizedState);
112-
nextTree = tree.appendChild(memoizedState);
122+
childTree = tree.appendChild(memoizedState);
113123
}
114124
// iterate through siblings
115125
createTree(sibling, tree);
116126
// iterate through children
117-
createTree(child, nextTree);
127+
createTree(child, childTree);
118128

119129
return tree;
120130
}
121-
// runs when page initially loads
131+
132+
// runs when page initially loads and on subsequent state changes
122133
// but skips 1st hook click
123134
function updateSnapShotTree() {
124-
const { current } = fiberRoot;
135+
const { current } = fiberRoot; // on initial page load, current - fiberNode is tag type HostRoot (entire fiber tree)
136+
console.log("current", current);
125137
snap.tree = createTree(current);
126138
}
127139

140+
// RUNS ONCE, ON INITIAL PAGE LOAD ?
128141
return container => {
142+
// on first page load, container is entire html hierarchy of top level div
143+
// _reactRootContainer is that invisible top level object which wraps the top level div
144+
// _reactRootContainer._internalRoot is an object with property .current which includes HostRoot fiberNode (entire fiber tree)
129145
const {
130146
_reactRootContainer: { _internalRoot },
131147
_reactRootContainer,
132148
} = container;
133-
// only assign internal rootp if it actually exists
149+
// only assign internal root if it actually exists
134150
fiberRoot = _internalRoot || _reactRootContainer;
135151

136152
updateSnapShotTree();
137153
// send the initial snapshot once the content script has started up
138154
window.addEventListener('message', ({ data: { action } }) => {
139-
if (action === 'contentScriptStarted') sendSnapshot();
155+
if (action === 'contentScriptStarted') { // runs once on initial page load
156+
// console.log("in window.addEL")
157+
sendSnapshot()
158+
};
140159
});
141160
};
142161
};

package/tree.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ class Tree {
1717
}
1818
this.children = [];
1919
// DEV: Added print() for debugging purposes
20-
// this.print();
20+
21+
// this.print(); // this call is not useful here. it would be useful in a function call where we've already added to this.children
2122
}
2223

2324
appendChild(component) {
@@ -39,20 +40,21 @@ class Tree {
3940
return copy;
4041
}
4142

42-
// print out the tree in the console
43+
// print out the tree structure in the console
4344
// DEV: Process may be different for useState components
4445
// BUG FIX: Don't print the Router as a component
4546
// Change how the children are printed
4647
print() {
48+
console.log("current tree structure for *this : ", this);
4749
const children = ['children: '];
4850
// DEV: What should we push instead for components using hooks (it wouldn't be state)
49-
this.children.forEach(child => {
51+
this.children.forEach(child => { // if this.children is always initialized to empty array, when would there ever be anything to iterate through here?
5052
children.push(child.state || child.component.state);
5153
});
52-
if (this.name) console.log(this.name);
54+
if (this.name) console.log("this.name if exists: ", this.name);
5355
if (children.length === 1) {
54-
console.log(this.state || this.component.state);
55-
} else console.log(this.state || this.component.state, ...children);
56+
console.log(`children length 1. ${this.state ? `this.state: ` : `this.component.state: `}`, this.state || this.component.state);
57+
} else console.log(`children length !== 1. ${this.state ? `this.state: ` : `this.component.state, children: `}`, this.state || this.component.state, ...children);
5658
this.children.forEach(child => {
5759
child.print();
5860
});

src-app.jpg

330 KB
Loading

0 commit comments

Comments
 (0)