refactor(store): rename nodeLookup to nodeInternals

This commit is contained in:
moklick
2021-11-04 18:13:21 +01:00
parent cc7debf8de
commit 996b1b51f5
13 changed files with 142 additions and 128 deletions
+14
View File
@@ -0,0 +1,14 @@
import { useRef, useEffect } from 'react';
import { useStoreApi } from '../store';
function useNodeInternalsRef() {
const store = useStoreApi();
const nodeInternals = useRef(store.getState().nodeInternals);
useEffect(() => store.subscribe((state) => (nodeInternals.current = state.nodeInternals)), []);
return nodeInternals;
}
export default useNodeInternalsRef;
-14
View File
@@ -1,14 +0,0 @@
import { useRef, useEffect } from 'react';
import { useStoreApi } from '../store';
function useNodeLookupRef() {
const store = useStoreApi();
const nodeLookup = useRef(store.getState().nodeLookup);
useEffect(() => store.subscribe((state) => (nodeLookup.current = state.nodeLookup)), []);
return nodeLookup;
}
export default useNodeLookupRef;
+9 -9
View File
@@ -2,15 +2,15 @@ import { useCallback } from 'react';
import { useStore } from '../store';
import { isEdgeVisible } from '../container/EdgeRenderer/utils';
import { ReactFlowState, NodeLookup, Edge } from '../types';
import { ReactFlowState, NodeInternals, Edge } from '../types';
function groupEdgesByTreeLevel(edges: Edge[], nodeLookup: NodeLookup) {
function groupEdgesByTreeLevel(edges: Edge[], nodeInternals: NodeInternals) {
let maxLevel = -1;
const levelLookup = edges.reduce<Record<string, Edge[]>>((tree, edge) => {
const treeLevel = Math.max(
nodeLookup.get(edge.source)?.treeLevel || 0,
nodeLookup.get(edge.target)?.treeLevel || 0
nodeInternals.get(edge.source)?.treeLevel || 0,
nodeInternals.get(edge.target)?.treeLevel || 0
);
if (tree[treeLevel]) {
tree[treeLevel].push(edge);
@@ -34,7 +34,7 @@ function groupEdgesByTreeLevel(edges: Edge[], nodeLookup: NodeLookup) {
});
}
function useVisibleEdges(onlyRenderVisible: boolean, nodeLookup: NodeLookup) {
function useVisibleEdges(onlyRenderVisible: boolean, nodeInternals: NodeInternals) {
const edges = useStore(
useCallback(
(s: ReactFlowState) => {
@@ -43,8 +43,8 @@ function useVisibleEdges(onlyRenderVisible: boolean, nodeLookup: NodeLookup) {
}
return s.edges.filter((e) => {
const sourceNode = nodeLookup.get(e.source);
const targetNode = nodeLookup.get(e.target);
const sourceNode = nodeInternals.get(e.source);
const targetNode = nodeInternals.get(e.target);
return (
sourceNode?.width &&
@@ -65,11 +65,11 @@ function useVisibleEdges(onlyRenderVisible: boolean, nodeLookup: NodeLookup) {
);
});
},
[onlyRenderVisible, nodeLookup]
[onlyRenderVisible, nodeInternals]
)
);
return groupEdgesByTreeLevel(edges, nodeLookup);
return groupEdgesByTreeLevel(edges, nodeInternals);
}
export default useVisibleEdges;