refactor(vue-flow)!: remove reactive options

This commit is contained in:
Braks
2022-05-27 23:36:01 +02:00
parent b63d3da0c0
commit e2f3da8e1b
@@ -1,5 +1,4 @@
import type { EffectScope } from 'vue' import type { EffectScope } from 'vue'
import type { MaybeRef } from '@vueuse/core'
import type { FlowOptions, FlowProps, State, VueFlowStore } from '~/types' import type { FlowOptions, FlowProps, State, VueFlowStore } from '~/types'
import { VueFlow } from '~/context' import { VueFlow } from '~/context'
import { useActions, useGetters, useState } from '~/store' import { useActions, useGetters, useState } from '~/store'
@@ -80,16 +79,13 @@ export class Storage {
type Injection = VueFlowStore | null | undefined type Injection = VueFlowStore | null | undefined
type Scope = (EffectScope & { vueFlowId: string }) | undefined type Scope = (EffectScope & { vueFlowId: string }) | undefined
type Options = { [key in keyof FlowProps]: MaybeRef<FlowProps[key]> }
export default (options?: Options): VueFlowStore => {
const reactiveOptions = options ? reactive(options) : undefined
export default (options?: FlowProps): VueFlowStore => {
const storage = Storage.getInstance() const storage = Storage.getInstance()
const scope = getCurrentScope() as Scope const scope = getCurrentScope() as Scope
const id = reactiveOptions?.id const id = options?.id
const vueFlowId = scope?.vueFlowId || id const vueFlowId = scope?.vueFlowId || id
let vueFlow: Injection let vueFlow: Injection
@@ -119,24 +115,14 @@ export default (options?: Options): VueFlowStore => {
if (!vueFlow || (vueFlow && id && id !== vueFlow.id)) { if (!vueFlow || (vueFlow && id && id !== vueFlow.id)) {
const name = id ?? storage.getId() const name = id ?? storage.getId()
vueFlow = storage.create(name, reactiveOptions) vueFlow = storage.create(name, options)
if (scope) { if (scope) {
scope.vueFlowId = name scope.vueFlowId = name
onBeforeMount(() => {
if (reactiveOptions) {
scope.run(() => {
watch(reactiveOptions, (opts) => {
vueFlow?.setState(opts)
})
})
}
})
} }
} else { } else {
// if composable was called with additional options after initialization, overwrite state with the options values // if composable was called with additional options after initialization, overwrite state with the options values
if (reactiveOptions) vueFlow.setState(reactiveOptions) if (options) vueFlow.setState(options)
} }
/** /**