Files
nocodb/packages/nc-gui/composables/useInjectionState/index.ts
Mert E 89f0895de3 fix: experimental frontend optimizations (#8427)
* fix: get rid of barrel files

* chore: get rid of explicit imports

* fix: use explicit import for classes and enums

* fix: use explicit import for enum & class & aliases

* fix: build issues
2024-05-08 15:55:09 +05:30

41 lines
978 B
TypeScript

import type { InjectionKey } from 'vue'
export function useInjectionState<Arguments extends any[], Return>(
composable: (...args: Arguments) => Return,
keyName = 'InjectionState',
): readonly [useInjectionState: (...args: Arguments) => Return, useInjectedState: () => Return | undefined] {
const key: string | InjectionKey<Return> = Symbol(keyName)
let providableState: Return | undefined
const useProvidingState = (...args: Arguments) => {
const providedState = composable(...args)
provide(key, providedState)
providableState = providedState
tryOnScopeDispose(() => {
providableState = undefined
})
return providedState
}
const useInjectedState = () => {
let injection = inject(key, undefined)
if (typeof injection === 'undefined') {
injection = providableState
}
return injection
}
tryOnScopeDispose(() => {
providableState = undefined
})
return [useProvidingState, useInjectedState]
}