refactor(hooks): optimize useContext and update useMixMenuContext

This commit is contained in:
Soybean
2025-07-19 02:40:25 +08:00
parent 32b8f99071
commit c965140b87
9 changed files with 50 additions and 48 deletions

View File

@ -1,5 +1,4 @@
import { inject, provide } from 'vue';
import type { InjectionKey } from 'vue';
/**
* Use context
@ -12,7 +11,7 @@ import type { InjectionKey } from 'vue';
* import { ref } from 'vue';
* import { useContext } from '@sa/hooks';
*
* export const { setupStore, useStore } = useContext('demo', () => {
* export const [provideDemoContext, useDemoContext] = useContext('demo', () => {
* const count = ref(0);
*
* function increment() {
@ -35,10 +34,10 @@ import type { InjectionKey } from 'vue';
* <div>A</div>
* </template>
* <script setup lang="ts">
* import { setupStore } from './context';
* import { provideDemoContext } from './context';
*
* setupStore();
* // const { increment } = setupStore(); // also can control the store in the parent component
* provideDemoContext();
* // const { increment } = provideDemoContext(); // also can control the store in the parent component
* </script>
* ``` // B.vue
* ```vue
@ -46,9 +45,9 @@ import type { InjectionKey } from 'vue';
* <div>B</div>
* </template>
* <script setup lang="ts">
* import { useStore } from './context';
* import { useDemoContext } from './context';
*
* const { count, increment } = useStore();
* const { count, increment } = useDemoContext();
* </script>
* ```;
*
@ -57,40 +56,41 @@ import type { InjectionKey } from 'vue';
* @param contextName Context name
* @param fn Context function
*/
export default function useContext<T extends (...args: any[]) => any>(contextName: string, fn: T) {
type Context = ReturnType<T>;
export default function useContext<Arguments extends Array<any>, T>(
contextName: string,
composable: (...args: Arguments) => T
) {
const key = Symbol(contextName);
const { useProvide, useInject: useStore } = createContext<Context>(contextName);
/**
* Injects the context value.
*
* @param consumerName - The name of the component that is consuming the context. If provided, the component must be
* used within the context provider.
* @param defaultValue - The default value to return if the context is not provided.
* @returns The context value.
*/
const useInject = <N extends string | null | undefined = undefined>(
consumerName?: N,
defaultValue?: T
): N extends null | undefined ? T | null : T => {
const value = inject(key, defaultValue);
function setupStore(...args: Parameters<T>) {
const context: Context = fn(...args);
return useProvide(context);
}
if (consumerName && !value) {
throw new Error(`\`${consumerName}\` must be used within \`${contextName}\``);
}
return {
/** Setup store in the parent component */
setupStore,
/** Use store in the child component */
useStore
// @ts-expect-error - we want to return null if the value is undefined or null
return value || null;
};
}
/** Create context */
function createContext<T>(contextName: string) {
const injectKey: InjectionKey<T> = Symbol(contextName);
const useProvide = (...args: Arguments) => {
const value = composable(...args);
function useProvide(context: T) {
provide(injectKey, context);
provide(key, value);
return context;
}
function useInject() {
return inject(injectKey) as T;
}
return {
useProvide,
useInject
return value;
};
return [useProvide, useInject] as const;
}