mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 02:44:45 +00:00
refactor(packages/vue): use another way to provide state at app level in useContextFactory
This commit is contained in:
@@ -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));
|
||||||
|
|||||||
@@ -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')],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -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');
|
||||||
* }
|
* }
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
@@ -31,26 +31,32 @@ import { VueToolsError } from '../..';
|
|||||||
*
|
*
|
||||||
* @since 0.0.1
|
* @since 0.0.1
|
||||||
*/
|
*/
|
||||||
export function useContextFactory<ContextValue>(name: string) {
|
export function useContextFactory<ContextValue>(name: string) {
|
||||||
const injectionKey: InjectionKey<ContextValue> = Symbol(name);
|
const injectionKey: InjectionKey<ContextValue> = Symbol(name);
|
||||||
|
|
||||||
const injectContext = <Fallback extends ContextValue = ContextValue>(fallback?: Fallback) => {
|
const injectContext = <Fallback extends ContextValue = ContextValue>(fallback?: Fallback) => {
|
||||||
const context = inject(injectionKey, fallback);
|
const context = inject(injectionKey, fallback);
|
||||||
|
|
||||||
if (context !== undefined)
|
if (context !== undefined)
|
||||||
return context;
|
return context;
|
||||||
|
|
||||||
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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user