feat: try to dispose states on unmount

Signed-off-by: bcakmakoglu <78412429+bcakmakoglu@users.noreply.github.com>
This commit is contained in:
bcakmakoglu
2022-02-21 20:25:17 +01:00
committed by Braks
parent d835e4546c
commit c5b39d4588
10 changed files with 240 additions and 224 deletions
+1 -7
View File
@@ -6,12 +6,6 @@ const selectAll = () => {
addSelectedNodes(getNodes.value) addSelectedNodes(getNodes.value)
nodesSelectionActive.value = true nodesSelectionActive.value = true
} }
const transformString = computed(() => [
transform.value[0].toFixed(2),
transform.value[1].toFixed(2),
transform.value[2].toFixed(2),
])
</script> </script>
<template> <template>
<aside> <aside>
@@ -20,7 +14,7 @@ const transformString = computed(() => [
</div> </div>
<div class="title">Zoom & pan transform</div> <div class="title">Zoom & pan transform</div>
<div class="transform"> <div class="transform">
{{ transformString }} {{ [transform[0].toFixed(2), transform[1].toFixed(2), transform[2].toFixed(2)] }}
</div> </div>
<div class="title">Nodes</div> <div class="title">Nodes</div>
<div v-for="node of getNodes" :key="node.id"> <div v-for="node of getNodes" :key="node.id">
+1 -1
View File
@@ -38,7 +38,7 @@ const onChange = ({ color: c, val }: { color: keyof Colors; val: number }) => (c
<RGBNode v-bind="props" :amount="color" @change="onChange" /> <RGBNode v-bind="props" :amount="color" @change="onChange" />
</template> </template>
<template #node-rgb-output="props"> <template #node-rgb-output="props">
<RGBOutputNode :v-bind="props" :rgb="`rgb(${color.red}, ${color.green}, ${color.blue})`" /> <RGBOutputNode v-bind="props" :rgb="`rgb(${color.red}, ${color.green}, ${color.blue})`" />
</template> </template>
</VueFlow> </VueFlow>
</div> </div>
@@ -14,7 +14,7 @@ const props = withDefaults(defineProps<MiniMapProps>(), {
maskColor: 'rgb(240, 242, 243, 0.7)', maskColor: 'rgb(240, 242, 243, 0.7)',
}) })
const attrs: any = useAttrs() const attrs = useAttrs()
const window = useWindow() const window = useWindow()
const defaultWidth = 200 const defaultWidth = 200
+5
View File
@@ -166,4 +166,9 @@ export default (store: FlowStore = useVueFlow().store) =>
doc.addEventListener('mousemove', onMouseMove as EventListenerOrEventListenerObject) doc.addEventListener('mousemove', onMouseMove as EventListenerOrEventListenerObject)
doc.addEventListener('mouseup', onMouseUp as EventListenerOrEventListenerObject) doc.addEventListener('mouseup', onMouseUp as EventListenerOrEventListenerObject)
onScopeDispose(() => {
doc.removeEventListener('mousemove', onMouseMove as EventListenerOrEventListenerObject)
doc.removeEventListener('mouseup', onMouseUp as EventListenerOrEventListenerObject)
})
} }
+18 -10
View File
@@ -1,17 +1,17 @@
import { getCurrentInstance } from 'vue' import { EffectScope } from 'vue'
import { FlowOptions, UseVueFlow } from '~/types' import { FlowOptions, UseVueFlow, Store } from '~/types'
import { VueFlow } from '~/context' import { VueFlow } from '~/context'
import { useStore } from '~/store' import { useStore } from '~/store'
let id = 0 let id = 0
type Scope = EffectScope & { vueFlow: UseVueFlow }
export default <N = any, E = N>(options?: Partial<FlowOptions<N, E>>): UseVueFlow<N, E> => { export default <N = any, E = N>(options?: Partial<FlowOptions<N, E>>): UseVueFlow<N, E> => {
const currentInstance: any = getCurrentInstance() const scope = getCurrentScope() as Scope
let vueFlow: false | UseVueFlow = currentInstance let vueFlow: UseVueFlow | null = scope ? inject(VueFlow, null) ?? (scope.vueFlow as UseVueFlow) : null
? inject(VueFlow, undefined) ?? (currentInstance.vueFlow as UseVueFlow)
: false
if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) { if (!vueFlow || (vueFlow && options?.id && options.id !== vueFlow.id)) {
const name = options?.id ?? `vue-flow-${id++}` const name = options?.id ?? `vue-flow-${id++}`
const store = useStore(options) let store: Store = useStore(options)
vueFlow = { vueFlow = {
id: name, id: name,
store: reactive(store), store: reactive(store),
@@ -20,11 +20,19 @@ export default <N = any, E = N>(options?: Partial<FlowOptions<N, E>>): UseVueFlo
...store.actions, ...store.actions,
...store.hooksOn, ...store.hooksOn,
} as unknown as UseVueFlow } as unknown as UseVueFlow
}
if (currentInstance) { if (scope) {
provide(VueFlow, vueFlow) provide(VueFlow, vueFlow)
currentInstance.vueFlow = vueFlow scope.vueFlow = vueFlow
} }
onScopeDispose(() => {
vueFlow = null as UseVueFlow
scope.vueFlow = null as UseVueFlow
store = null as Store
})
}
if (!vueFlow) throw new Error('VueFlow instance not found.')
return <UseVueFlow<N, E>>vueFlow return <UseVueFlow<N, E>>vueFlow
} }
+5 -7
View File
@@ -15,6 +15,8 @@ export default (
props: FlowProps, props: FlowProps,
store: FlowStore, store: FlowStore,
) => { ) => {
const scope = getCurrentScope()
scope?.run(() => {
if (isDefined(props.modelValue)) { if (isDefined(props.modelValue)) {
const { pause, resume } = pausableWatch([() => props.modelValue, () => props.modelValue?.length], async ([v]) => { const { pause, resume } = pausableWatch([() => props.modelValue, () => props.modelValue?.length], async ([v]) => {
if (v && Array.isArray(v)) { if (v && Array.isArray(v)) {
@@ -31,7 +33,7 @@ export default (
if (v && Array.isArray(v)) { if (v && Array.isArray(v)) {
pause() pause()
store.setNodes(v) store.setNodes(v)
if (nodes) nodes.value = store.nodes if (nodes) nodes = store.nodes
await nextTick() await nextTick()
resume() resume()
} }
@@ -42,7 +44,7 @@ export default (
if (v && Array.isArray(v)) { if (v && Array.isArray(v)) {
pause() pause()
store.setEdges(v) store.setEdges(v)
if (edges) edges.value = store.edges if (edges) edges = store.edges
await nextTick() await nextTick()
resume() resume()
} }
@@ -204,9 +206,5 @@ export default (
(v) => isDef(v) && (store.applyDefault = v), (v) => isDef(v) && (store.applyDefault = v),
{ immediate: isDef(props.applyDefault) }, { immediate: isDef(props.applyDefault) },
) )
watch( })
() => props.fitViewOnInit,
(v) => isDef(v) && (store.fitViewOnInit = v),
{ immediate: isDef(props.fitViewOnInit) },
)
} }
+4
View File
@@ -1,3 +1,4 @@
import useState from './state'
import { import {
CoordinateExtent, CoordinateExtent,
EdgeChange, EdgeChange,
@@ -304,5 +305,8 @@ export default (state: State, getters: ComputedGetters): Actions => {
setConnectionNodeId, setConnectionNodeId,
setInteractive, setInteractive,
setState, setState,
$reset: () => {
setState(useState())
},
} }
} }
+1 -1
View File
@@ -97,7 +97,7 @@ export default (opts?: FlowOptions): State => {
applyDefault: true, applyDefault: true,
vueFlowVersion: typeof __VUE_FLOW_VERSION__ !== 'undefined' ? __VUE_FLOW_VERSION__ : '-', vueFlowVersion: typeof __VUE_FLOW_VERSION__ !== 'undefined' ? __VUE_FLOW_VERSION__ : '-',
} } as State
if (opts) { if (opts) {
if (typeof opts.panOnScroll !== 'undefined') state.panOnScroll = opts.panOnScroll if (typeof opts.panOnScroll !== 'undefined') state.panOnScroll = opts.panOnScroll
+16 -9
View File
@@ -1,31 +1,38 @@
import useState from './state' import useState from './state'
import useActions from './actions' import useActions from './actions'
import useGetters from './getters' import useGetters from './getters'
import { FlowHooksOn, FlowOptions, Store } from '~/types' import { FlowHooksOn, FlowOptions, Store, State } from '~/types'
export default (preloadedState?: FlowOptions): Store => { export default (preloadedState?: FlowOptions): Store => {
const state = reactive(useState(preloadedState)) const state: State = useState(preloadedState)
const getters = useGetters(state) const reactiveState = reactive(state)
const actions = useActions(state, getters) const getters = useGetters(reactiveState)
const actions = useActions(reactiveState, getters)
const hooksOn: FlowHooksOn = <any>{} const hooksOn: FlowHooksOn = <any>{}
Object.entries(state.hooks).forEach(([n, h]) => { Object.entries(reactiveState.hooks).forEach(([n, h]) => {
const name = `on${n.charAt(0).toUpperCase() + n.slice(1)}` const name = `on${n.charAt(0).toUpperCase() + n.slice(1)}`
hooksOn[<keyof FlowHooksOn>name] = h.on as any hooksOn[<keyof FlowHooksOn>name] = h.on as any
}) })
actions.setState(state) actions.setState(reactiveState)
if (preloadedState) { if (preloadedState) {
if (preloadedState.modelValue) actions.setElements(preloadedState.modelValue) if (preloadedState.modelValue) actions.setElements(preloadedState.modelValue)
if (preloadedState.nodes) actions.setNodes(preloadedState.nodes) if (preloadedState.nodes) actions.setNodes(preloadedState.nodes)
if (preloadedState.edges) actions.setEdges(preloadedState.edges) if (preloadedState.edges) actions.setEdges(preloadedState.edges)
} }
return { const store = {
state, state: reactiveState,
actions, actions,
getters, getters,
hooksOn, hooksOn,
...toRefs(state), ...toRefs(reactiveState),
...getters, ...getters,
...actions, ...actions,
} as unknown as Store } as unknown as Store
onScopeDispose(() => {
store.$reset()
})
return store
} }
+1 -1
View File
@@ -90,7 +90,7 @@ export interface Actions<N = any, E = N> {
setInteractive: (isInteractive: boolean) => void setInteractive: (isInteractive: boolean) => void
setState: (state: Partial<FlowOptions<N, E>>) => void setState: (state: Partial<FlowOptions<N, E>>) => void
updateNodePosition: ({ id, diff, dragging }: { id?: string; diff?: XYPosition; dragging?: boolean }) => void updateNodePosition: ({ id, diff, dragging }: { id?: string; diff?: XYPosition; dragging?: boolean }) => void
$destroy: () => void $reset: () => void
} }
export interface Getters<N = any, E = N> { export interface Getters<N = any, E = N> {