Files
lingji-work-fe/src/components/xt-chat/Bubble/context.ts

45 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-08-21 00:24:36 +08:00
/*
* @Author: RenXiaoDong
* @Date: 2025-08-20 23:17:49
*/
import { computed, defineComponent, inject, provide, shallowRef, triggerRef, unref, watch } from "vue";
import type { ComputedRef, InjectionKey } from "vue";
import { objectType } from "@/utils/type";
import type { BubbleContextProps } from "./types";
const BubbleContextKey: InjectionKey<ComputedRef<BubbleContextProps>> =
Symbol('BubbleContext');
export const globalBubbleContextApi = shallowRef<BubbleContextProps>();
export const useBubbleContextProvider = (value: ComputedRef<BubbleContextProps>) => {
provide(BubbleContextKey, value);
watch(
value,
() => {
globalBubbleContextApi.value = unref(value);
triggerRef(globalBubbleContextApi);
},
{ immediate: true, deep: true },
);
};
export const useBubbleContextInject = () => {
return inject(
BubbleContextKey,
computed(() => globalBubbleContextApi.value || {}),
);
};
export const BubbleContextProvider = defineComponent({
props: {
value: objectType<BubbleContextProps>(),
},
setup(props, { slots }) {
useBubbleContextProvider(computed(() => props.value));
return () => {
return slots.default?.();
};
},
});
export default BubbleContextProvider;