import { debounce, EventType } from '@jolibox/common'; import { ReportHandler, Track, TrackPerformance, CommonReportConfig } from './types'; import { PerformanceType, TrackEvent } from '@jolibox/types'; import { InternalGlobalJSError } from '@jolibox/common'; import { reportError } from './errors/report'; import { context } from '@/common/context'; // Track system event, wrap common config export function createTrack(reportHandler: ReportHandler, common: CommonReportConfig): Track { const track = ( tag: TrackEvent, info: Record | null, originEventType?: EventType ): void => { const { eventType, ...rest } = info ?? {}; const data = { tag, eventType: originEventType ?? ((eventType ?? EventType.Other) as number), ...common, extra: { ...rest } }; if (tag == 'globalJsError') { reportError(new InternalGlobalJSError(JSON.stringify(data))); // window.onerror, 白屏 } else { reportHandler('systemLog', data, context.webviewId); } }; track.debounce = debounce(track, 500, { leading: true }); return track; } export function createTrackPerformance(track: Track): TrackPerformance { const trackPerformance = ( type: PerformanceType, duration: number, data?: Record ): void => { const length = (data && JSON.stringify(data)) ?? 0; const info = { speed_value_type: type, total_duration: duration }; if (length) { Object.assign(info, { extra: length }); } track('joliboxSpeedAnalysis', info); }; trackPerformance.debounce = debounce(trackPerformance, 500, { leading: true }); return trackPerformance; }