import type { Participant } from 'livekit-client'; import { inject, provide, shallowRef, type InjectionKey, type ShallowRef, computed, toValue, type MaybeRef, type ComputedRef, } from 'vue'; export type ParticipantContext = ShallowRef; const ParticipantContextKey: InjectionKey = Symbol( 'livekit-participant-context', ); function useProvideParticipantContext(initialValue: Participant): ParticipantContext { const context = shallowRef(initialValue); provide(ParticipantContextKey, context); return context; } function useParticipantContextRaw(): ParticipantContext | undefined { return inject(ParticipantContextKey, undefined); } export { useParticipantContextRaw, useProvideParticipantContext }; export function useMaybeParticipantContext(): ParticipantContext | undefined { return useParticipantContextRaw(); } export function useParticipantContext(): ParticipantContext { return useParticipantContextRaw() ?? shallowRef(undefined); } export function useResolveParticipant( participantRef?: MaybeRef, ): ComputedRef { const participantFromContext = useMaybeParticipantContext(); return computed(() => { const participantFromProp = toValue(participantRef); if (participantFromProp) { return participantFromProp; } const participantFromCtx = participantFromContext?.value; if (participantFromCtx) { return participantFromCtx; } return undefined; }); } export function useEnsureParticipant( participantRef?: MaybeRef, ): ComputedRef { const resolvedParticipant = useResolveParticipant(participantRef); return computed(() => { const p = resolvedParticipant.value; if (!p) { throw new Error( 'Participant could not be found in props or context. Please ensure that a participant is provided.', ); } return p; }); }