diff --git a/packages/nc-gui-v2/composables/useInjectionState/index.ts b/packages/nc-gui-v2/composables/useInjectionState/index.ts index 09933498d6..6fdb541cb3 100644 --- a/packages/nc-gui-v2/composables/useInjectionState/index.ts +++ b/packages/nc-gui-v2/composables/useInjectionState/index.ts @@ -1,23 +1,21 @@ -import type { EffectScope, InjectionKey } from 'vue' -import { getCurrentScope } from 'vue' - -type ExtendedScope = { [key: symbol]: T } & EffectScope +import type { InjectionKey } from 'vue' +import { inject, provide, tryOnScopeDispose } from '#imports' export function useInjectionState( composable: (...args: Arguments) => Return, keyName = 'InjectionState', ): readonly [useInjectionState: (...args: Arguments) => Return, useInjectedState: () => Return | undefined] { - const keySymbol = Symbol(keyName) - const key: string | InjectionKey = keySymbol + const key: string | InjectionKey = Symbol(keyName) + + let providableState: Return | undefined const useProvidingState = (...args: Arguments) => { const providedState = composable(...args) - const currentScope = getCurrentScope() as ExtendedScope - currentScope[keySymbol] = providedState - provide(key, providedState) + providableState = providedState + return providedState } @@ -25,12 +23,15 @@ export function useInjectionState( let injection = inject(key, undefined) if (typeof injection === 'undefined') { - const currentScope = getCurrentScope() as ExtendedScope - injection = currentScope[keySymbol] + injection = providableState } return injection } + tryOnScopeDispose(() => { + providableState = undefined + }) + return [useProvidingState, useInjectedState] }