import { useEffect, useMemo } from 'react'; import { getOrCreateSharedImpl } from './get-shared'; import { init, type RumService, type ServiceConfig } from './init'; /** * Registers a service on mount and disposes it on unmount. On unmount the * service's `beforeSend`, `allowedTracingUrls`, and any context previously set * via `setServiceContext` are removed atomically. The config is captured on * first render — changing it across renders does not re-register. * * Returns a `RumService` — `{ setServiceContext }`, bound to this service. * The handle is identity-stable across renders. */ export function useRumService(config: ServiceConfig): RumService { useEffect(() => { return init({ service: config }).cleanup; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); return useMemo( () => ({ setServiceContext: context => getOrCreateSharedImpl().setServiceContext(config.name, context), }), // eslint-disable-next-line react-hooks/exhaustive-deps [] ); }