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

@@ -72,7 +72,7 @@ describe('useContextFactory', () => {
const childComponent = mount(Child, {
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 '../..';
/**
* @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<ContextValue>(name: string) {
export function useContextFactory<ContextValue>(name: string) {
const injectionKey: InjectionKey<ContextValue> = Symbol(name);
const injectContext = <Fallback extends ContextValue = ContextValue>(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,
}
}