Skip to content

Commit 0d36103

Browse files
committed
refactored 1 function and added comments
1 parent 461a2e4 commit 0d36103

File tree

1 file changed

+72
-95
lines changed

1 file changed

+72
-95
lines changed

src/backend/linkFiber.ts

Lines changed: 72 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
Snapshot,
1717
//jump, pause, lock
1818
Mode,
19-
2019
ComponentData,
2120
// array of state and component
2221
HookStates,
@@ -37,7 +36,7 @@ import { isNull } from 'util';
3736

3837
// Set global variables to use in exported module and helper functions
3938
declare global {
40-
interface Window {
39+
interface Window {
4140
__REACT_DEVTOOLS_GLOBAL_HOOK__?: any;
4241
}
4342
}
@@ -56,22 +55,23 @@ let recoilDomNode = {};
5655
if (window[`$recoilDebugStates`]) {
5756
isRecoil = true;
5857
}
59-
60-
// function getRecoilState(): any {
61-
// const RecoilSnapshotsLength = window[`$recoilDebugStates`].length;
62-
// const lastRecoilSnapshot =
63-
// window[`$recoilDebugStates`][RecoilSnapshotsLength - 1];
64-
// const nodeToNodeSubs = lastRecoilSnapshot.nodeToNodeSubscriptions;
65-
// const nodeToNodeSubsKeys = lastRecoilSnapshot.nodeToNodeSubscriptions.keys();
66-
// nodeToNodeSubsKeys.forEach((node) => {
67-
// nodeToNodeSubs
68-
// .get(node)
69-
// .forEach((nodeSubs) =>
70-
// allAtomsRelationship.push([node, nodeSubs, 'atoms and selectors'])
71-
// );
72-
// });
73-
// }
74-
58+
// function for recoil apps (unused as of 11.22.2020)
59+
/*
60+
function getRecoilState(): any {
61+
const RecoilSnapshotsLength = window[`$recoilDebugStates`].length;
62+
const lastRecoilSnapshot =
63+
window[`$recoilDebugStates`][RecoilSnapshotsLength - 1];
64+
const nodeToNodeSubs = lastRecoilSnapshot.nodeToNodeSubscriptions;
65+
const nodeToNodeSubsKeys = lastRecoilSnapshot.nodeToNodeSubscriptions.keys();
66+
nodeToNodeSubsKeys.forEach((node) => {
67+
nodeToNodeSubs
68+
.get(node)
69+
.forEach((nodeSubs) =>
70+
allAtomsRelationship.push([node, nodeSubs, 'atoms and selectors'])
71+
);
72+
});
73+
}
74+
*/
7575
/**
7676
* @method sendSnapshot
7777
* @param snap The current snapshot
@@ -116,9 +116,6 @@ function sendSnapshot(snap: Snapshot, mode: Mode): void {
116116
//updating tree depending on current mode on the panel (pause, locked etc)
117117
function updateSnapShotTree(snap: Snapshot, mode: Mode): void {
118118
// this is the currently active root fiber(the mutable root of the tree)
119-
let fiberRootCurrent = fiberRoot.current;
120-
// console.log("fiber root props: ", Object.entries(fiberRootCurrent));
121-
// console.log("fiberroot sibling:", fiberRootCurrent.sibling, "fiberroot stateNode:", fiberRootCurrent.stateNode, "fiberroot child:", fiberRootCurrent.child, "fiberroot memoizedState:", fiberRootCurrent.memoizedState, "fiberroot memoizedProps:", fiberRootCurrent.memoizedProps, "fiberRootCurrent.elementType:",fiberRootCurrent.elementType, "fiberRootCurrent.tag: ", fiberRootCurrent.tag, "fiberRootCurrent.actualDuration: ", fiberRootCurrent.actualDuration, "fiberRootCurrent.actualStartTime: ", fiberRootCurrent.actualStartTime, "fiberRootCurrent.selfBaseDuration: ", fiberRootCurrent.selfBaseDuration, "fiberRootCurrent.treeBaseDuration:", fiberRootCurrent.treeBaseDuration);
122119

123120
if (fiberRoot) {
124121
const { current } = fiberRoot;
@@ -183,8 +180,7 @@ function traverseHooks(memoizedState: any): HookStates {
183180
state: memoizedState.memoizedState,
184181
});
185182
}
186-
memoizedState =
187-
memoizedState.next !== memoizedState ? memoizedState.next : null;
183+
memoizedState = (memoizedState.next !== memoizedState) ? memoizedState.next : null;
188184
}
189185
return hooksStates;
190186
}
@@ -212,14 +208,10 @@ function createTree(
212208
fromSibling = false
213209
) {
214210
// Base case: child or sibling pointed to null
215-
216211
if (!currentFiber) return null;
217212
if (!tree) return tree;
218-
219-
220213
// These have the newest state. We update state and then
221214
// called updateSnapshotTree()
222-
223215
const {
224216
sibling,
225217
stateNode,
@@ -235,7 +227,6 @@ function createTree(
235227
treeBaseDuration,
236228
} = currentFiber;
237229

238-
239230
//Checks Recoil Atom and Selector Relationships
240231
if (
241232
currentFiber.memoizedState &&
@@ -302,13 +293,10 @@ function createTree(
302293
stateNode.state,
303294
stateNode
304295
);
305-
306296
newState = stateNode.state;
307297
componentFound = true;
308298
}
309-
310299
let hooksIndex;
311-
312300
const atomArray = [];
313301
atomArray.push(memoizedProps);
314302

@@ -324,21 +312,36 @@ function createTree(
324312
// We then store them along with the corresponding memoizedState.queue,
325313
// which includes the dispatch() function we use to change their state.
326314
const hooksStates = traverseRecoilHooks(memoizedState, memoizedProps);
327-
hooksStates.forEach((state) => {
315+
console.log("hookStates: ", hooksStates);
316+
hooksStates.forEach((state, i) => {
328317
hooksIndex = componentActionsRecord.saveNew(
329318
state.state,
330319
state.component
331320
);
332321
componentData.hooksIndex = hooksIndex;
333322

334323
// Improves tree visualization but breaks jump ?
335-
if (newState && newState.hooksState) {
336-
newState.push(state.state);
337-
} else if (newState) {
338-
newState = [state.state];
339-
} else {
340-
newState.push(state.state);
324+
// if (!newState) {
325+
326+
// }
327+
// newState.push(state.state);
328+
// console.log('newState in Recoil: ', newState);
329+
// console.log('state.state: ', state.state);
330+
/* what is this supposed to do??? currently doesn't work?? and makes no sense, newState is an object, how can you push state.state into an object?? */
331+
// if (newState && newState.hooksState) {
332+
// newState.push(state.state);
333+
// } else if (newState) {
334+
// newState = [state.state];
335+
// } else {
336+
// newState.push(state.state);
337+
// }
338+
// componentFound = true;
339+
if (!newState) {
340+
newState = { hooksState: [] };
341+
} else if (!newState.hooksState) {
342+
newState.hooksState = [];
341343
}
344+
newState.hooksState.push({ [i]: state.state });
342345
componentFound = true;
343346
});
344347
}
@@ -352,6 +355,7 @@ function createTree(
352355
isRecoil === false
353356
) {
354357
if (memoizedState.queue) {
358+
console.log("line 357...")
355359
// Hooks states are stored as a linked list using memoizedState.next,
356360
// so we must traverse through the list and get the states.
357361
// We then store them along with the corresponding memoizedState.queue,
@@ -364,15 +368,12 @@ function createTree(
364368
state.component
365369
);
366370
componentData.hooksIndex = hooksIndex;
367-
if (newState && newState.hooksState) {
368-
newState.hooksState.push({ [hooksNames[i]]: state.state });
369-
} else if (newState) {
370-
newState.hooksState = [{ [hooksNames[i]]: state.state }];
371-
} else {
372-
// possibly app breaks somewhere if newState and hooksState do not exist?
371+
if (!newState) {
373372
newState = { hooksState: [] };
374-
newState.hooksState.push({ [hooksNames[i]]: state.state });
373+
} else if (!newState.hooksState) {
374+
newState.hooksState = [];
375375
}
376+
newState.hooksState.push({ [hooksNames[i]]: state.state });
376377
componentFound = true;
377378
});
378379
}
@@ -397,33 +398,33 @@ function createTree(
397398
// We want to add this fiber node to the snapshot
398399
if (componentFound || newState === 'stateless') {
399400
// where does this get changed to true?
400-
if (fromSibling) {
401-
402-
if(isRecoil){
403-
if(currentFiber.elementType.name){
404-
if(!recoilDomNode[currentFiber.elementType.name]){
405-
recoilDomNode[currentFiber.elementType.name] = [];
406-
}
401+
if (isRecoil) {
402+
// do this down below too
403+
if(currentFiber.elementType.name){
404+
if(!recoilDomNode[currentFiber.elementType.name]){
405+
recoilDomNode[currentFiber.elementType.name] = [];
407406
}
408-
409-
let pointer = currentFiber
410-
411-
while(pointer !== null){
412-
if(pointer.stateNode !== null){
413-
rtid = "fromLinkFiber" + rtidCounter++
414-
recoilDomNode[currentFiber.elementType.name].push(rtid)
415-
pointer.stateNode.setAttribute("id", rtid)
416-
}
417-
418-
pointer = pointer.child
419-
}
420-
} else {
421-
if (currentFiber.child && currentFiber.child.stateNode && currentFiber.child.stateNode.setAttribute) {
422-
rtid = "fromLinkFiber" + rtidCounter
423-
currentFiber.child.stateNode.setAttribute("id", rtid);
424-
}
425-
rtidCounter++;
426407
}
408+
let pointer = currentFiber
409+
// end of repeat code
410+
411+
while (pointer !== null) {
412+
if(pointer.stateNode !== null){
413+
rtid = "fromLinkFiber" + rtidCounter++
414+
recoilDomNode[currentFiber.elementType.name].push(rtid)
415+
pointer.stateNode.setAttribute("id", rtid)
416+
}
417+
pointer = pointer.child
418+
}
419+
} else {
420+
if (currentFiber.child && currentFiber.child.stateNode && currentFiber.child.stateNode.setAttribute) {
421+
rtid = "fromLinkFiber" + rtidCounter
422+
currentFiber.child.stateNode.setAttribute("id", rtid);
423+
}
424+
rtidCounter++;
425+
}
426+
// checking if tree fromSibling is true
427+
if (fromSibling) {
427428
// tree object from tree.ts, with addSibling
428429
newNode = tree.addSibling(
429430
newState,
@@ -432,31 +433,7 @@ function createTree(
432433
rtid,
433434
recoilDomNode
434435
);
435-
} else {
436-
437-
if(isRecoil){
438-
if(currentFiber.elementType.name){
439-
if(!recoilDomNode[currentFiber.elementType.name]){
440-
recoilDomNode[currentFiber.elementType.name] = [];
441-
}
442-
}
443-
let pointer = currentFiber
444-
while(pointer !== null){
445-
if(pointer.stateNode !== null){
446-
rtid = "fromLinkFiber" + rtidCounter++
447-
recoilDomNode[currentFiber.elementType.name].push(rtid)
448-
pointer.stateNode.setAttribute("id", rtid)
449-
}
450-
pointer = pointer.child
451-
}
452-
} else {
453-
if (currentFiber.child && currentFiber.child.stateNode && currentFiber.child.stateNode.setAttribute) {
454-
rtid = "fromLinkFiber" + rtidCounter
455-
currentFiber.child.stateNode.setAttribute("id", rtid);
456-
}
457-
rtidCounter++;
458-
}
459-
436+
} else {
460437
newNode = tree.addChild(
461438
newState,
462439
elementType ? elementType.name : 'nameless',

0 commit comments

Comments
 (0)