/* * @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> = Symbol('BubbleContext'); export const globalBubbleContextApi = shallowRef(); export const useBubbleContextProvider = (value: ComputedRef) => { 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(), }, setup(props, { slots }) { useBubbleContextProvider(computed(() => props.value)); return () => { return slots.default?.(); }; }, }); export default BubbleContextProvider;