|
1 | | -//导入类型定义 |
2 | | -import { MonitorOptions, MonitorEvent } from './types'; |
3 | | -import reportData from '../report/index'; |
4 | | -import PerformanceMonitor from '../plugins/performance/index'; |
5 | | -import { dataInterface } from '../report/types'; |
| 1 | +import performance from '../plugins/performance'; |
| 2 | +import error from '../plugins/error'; |
| 3 | +import { setConfig } from '../utils/config'; // Ensure setConfig is properly imported |
6 | 4 |
|
7 | | -export class Monitor { |
8 | | - private options: MonitorOptions; |
9 | | - private performanceMonitor: PerformanceMonitor; // 持有性能监控实例 |
10 | | - private queue: MonitorEvent[] = []; // 事件队列 |
11 | | - |
12 | | - constructor(options: MonitorOptions) { |
13 | | - this.options = { |
14 | | - delay: 1000, |
15 | | - ...options, |
| 5 | +// Initialize the global SDK object with type safety |
| 6 | +declare global { |
| 7 | + interface Window { |
| 8 | + __EzMonitorSDK__?: { |
| 9 | + version: string; |
| 10 | + vue?: boolean; |
| 11 | + react?: boolean; |
16 | 12 | }; |
17 | | - this.performanceMonitor = new PerformanceMonitor(); // 初始化性能监控 |
18 | | - this.init(); |
19 | 13 | } |
| 14 | +} |
20 | 15 |
|
21 | | - //初始化会发送数据到数据库 |
22 | | - private init(): void { |
23 | | - console.log('初始化监听。。。'); |
| 16 | +window.__EzMonitorSDK__ = window.__EzMonitorSDK__ || { |
| 17 | + version: '0.0.1', |
| 18 | +}; |
| 19 | + |
| 20 | +// 针对 Vue 项目的错误捕获 |
| 21 | +export function install(Vue, options) { |
| 22 | + if (window.__EzMonitorSDK__?.vue) return; |
| 23 | + window.__EzMonitorSDK__!.vue = true; |
| 24 | + setConfig(options); // Ensure this function is defined and imported |
| 25 | + const handler = Vue.config.errorHandler; |
| 26 | + Vue.config.errorHandler = function (err, vm, info) { |
| 27 | + if (handler) { |
| 28 | + handler(err, vm, info); |
| 29 | + } |
| 30 | + const reportData = { |
| 31 | + type: 'error', |
| 32 | + subType: 'vue', |
| 33 | + info, |
| 34 | + startTime: window.performance.now(), |
| 35 | + pageURL: window.location.href, |
| 36 | + }; |
| 37 | + console.log('vue error', reportData); |
| 38 | + }; |
| 39 | +} |
24 | 40 |
|
25 | | - // 延迟上报(确保性能数据已收集) |
26 | | - setTimeout(() => { |
27 | | - // 合并性能数据和其他数据(如错误) |
28 | | - const reportPayload: dataInterface = { |
29 | | - performance: this.performanceMonitor.getMetrics(), |
30 | | - event: this.queue, |
31 | | - }; |
| 41 | +// 针对 React 项目的错误捕获 |
| 42 | +export function errorBoundary(err, info) { |
| 43 | + if (window.__EzMonitorSDK__?.react) return; |
| 44 | + window.__EzMonitorSDK__!.react = true; |
| 45 | + const reportData = { |
| 46 | + type: 'error', |
| 47 | + subType: 'react', |
| 48 | + info, |
| 49 | + startTime: window.performance.now(), |
| 50 | + pageURL: window.location.href, |
| 51 | + }; |
| 52 | + console.log('react error', reportData); |
| 53 | +} |
32 | 54 |
|
33 | | - reportData({ |
34 | | - url: this.options.reportUrl, |
35 | | - data: reportPayload, // 包含性能数据+其他数据 |
36 | | - delay: 0, // 立即发送(外层已有setTimeout) |
37 | | - }); |
38 | | - }, this.options.delay); |
39 | | - // 其他初始化逻辑(如错误监听) |
40 | | - } |
| 55 | +// 初始化函数 |
| 56 | +export function init(options) { |
| 57 | + setConfig(options); // Ensure configuration is set |
| 58 | + performance(); // Initialize performance monitoring |
| 59 | + error(); // Initialize error monitoring |
41 | 60 | } |
| 61 | + |
| 62 | +export default { |
| 63 | + install, |
| 64 | + errorBoundary, |
| 65 | + init, |
| 66 | + performance, |
| 67 | + error, |
| 68 | +}; |
0 commit comments