1
0
mirror of https://github.com/robonen/tools.git synced 2026-03-20 10:54:44 +00:00

refactor(packages/vue): use another way to provide state at app level in useContextFactory

This commit is contained in:
2025-02-22 23:35:20 +07:00
parent 5594cef31e
commit 30b72fb2f0
3 changed files with 18 additions and 10 deletions

View File

@@ -13,6 +13,8 @@
* sleep(1000).then(() => { * sleep(1000).then(() => {
* console.log('Hello, World!'); * console.log('Hello, World!');
* }); * });
*
* @since 0.0.3
*/ */
export function sleep(ms: number): Promise<void> { export function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms)); return new Promise((resolve) => setTimeout(resolve, ms));

View File

@@ -72,7 +72,7 @@ describe('useContextFactory', () => {
const childComponent = mount(Child, { const childComponent = mount(Child, {
global: { global: {
plugins: [app => context.provide('test', app)], plugins: [app => context.appProvide(app)('test')],
}, },
}); });

View File

@@ -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 '../..'; import { VueToolsError } from '../..';
/** /**
* @name useContextFactory * @name useContextFactory
* @category Utilities * @category State
* @description A composable that provides a factory for creating context with unique key * @description A composable that provides a factory for creating context with unique key
* *
* @param {string} name The name of the context * @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 * @throws {VueToolsError} when the context is not provided
* *
* @example * @example
@@ -17,12 +17,12 @@ import { VueToolsError } from '../..';
* const value = inject(); * const value = inject();
* *
* @example * @example
* const { inject: injectContext, provide: provideContext } = useContextFactory('MyContext'); * const { inject: injectContext, appProvide } = useContextFactory('MyContext');
* *
* // In a plugin * // In a plugin
* { * {
* install(app) { * install(app) {
* provideContext('Hello World', app); * appProvide(app)('Hello World');
* } * }
* } * }
* *
@@ -43,14 +43,20 @@ export function useContextFactory<ContextValue>(name: string) {
throw new VueToolsError(`useContextFactory: '${name}' context is not provided`); throw new VueToolsError(`useContextFactory: '${name}' context is not provided`);
}; };
const provideContext = (context: ContextValue, app?: App) => { const provideContext = (context: ContextValue) => {
(app?.provide ?? provide)(injectionKey, context); provide(injectionKey, context);
return context;
};
const appProvide = (app: App) => (context: ContextValue) => {
app.provide(injectionKey, context);
return context; return context;
}; };
return { return {
inject: injectContext, inject: injectContext,
provide: provideContext, provide: provideContext,
appProvide,
key: injectionKey, key: injectionKey,
} }
} }