Skip to content

Commit 5af0eef

Browse files
(add) LinkedList algorithm to find DOM elements in the Fiber node for Recoil
Co-authored-by: Sanjay Lavingia <[email protected]>
2 parents c4605c9 + e64a870 commit 5af0eef

File tree

7 files changed

+87
-18
lines changed

7 files changed

+87
-18
lines changed

src/app/actions/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export const resetSlider = () => ({
7878
type: types.SLIDER_ZERO,
7979
});
8080

81-
export const onHover = () => ({
81+
export const onHover = (rtid) => ({
8282
type: types.ON_HOVER,
8383
//the payload should be something to relate the component we're hovering and highlight that component on the DOM
84-
payload: 'PAYLOAD FROM onHover inside of action.ts'
84+
payload: rtid
8585
})

src/app/components/ComponentMap.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { pointRadial } from 'd3-shape';
1111
import useForceUpdate from './useForceUpdate';
1212
import LinkControls from './LinkControls';
1313
import getLinkComponent from './getLinkComponent';
14+
import { onHover } from '../actions/actions'
15+
import { useStoreContext } from '../store'
1416

1517
const defaultMargin = { top: 30, left: 30, right: 30, bottom: 70 };
1618

@@ -28,6 +30,8 @@ export default function ComponentMap({
2830
margin = defaultMargin,
2931
snapshots: snapshots,
3032
}: LinkTypesProps) {
33+
34+
const [{ tabs, currentTab }, dispatch] = useStoreContext();
3135
// preparing the data to be used for render
3236
const lastNode = snapshots.length - 1;
3337
const data = snapshots[lastNode];
@@ -147,6 +151,7 @@ export default function ComponentMap({
147151
console.log(node);
148152
forceUpdate();
149153
}}
154+
onMouseEnter={()=>dispatch(onHover(node.data.rtid))}
150155
/>
151156
)}
152157
<text

src/app/reducers/mainReducer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default (state, action) => produce(state, draft => {
4242
case types.ON_HOVER: {
4343
port.postMessage({
4444
action: 'onHover',
45-
payload: 'payload from Reducer ON_HOVER',
45+
payload: action.payload,
4646
tabId: currentTab,
4747
})
4848
break;
@@ -199,6 +199,7 @@ export default (state, action) => produce(state, draft => {
199199
break;
200200
}
201201
case types.INITIAL_CONNECT: {
202+
console.log(action.paylooad)
202203
const { payload } = action;
203204
Object.keys(payload).forEach(tab => {
204205
// check if tab exists in memory

src/backend/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,20 @@ window.addEventListener('message', ({ data: { action, payload } }: MsgData) => {
5959
mode.paused = payload;
6060
break;
6161
case 'onHover':
62-
// console.log('WE MADE IT ALL THE WAY FROM THE FRONTEND! HERE\'S THE PAYLOAD:', payload);
62+
if (payload !== null) {
63+
let element = document.getElementById(payload)
64+
65+
if (element !== null) {
66+
67+
element.style.backgroundColor = '#C0D9D9';
68+
setTimeout( () => {
69+
element.style.backgroundColor = "";
70+
}, 500)
71+
}
72+
73+
// console.log('WE MADE IT ALL THE WAY FROM THE FRONTEND! HERE\'S THE PAYLOAD:', payload);
74+
// console.log(element);
75+
}
6376
break;
6477
default:
6578
break;

src/backend/linkFiber.ts

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import componentActionsRecord from './masterState';
2222
import { throttle, getHooksNames } from './helpers';
2323
import { Console } from 'console';
2424
import AtomsRelationship from '../app/components/AtomsRelationship';
25+
import { isNull } from 'util';
2526

2627
// Set global variables to use in exported module and helper functions
2728
declare global {
@@ -35,6 +36,9 @@ const circularComponentTable = new Set();
3536
let isRecoil = false;
3637
let allAtomsRelationship = [];
3738
let initialstart = false;
39+
let rtidCounter = 0;
40+
let rtid = null;
41+
let recoilStateNode = {};
3842

3943
// Simple check for whether our target app uses Recoil
4044
if (window[`$recoilDebugStates`]) {
@@ -78,7 +82,7 @@ function sendSnapshot(snap: Snapshot, mode: Mode): void {
7882
payload.atomSelectors = atomsSelectors;
7983
}
8084

81-
85+
console.log(recoilStateNode)
8286
window.postMessage(
8387
{
8488
action: 'recordSnap',
@@ -183,6 +187,17 @@ function createTree(
183187
if (!currentFiber) return null;
184188
if (!tree) return tree;
185189

190+
if (currentFiber.tag === 0 || currentFiber.tag === 1 || currentFiber.tag === 2) {
191+
if (currentFiber.child && currentFiber.child.stateNode) {
192+
// console.log('Sanjays algo', currentFiber)
193+
rtid = "fromLinkFiber" + rtidCounter
194+
// if (currentFiber.child.stateNode.setAttribute) {
195+
currentFiber.child.stateNode.setAttribute("id", rtid);
196+
// }
197+
}
198+
rtidCounter++;
199+
}
200+
186201
// These have the newest state. We update state and then
187202
// called updateSnapshotTree()
188203

@@ -200,6 +215,7 @@ function createTree(
200215
treeBaseDuration,
201216
} = currentFiber;
202217

218+
203219
//Checks Recoil Atom and Selector Relationships
204220
if (
205221
currentFiber.memoizedState &&
@@ -282,6 +298,7 @@ function createTree(
282298
(tag === 0 || tag === 1 || tag === 2 || tag === 10) &&
283299
isRecoil === true
284300
) {
301+
// console.log('Recoil Hooks Algo', currentFiber)
285302
if (memoizedState.queue) {
286303
// Hooks states are stored as a linked list using memoizedState.next,
287304
// so we must traverse through the list and get the states.
@@ -315,6 +332,7 @@ function createTree(
315332
(tag === 0 || tag === 1 || tag === 2 || tag === 10) &&
316333
isRecoil === false
317334
) {
335+
// console.log('Regular Hooks Algo', currentFiber)
318336
if (memoizedState.queue) {
319337
// Hooks states are stored as a linked list using memoizedState.next,
320338
// so we must traverse through the list and get the states.
@@ -343,6 +361,7 @@ function createTree(
343361

344362
// This grabs stateless components
345363
if (!componentFound && (tag === 0 || tag === 1 || tag === 2)) {
364+
// console.log('Stateless Algo', currentFiber)
346365
newState = 'stateless';
347366
}
348367

@@ -356,19 +375,47 @@ function createTree(
356375
};
357376

358377
let newNode = null;
378+
359379
// We want to add this fiber node to the snapshot
360380
if (componentFound || newState === 'stateless') {
361381
if (fromSibling) {
382+
383+
console.log(currentFiber)
384+
385+
386+
if(currentFiber.elementType.name){
387+
if(!recoilStateNode[currentFiber.elementType.name]){
388+
recoilStateNode[currentFiber.elementType.name] = [];
389+
}
390+
}
391+
392+
let pointer = currentFiber
393+
while(pointer !== null){
394+
if(pointer.stateNode !== null){
395+
rtid = "fromLinkFiber" + rtidCounter++
396+
recoilStateNode[currentFiber.elementType.name].push(pointer.stateNode)}
397+
pointer.stateNode.setAttribute("id", rtid)
398+
pointer = pointer.child
399+
}
400+
401+
// console.log(currentFiber)
402+
403+
// currentFiber.child.stateNode.setAttribute("id", rtid);
362404
newNode = tree.addSibling(
363405
newState,
364406
elementType ? elementType.name : 'nameless',
365-
componentData
407+
componentData,
408+
rtid
366409
);
367410
} else {
411+
// console.log(currentFiber)
412+
rtid = "fromLinkFiber" + rtidCounter++
413+
// currentFiber.child.stateNode.setAttribute("id", rtid);
368414
newNode = tree.addChild(
369415
newState,
370416
elementType ? elementType.name : 'nameless',
371-
componentData
417+
componentData,
418+
rtid
372419
);
373420
}
374421
} else {
@@ -382,13 +429,13 @@ function createTree(
382429
// Otherwise, attach children to this same node.
383430
circularComponentTable.add(child);
384431
createTree(child, newNode);
432+
385433
}
386434
// Recurse on siblings
387435
if (sibling && !circularComponentTable.has(sibling)) {
388436
circularComponentTable.add(sibling);
389437
createTree(sibling, newNode, true);
390438
}
391-
392439
return tree;
393440
}
394441

src/backend/tree.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,26 @@ class Tree {
4949

5050
atomSelectors: any;
5151

52-
constructor(state: string | {}, name = 'nameless', componentData: {} = {}) {
52+
rtid: any;
53+
54+
constructor(state: string | {}, name = 'nameless', componentData: {} = {}, rtid: any = null) {
5355
this.state = state === 'root' ? 'root' : serializeState(state);
5456
this.name = name;
5557
this.componentData = componentData ? JSON.parse(JSON.stringify(componentData)) : {};
5658
this.children = [];
5759
this.parent = null; // ref to parent so we can add siblings
60+
this.rtid = rtid
5861
}
5962

60-
addChild(state: string | {}, name: string, componentData: {}): Tree {
61-
const newChild: Tree = new Tree(state, name, componentData);
63+
addChild(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
64+
const newChild: Tree = new Tree(state, name, componentData, rtid);
6265
newChild.parent = this;
6366
this.children.push(newChild);
6467
return newChild;
6568
}
6669

67-
addSibling(state: string | {}, name: string, componentData: {}): Tree {
68-
const newSibling: Tree = new Tree(state, name, componentData);
70+
addSibling(state: string | {}, name: string, componentData: {}, rtid: any): Tree {
71+
const newSibling: Tree = new Tree(state, name, componentData, rtid);
6972
newSibling.parent = this.parent;
7073
this.parent.children.push(newSibling);
7174
return newSibling;
@@ -84,7 +87,7 @@ class Tree {
8487
circularComponentTable.clear();
8588
}
8689
// creates copy of present node
87-
let copy: Tree = new Tree(this.state, this.name, this.componentData);
90+
let copy: Tree = new Tree(this.state, this.name, this.componentData, this.rtid);
8891
delete copy.parent;
8992
circularComponentTable.add(this);
9093
copy = scrubUnserializableMembers(copy);

tests/manual-tests/ConcurrentMode/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)