import { useContext } from 'react'; import React, { ReactNode, createContext } from 'react'; import { RTMClient } from '../api/RTMClient'; export interface RTMProviderProps { readonly client: RTMClient; readonly children?: ReactNode; } export const AgoraRTMContext = /* @__PURE__ */ createContext( null ); export function RTMProvider({ client, children }: RTMProviderProps) { return ( {children} ); } function useOptionalRTM(client?: RTMClient | null): RTMClient | null { const clientFromContext = useContext(AgoraRTMContext); return client || clientFromContext; } export function useRtm(client?: RTMClient | null): RTMClient { const resolvedClient = useOptionalRTM(client); if (!resolvedClient) { throw new Error( 'Agora RTM client not found. Should be wrapped in ' ); } return resolvedClient; }