-
-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(core): add overloads to useVueFlow (#1481)
* refactor(core): add overloads to `useVueFlow` * chore(changeset): add * chore(core): cleanup
- Loading branch information
1 parent
659ca6a
commit 192b154
Showing
5 changed files
with
37 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@vue-flow/core": minor | ||
--- | ||
|
||
Add overloads to `useVueFlow`. Allows calling `useVueFlow` with an `id` string only while emitting a deprecation warning for using the options obj. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { tryOnScopeDispose } from '@vueuse/core' | ||
import type { EffectScope } from 'vue' | ||
import { effectScope, getCurrentScope, inject, provide, watch } from 'vue' | ||
import { effectScope, getCurrentInstance, getCurrentScope, inject, provide, watch } from 'vue' | ||
import type { EdgeChange, FlowOptions, NodeChange, VueFlowStore } from '../types' | ||
import { warn } from '../utils' | ||
import { ErrorCode, VueFlowError, warn } from '../utils' | ||
import { VueFlow } from '../context' | ||
import { Storage } from '../utils/storage' | ||
|
||
|
@@ -18,15 +18,21 @@ type Scope = (EffectScope & { vueFlowId: string }) | undefined | |
* If no store instance is found in context, a new store instance is created and registered in storage | ||
* | ||
* @public | ||
* @param options - optional options to initialize the store instance | ||
* @returns a vue flow store instance | ||
* @param idOrOpts - id of the store instance or options to create a new store instance | ||
Check warning on line 22 in packages/core/src/composables/useVueFlow.ts GitHub Actions / build-and-test (ubuntu-latest, 18)
|
||
*/ | ||
export function useVueFlow(options?: FlowOptions): VueFlowStore { | ||
export function useVueFlow(id?: string): VueFlowStore | ||
export function useVueFlow(options?: FlowOptions): VueFlowStore | ||
export function useVueFlow(idOrOpts?: any): VueFlowStore { | ||
const storage = Storage.getInstance() | ||
|
||
const scope = getCurrentScope() as Scope | ||
|
||
const id = options?.id | ||
const isOptsObj = typeof idOrOpts === 'object' | ||
|
||
const options = isOptsObj ? idOrOpts : undefined | ||
|
||
const id = options?.id ?? idOrOpts | ||
const vueFlowId = scope?.vueFlowId || id | ||
|
||
let vueFlow: Injection | ||
|
@@ -114,7 +120,7 @@ export function useVueFlow(options?: FlowOptions): VueFlowStore { | |
}) | ||
} else { | ||
// If options were passed, overwrite state with the options' values | ||
if (options) { | ||
if (isOptsObj) { | ||
vueFlow.setState(options) | ||
} | ||
} | ||
|
@@ -126,8 +132,13 @@ export function useVueFlow(options?: FlowOptions): VueFlowStore { | |
scope.vueFlowId = vueFlow.id | ||
} | ||
|
||
if (options) { | ||
warn('options are deprecated and will be removed in the next major version. Use props on the `<VueFlow>` component instead.') | ||
if (isOptsObj) { | ||
const instance = getCurrentInstance() | ||
|
||
// ignore the warning if we are in a VueFlow component | ||
if (instance?.type.name !== 'VueFlow') { | ||
vueFlow.emits.error(new VueFlowError(ErrorCode.USEVUEFLOW_OPTIONS)) | ||
} | ||
} | ||
|
||
return vueFlow | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters