feat(svelte): reset store when component unmounts
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
<script lang="ts">
|
||||
import { setContext } from 'svelte';
|
||||
import { onDestroy, setContext } from 'svelte';
|
||||
|
||||
import { createStore, key } from '$lib/store';
|
||||
import type { SvelteFlowProviderProps } from './types';
|
||||
@@ -17,6 +17,10 @@
|
||||
setContext(key, {
|
||||
getStore: () => store
|
||||
});
|
||||
|
||||
onDestroy(() => {
|
||||
store.reset();
|
||||
});
|
||||
</script>
|
||||
|
||||
<slot />
|
||||
|
||||
@@ -41,6 +41,10 @@
|
||||
store.width.set(width);
|
||||
store.height.set(height);
|
||||
store.domNode.set(domNode);
|
||||
|
||||
return () => {
|
||||
store.reset();
|
||||
}
|
||||
});
|
||||
|
||||
$: {
|
||||
|
||||
@@ -287,6 +287,15 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
||||
updateConnection(initConnectionData);
|
||||
}
|
||||
|
||||
function reset() {
|
||||
store.fitViewOnInitDone.set(false);
|
||||
store.selectionRect.set(null);
|
||||
store.selectionRectMode.set(null);
|
||||
|
||||
resetSelectedElements();
|
||||
cancelConnection();
|
||||
}
|
||||
|
||||
return {
|
||||
// state
|
||||
...store,
|
||||
@@ -310,7 +319,8 @@ export function createStore(params: CreateStoreParams): SvelteFlowStore {
|
||||
addSelectedNodes,
|
||||
panBy,
|
||||
updateConnection,
|
||||
cancelConnection
|
||||
cancelConnection,
|
||||
reset
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -30,6 +30,7 @@ export type SvelteFlowStoreActions = {
|
||||
panBy: (delta: XYPosition) => void;
|
||||
updateConnection: (connection: Partial<ConnectionData>) => void;
|
||||
cancelConnection: () => void;
|
||||
reset(): void;
|
||||
};
|
||||
|
||||
export type SvelteFlowStoreState = typeof initialStoreState & {
|
||||
|
||||
Reference in New Issue
Block a user