refactor(storeupdater): cleanup
This commit is contained in:
@@ -62,6 +62,14 @@ const selector = (s: ReactFlowState) => ({
|
|||||||
setFitViewOnInit: s.setFitViewOnInit,
|
setFitViewOnInit: s.setFitViewOnInit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function useStoreUpdater<T>(value: T | undefined, setStoreState: (param: T) => void) {
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof value !== 'undefined') {
|
||||||
|
setStoreState(value);
|
||||||
|
}
|
||||||
|
}, [value]);
|
||||||
|
}
|
||||||
|
|
||||||
const StoreUpdater = ({
|
const StoreUpdater = ({
|
||||||
nodes,
|
nodes,
|
||||||
edges,
|
edges,
|
||||||
@@ -112,113 +120,26 @@ const StoreUpdater = ({
|
|||||||
};
|
};
|
||||||
}, [reset]);
|
}, [reset]);
|
||||||
|
|
||||||
useEffect(() => {
|
useStoreUpdater<Node[]>(nodes, setNodes);
|
||||||
setNodes(nodes);
|
useStoreUpdater<Edge[]>(edges, setEdges);
|
||||||
}, [nodes]);
|
useStoreUpdater<OnConnect>(onConnect, setOnConnect);
|
||||||
|
useStoreUpdater<OnConnect>(onConnect, setOnConnect);
|
||||||
useEffect(() => {
|
useStoreUpdater<OnConnectStart>(onConnectStart, setOnConnectStart);
|
||||||
setEdges(edges);
|
useStoreUpdater<OnConnectStop>(onConnectStop, setOnConnectStop);
|
||||||
}, [edges]);
|
useStoreUpdater<OnConnectEnd>(onConnectEnd, setOnConnectEnd);
|
||||||
|
useStoreUpdater<boolean>(snapToGrid, setSnapToGrid);
|
||||||
useEffect(() => {
|
useStoreUpdater<SnapGrid>(snapGrid, setSnapGrid);
|
||||||
if (onConnect) {
|
useStoreUpdater<boolean>(nodesDraggable, setNodesDraggable);
|
||||||
setOnConnect(onConnect);
|
useStoreUpdater<boolean>(nodesConnectable, setNodesConnectable);
|
||||||
}
|
useStoreUpdater<boolean>(elementsSelectable, setElementsSelectable);
|
||||||
}, [onConnect]);
|
useStoreUpdater<number>(minZoom, setMinZoom);
|
||||||
|
useStoreUpdater<number>(maxZoom, setMaxZoom);
|
||||||
useEffect(() => {
|
useStoreUpdater<CoordinateExtent>(translateExtent, setTranslateExtent);
|
||||||
if (onConnectStart) {
|
useStoreUpdater<CoordinateExtent>(nodeExtent, setNodeExtent);
|
||||||
setOnConnectStart(onConnectStart);
|
useStoreUpdater<ConnectionMode>(connectionMode, setConnectionMode);
|
||||||
}
|
useStoreUpdater<OnNodesChange>(onNodesChange, setOnNodesChange);
|
||||||
}, [onConnectStart]);
|
useStoreUpdater<OnEdgesChange>(onEdgesChange, setOnEdgesChange);
|
||||||
|
useStoreUpdater<boolean>(fitViewOnInit, setFitViewOnInit);
|
||||||
useEffect(() => {
|
|
||||||
if (onConnectStop) {
|
|
||||||
setOnConnectStop(onConnectStop);
|
|
||||||
}
|
|
||||||
}, [onConnectStop]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (onConnectEnd) {
|
|
||||||
setOnConnectEnd(onConnectEnd);
|
|
||||||
}
|
|
||||||
}, [onConnectEnd]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof snapToGrid !== 'undefined') {
|
|
||||||
setSnapToGrid(snapToGrid);
|
|
||||||
}
|
|
||||||
}, [snapToGrid]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof snapGrid !== 'undefined') {
|
|
||||||
setSnapGrid(snapGrid);
|
|
||||||
}
|
|
||||||
}, [snapGrid]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof nodesDraggable !== 'undefined') {
|
|
||||||
setNodesDraggable(nodesDraggable);
|
|
||||||
}
|
|
||||||
}, [nodesDraggable]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof nodesConnectable !== 'undefined') {
|
|
||||||
setNodesConnectable(nodesConnectable);
|
|
||||||
}
|
|
||||||
}, [nodesConnectable]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof elementsSelectable !== 'undefined') {
|
|
||||||
setElementsSelectable(elementsSelectable);
|
|
||||||
}
|
|
||||||
}, [elementsSelectable]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof minZoom !== 'undefined') {
|
|
||||||
setMinZoom(minZoom);
|
|
||||||
}
|
|
||||||
}, [minZoom]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof maxZoom !== 'undefined') {
|
|
||||||
setMaxZoom(maxZoom);
|
|
||||||
}
|
|
||||||
}, [maxZoom]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof translateExtent !== 'undefined') {
|
|
||||||
setTranslateExtent(translateExtent);
|
|
||||||
}
|
|
||||||
}, [translateExtent]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof nodeExtent !== 'undefined') {
|
|
||||||
setNodeExtent(nodeExtent);
|
|
||||||
}
|
|
||||||
}, [nodeExtent]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof connectionMode !== 'undefined') {
|
|
||||||
setConnectionMode(connectionMode);
|
|
||||||
}
|
|
||||||
}, [connectionMode]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof onNodesChange !== 'undefined') {
|
|
||||||
setOnNodesChange(onNodesChange);
|
|
||||||
}
|
|
||||||
}, [onNodesChange]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (typeof onEdgesChange !== 'undefined') {
|
|
||||||
setOnEdgesChange(onEdgesChange);
|
|
||||||
}
|
|
||||||
}, [onEdgesChange]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
setFitViewOnInit(fitViewOnInit);
|
|
||||||
}, [fitViewOnInit]);
|
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user