import type { Room } from 'livekit-client'; import { computed, inject, MaybeRef, provide, shallowRef, toValue, type InjectionKey, type ShallowRef, } from 'vue'; export type RoomContext = ShallowRef; const RoomContextKey: InjectionKey = Symbol('livekit-room-context'); export function useProvideRoomContext(initialValue: MaybeRef): RoomContext { const ctx = shallowRef(initialValue); console.log('>>>>>>>>>', ctx); provide>(RoomContextKey, ctx); return ctx; } export function useMaybeRoomContext(): RoomContext | undefined { return inject(RoomContextKey); } export function useRoomContext(): RoomContext { const ctx = useMaybeRoomContext(); if (!ctx) { throw Error('tried to access room context outside of livekit room component'); } return ctx; } export function useEnsureRoom(room?: MaybeRef): RoomContext { const ctx = useMaybeRoomContext(); const r = computed(() => toValue(room) ?? toValue(ctx)); if (!r.value) { throw new Error( 'No room provided, make sure you are inside a Room context or pass the room explicitly', ); } return r as RoomContext; }