mirror of https://github.com/nocodb/nocodb
9 changed files with 57 additions and 48 deletions
@ -0,0 +1,20 @@ |
|||||||
|
import type { InjectionKey } from 'vue' |
||||||
|
|
||||||
|
export function useInjectionState<Arguments extends any[], Return>( |
||||||
|
composable: (...args: Arguments) => Return, |
||||||
|
keyName = 'InjectionState', |
||||||
|
): readonly [useProvidingState: (...args: Arguments) => void, useInjectedState: () => Return | undefined] { |
||||||
|
const key: string | InjectionKey<Return> = Symbol(keyName) |
||||||
|
|
||||||
|
const useProvidingState = (...args: Arguments) => { |
||||||
|
const providedState = composable(...args) |
||||||
|
|
||||||
|
provide(key, providedState) |
||||||
|
|
||||||
|
return providedState |
||||||
|
} |
||||||
|
|
||||||
|
const useInjectedState = () => inject(key) |
||||||
|
|
||||||
|
return [useProvidingState, useInjectedState] |
||||||
|
} |
Loading…
Reference in new issue