Browse Source

feat(gui-v2): add `useInjectionState` composable

# What's changed?

* extends on VueUses' `createInjectionState`
* immediately returns injection state on provide
pull/2942/head
braks 2 years ago
parent
commit
7ad484613e
  1. 1
      packages/nc-gui-v2/composables/index.ts
  2. 19
      packages/nc-gui-v2/composables/useInjectionState/index.ts

1
packages/nc-gui-v2/composables/index.ts

@ -1,5 +1,6 @@
export * from './useApi'
export * from './useGlobal'
export * from './useInjectionState'
export * from './useUIPermission'
export * from './useAttachment'
export * from './useBelongsTo'

19
packages/nc-gui-v2/composables/useInjectionState/index.ts

@ -0,0 +1,19 @@
import type { InjectionKey } from 'vue'
export function useInjectionState<Arguments extends any[], Return>(
composable: (...args: Arguments) => Return,
): readonly [useProvidingState: (...args: Arguments) => void, useInjectedState: () => Return | undefined] {
const key: string | InjectionKey<Return> = Symbol('InjectionState')
const useProvidingState = (...args: Arguments) => {
const providedState = composable(...args)
provide(key, providedState)
return providedState
}
const useInjectedState = () => inject(key)
return [useProvidingState, useInjectedState]
}
Loading…
Cancel
Save