|
| 1 | +import type { RumPlugin, RumPublicApi, StartRumResult } from '@datadog/browser-rum-core' |
| 2 | + |
| 3 | +let globalPublicApi: RumPublicApi | undefined |
| 4 | +let globalConfiguration: VuePluginConfiguration | undefined |
| 5 | +let globalAddError: StartRumResult['addError'] | undefined |
| 6 | + |
| 7 | +type InitSubscriber = (configuration: VuePluginConfiguration, rumPublicApi: RumPublicApi) => void |
| 8 | +type StartSubscriber = (addError: StartRumResult['addError']) => void |
| 9 | + |
| 10 | +const onRumInitSubscribers: InitSubscriber[] = [] |
| 11 | +const onRumStartSubscribers: StartSubscriber[] = [] |
| 12 | + |
| 13 | +export interface VuePluginConfiguration { |
| 14 | + router?: boolean |
| 15 | +} |
| 16 | + |
| 17 | +export type VuePlugin = Required<RumPlugin> |
| 18 | + |
| 19 | +export function vuePlugin(configuration: VuePluginConfiguration = {}): VuePlugin { |
| 20 | + return { |
| 21 | + name: 'vue', |
| 22 | + onInit({ publicApi, initConfiguration }) { |
| 23 | + globalPublicApi = publicApi |
| 24 | + globalConfiguration = configuration |
| 25 | + for (const subscriber of onRumInitSubscribers) { |
| 26 | + subscriber(globalConfiguration, globalPublicApi) |
| 27 | + } |
| 28 | + if (configuration.router) { |
| 29 | + initConfiguration.trackViewsManually = true |
| 30 | + } |
| 31 | + }, |
| 32 | + onRumStart({ addError }) { |
| 33 | + globalAddError = addError |
| 34 | + for (const subscriber of onRumStartSubscribers) { |
| 35 | + if (addError) { |
| 36 | + subscriber(addError) |
| 37 | + } |
| 38 | + } |
| 39 | + }, |
| 40 | + getConfigurationTelemetry() { |
| 41 | + return { router: !!configuration.router } |
| 42 | + }, |
| 43 | + } satisfies RumPlugin |
| 44 | +} |
| 45 | + |
| 46 | +export function onVueInit(callback: InitSubscriber) { |
| 47 | + if (globalConfiguration && globalPublicApi) { |
| 48 | + callback(globalConfiguration, globalPublicApi) |
| 49 | + } else { |
| 50 | + onRumInitSubscribers.push(callback) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +export function onVueStart(callback: StartSubscriber) { |
| 55 | + if (globalAddError) { |
| 56 | + callback(globalAddError) |
| 57 | + } else { |
| 58 | + onRumStartSubscribers.push(callback) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +export function resetVuePlugin() { |
| 63 | + globalPublicApi = undefined |
| 64 | + globalConfiguration = undefined |
| 65 | + globalAddError = undefined |
| 66 | + onRumInitSubscribers.length = 0 |
| 67 | + onRumStartSubscribers.length = 0 |
| 68 | +} |
0 commit comments