From 30b72fb2f03910253381e1d9b0c5913303e4fde9 Mon Sep 17 00:00:00 2001 From: robonen Date: Sat, 22 Feb 2025 23:35:20 +0700 Subject: [PATCH] refactor(packages/vue): use another way to provide state at app level in useContextFactory --- packages/stdlib/src/async/sleep/index.ts | 2 ++ .../useContextFactory/index.test.ts | 2 +- .../composables/useContextFactory/index.ts | 24 ++++++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/stdlib/src/async/sleep/index.ts b/packages/stdlib/src/async/sleep/index.ts index 94a50d9..db3f5fc 100644 --- a/packages/stdlib/src/async/sleep/index.ts +++ b/packages/stdlib/src/async/sleep/index.ts @@ -13,6 +13,8 @@ * sleep(1000).then(() => { * console.log('Hello, World!'); * }); + * + * @since 0.0.3 */ export function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); diff --git a/packages/vue/src/composables/useContextFactory/index.test.ts b/packages/vue/src/composables/useContextFactory/index.test.ts index ad26192..5272ffe 100644 --- a/packages/vue/src/composables/useContextFactory/index.test.ts +++ b/packages/vue/src/composables/useContextFactory/index.test.ts @@ -72,7 +72,7 @@ describe('useContextFactory', () => { const childComponent = mount(Child, { global: { - plugins: [app => context.provide('test', app)], + plugins: [app => context.appProvide(app)('test')], }, }); diff --git a/packages/vue/src/composables/useContextFactory/index.ts b/packages/vue/src/composables/useContextFactory/index.ts index c258d52..4428423 100644 --- a/packages/vue/src/composables/useContextFactory/index.ts +++ b/packages/vue/src/composables/useContextFactory/index.ts @@ -1,13 +1,13 @@ -import {inject, provide, type InjectionKey, type App} from 'vue'; +import { inject, provide, type InjectionKey, type App } from 'vue'; import { VueToolsError } from '../..'; /** * @name useContextFactory - * @category Utilities + * @category State * @description A composable that provides a factory for creating context with unique key * * @param {string} name The name of the context - * @returns {Object} An object with `inject`, `provide` and `key` properties + * @returns {Object} An object with `inject`, `provide`, `appProvide` and `key` properties * @throws {VueToolsError} when the context is not provided * * @example @@ -17,12 +17,12 @@ import { VueToolsError } from '../..'; * const value = inject(); * * @example - * const { inject: injectContext, provide: provideContext } = useContextFactory('MyContext'); + * const { inject: injectContext, appProvide } = useContextFactory('MyContext'); * * // In a plugin * { * install(app) { - * provideContext('Hello World', app); + * appProvide(app)('Hello World'); * } * } * @@ -31,26 +31,32 @@ import { VueToolsError } from '../..'; * * @since 0.0.1 */ -export function useContextFactory(name: string) { +export function useContextFactory(name: string) { const injectionKey: InjectionKey = Symbol(name); const injectContext = (fallback?: Fallback) => { const context = inject(injectionKey, fallback); if (context !== undefined) - return context; + return context; throw new VueToolsError(`useContextFactory: '${name}' context is not provided`); }; - const provideContext = (context: ContextValue, app?: App) => { - (app?.provide ?? provide)(injectionKey, context); + const provideContext = (context: ContextValue) => { + provide(injectionKey, context); + return context; + }; + + const appProvide = (app: App) => (context: ContextValue) => { + app.provide(injectionKey, context); return context; }; return { inject: injectContext, provide: provideContext, + appProvide, key: injectionKey, } } \ No newline at end of file