Skip to content

Commit

Permalink
refactor(store): reset on unmount
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Nov 23, 2021
1 parent f775cf2 commit 2962897
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 44 deletions.
2 changes: 1 addition & 1 deletion example/src/index.tsx
Expand Up @@ -68,7 +68,7 @@ const Header = withRouter(({ history, location }) => {
});

ReactDOM.render(
<Router forceRefresh={true}>
<Router>
<Header />
<Switch>
{routes.map((route) => (
Expand Down
12 changes: 10 additions & 2 deletions src/components/StoreUpdater/index.tsx
Expand Up @@ -57,6 +57,7 @@ const selector = (s: ReactFlowState) => ({
setConnectionMode: s.setConnectionMode,
setOnNodesChange: s.setOnNodesChange,
setOnEdgesChange: s.setOnEdgesChange,
reset: s.reset,
});

const StoreUpdater = ({
Expand Down Expand Up @@ -98,15 +99,22 @@ const StoreUpdater = ({
setOnNodesChange,
setOnEdgesChange,
setConnectionMode,
reset,
} = useStore(selector, shallow);

useEffect(() => {
return () => {
reset();
};
}, [reset]);

useEffect(() => {
setNodes(nodes);
}, [nodes]);

useEffect(() => {
setEdges(edges, nodes);
}, [edges, nodes]);
setEdges(edges);
}, [edges]);

useEffect(() => {
if (onConnect) {
Expand Down
81 changes: 43 additions & 38 deletions src/store/index.ts
Expand Up @@ -25,6 +25,7 @@ import {
Transform,
Dimensions,
XYPosition,
ReactFlowStore,
} from '../types';
import { isNode, isEdge, getRectOfNodes, getNodesInside, getConnectedEdges } from '../utils/graph';
import { getHandleBounds } from '../components/Nodes/utils';
Expand All @@ -37,51 +38,54 @@ const infiniteExtent: CoordinateExtent = [
[Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY],
];

const createStore = () =>
create<ReactFlowState>((set, get) => ({
const initialState: ReactFlowStore = {
width: 0,
height: 0,
transform: [0, 0, 1],
nodeInternals: new Map(),
edges: [],
onNodesChange: null,
onEdgesChange: null,
selectedNodesBbox: { x: 0, y: 0, width: 0, height: 0 },
d3Zoom: null,
d3Selection: null,
d3ZoomHandler: undefined,
minZoom: 0.5,
maxZoom: 2,
translateExtent: infiniteExtent,
nodeExtent: infiniteExtent,
nodesSelectionActive: false,
selectionActive: false,
userSelectionRect: {
startX: 0,
startY: 0,
x: 0,
y: 0,
width: 0,
height: 0,
transform: [0, 0, 1],
edges: [],
onNodesChange: null,
onEdgesChange: null,
selectedNodesBbox: { x: 0, y: 0, width: 0, height: 0 },
d3Zoom: null,
d3Selection: null,
d3ZoomHandler: undefined,
minZoom: 0.5,
maxZoom: 2,
translateExtent: infiniteExtent,
nodeExtent: infiniteExtent,
nodesSelectionActive: false,
selectionActive: false,
userSelectionRect: {
startX: 0,
startY: 0,
x: 0,
y: 0,
width: 0,
height: 0,
draw: false,
},
connectionNodeId: null,
connectionHandleId: null,
connectionHandleType: 'source',
connectionPosition: { x: 0, y: 0 },
connectionMode: ConnectionMode.Strict,
draw: false,
},
connectionNodeId: null,
connectionHandleId: null,
connectionHandleType: 'source',
connectionPosition: { x: 0, y: 0 },
connectionMode: ConnectionMode.Strict,

snapGrid: [15, 15],
snapToGrid: false,
snapGrid: [15, 15],
snapToGrid: false,

nodesDraggable: true,
nodesConnectable: true,
elementsSelectable: true,
nodesDraggable: true,
nodesConnectable: true,
elementsSelectable: true,

multiSelectionActive: false,
multiSelectionActive: false,

reactFlowVersion: typeof __REACT_FLOW_VERSION__ !== 'undefined' ? __REACT_FLOW_VERSION__ : '-',
reactFlowVersion: typeof __REACT_FLOW_VERSION__ !== 'undefined' ? __REACT_FLOW_VERSION__ : '-',
};

nodeInternals: new Map(),
const createStore = () =>
create<ReactFlowState>((set, get) => ({
...initialState,

setNodes: (nodes: Node[]) => {
const nodeInternals = createNodeInternals(nodes, get().nodeInternals);
Expand Down Expand Up @@ -366,6 +370,7 @@ const createStore = () =>
setConnectionMode: (connectionMode: ConnectionMode) => set({ connectionMode }),
setOnNodesChange: (onNodesChange: OnNodesChange) => set({ onNodesChange }),
setOnEdgesChange: (onEdgesChange: OnEdgesChange) => set({ onEdgesChange }),
reset: () => set({ ...initialState }),
}));

export { Provider, useStore, createStore, useStoreApi };
12 changes: 9 additions & 3 deletions src/types/general.ts
Expand Up @@ -130,7 +130,7 @@ export type InitD3ZoomPayload = {
transform: Transform;
};

export interface ReactFlowState {
export type ReactFlowStore = {
width: number;
height: number;
transform: Transform;
Expand Down Expand Up @@ -169,9 +169,11 @@ export interface ReactFlowState {
multiSelectionActive: boolean;

reactFlowVersion: string;
};

export type ReactFlowActions = {
setNodes: (nodes: Node[]) => void;
setEdges: (edges: Edge[], nodes: Node[]) => void;
setEdges: (edges: Edge[]) => void;
updateNodeDimensions: (updates: NodeDimensionUpdate[]) => void;
updateNodePosition: (update: NodeDiffUpdate) => void;
setUserSelection: (mousePos: XYPosition) => void;
Expand Down Expand Up @@ -209,7 +211,11 @@ export interface ReactFlowState {
onConnectStart?: OnConnectStart;
onConnectStop?: OnConnectStop;
onConnectEnd?: OnConnectEnd;
}

reset: () => void;
};

export type ReactFlowState = ReactFlowStore & ReactFlowActions;

export type UpdateNodeInternals = (nodeId: string) => void;

Expand Down

0 comments on commit 2962897

Please sign in to comment.