@king-one/monitor 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,10 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import { EventEmitter } from "./EventEmitter.mjs";
5
- import { SourceError } from "./SourceError.mjs";
6
- const _ErrorMonitor = class _ErrorMonitor {
1
+ var p = Object.defineProperty;
2
+ var h = (o, t, e) => t in o ? p(o, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : o[t] = e;
3
+ var n = (o, t, e) => h(o, typeof t != "symbol" ? t + "" : t, e);
4
+ import { EventEmitter as c } from "./EventEmitter.mjs";
5
+ import { SourceError as l } from "./SourceError.mjs";
6
+ import { stringify as f } from "./utils.mjs";
7
+ const r = class r {
7
8
  /**
8
9
  * 取消订阅错误上报事件
9
10
  * @param callback - 错误上报回调函数
@@ -14,33 +15,28 @@ const _ErrorMonitor = class _ErrorMonitor {
14
15
  /**
15
16
  * 私有构造函数,防止外部直接实例化
16
17
  */
17
- constructor(options) {
18
- __publicField(this, "options");
19
- __publicField(this, "event");
20
- __publicField(this, "source");
18
+ constructor(t) {
19
+ n(this, "options");
20
+ n(this, "event");
21
+ n(this, "source");
21
22
  // 错误事件类型常量
22
- __publicField(this, "ERROR_REPORTED", "error-reported");
23
- this.options = this.validateOptions(options);
24
- this.event = new EventEmitter();
25
- this.source = new SourceError();
26
- this.init();
27
- if (options.integrations)
28
- this.initIntegrations();
23
+ n(this, "ERROR_REPORTED", "error-reported");
24
+ this.options = this.validateOptions(t), this.event = new c(), this.source = new l(), this.init(), t.integrations && this.initIntegrations();
29
25
  }
30
26
  /**
31
27
  * 订阅错误上报事件
32
28
  * @param callback - 错误上报回调函数
33
29
  */
34
- onErrorReported(callback) {
35
- this.event.on(this.ERROR_REPORTED, callback);
30
+ onErrorReported(t) {
31
+ this.event.on(this.ERROR_REPORTED, t);
36
32
  }
37
33
  initIntegrations() {
38
- var _a;
39
- (_a = this.options.integrations) == null ? void 0 : _a.forEach((integration) => {
34
+ var t;
35
+ (t = this.options.integrations) == null || t.forEach((e) => {
40
36
  try {
41
- integration.install(this);
42
- } catch (error) {
43
- console.error(`Failed to initialize integration ${integration.name}:`, error);
37
+ e.install(this);
38
+ } catch (i) {
39
+ console.error(`Failed to initialize integration ${e.name}:`, i);
44
40
  }
45
41
  });
46
42
  }
@@ -49,10 +45,9 @@ const _ErrorMonitor = class _ErrorMonitor {
49
45
  * @throws {Error} 当实例未初始化时抛出错误
50
46
  */
51
47
  static getInstance() {
52
- if (!_ErrorMonitor.instance) {
48
+ if (!r.instance)
53
49
  throw new Error("ErrorMonitor未初始化,请先调用initialize方法");
54
- }
55
- return _ErrorMonitor.instance;
50
+ return r.instance;
56
51
  }
57
52
  /**
58
53
  * 静态方法:手动上报错误信息
@@ -79,17 +74,15 @@ const _ErrorMonitor = class _ErrorMonitor {
79
74
  * @returns 处理后的完整配置项
80
75
  * @throws {Error} 当必要配置项缺失时抛出错误
81
76
  */
82
- validateOptions(options) {
83
- if (!options.url) {
77
+ validateOptions(t) {
78
+ if (!t.url)
84
79
  throw new Error("上报地址url不能为空");
85
- }
86
- if (!options.appId) {
87
- throw new Error("应用标识appId不能为空");
88
- }
80
+ if (!t.appId)
81
+ throw new Error("平台标识appId不能为空");
89
82
  return {
90
83
  env: "development",
91
84
  sampleRate: 1,
92
- ...options
85
+ ...t
93
86
  };
94
87
  }
95
88
  /**
@@ -97,8 +90,8 @@ const _ErrorMonitor = class _ErrorMonitor {
97
90
  * 包括:JS运行时错误、Promise异常、资源加载错误
98
91
  */
99
92
  init() {
100
- this.onErrorReported((error) => {
101
- this.reportError(error);
93
+ this.onErrorReported((t) => {
94
+ this.reportError(t);
102
95
  });
103
96
  }
104
97
  /**
@@ -117,61 +110,53 @@ const _ErrorMonitor = class _ErrorMonitor {
117
110
  * 上报错误信息到服务器
118
111
  * @param errorInfo - 错误信息对象
119
112
  */
120
- report(errorInfo) {
121
- if (Math.random() > (this.options.sampleRate || 1)) {
113
+ report(t) {
114
+ if (Math.random() > (this.options.sampleRate || 1))
122
115
  return;
123
- }
124
- let reportData = {
125
- ...errorInfo,
116
+ let e = {
117
+ ...t,
126
118
  appId: this.options.appId,
127
119
  env: this.options.env
128
- };
120
+ }, i = this.options.url;
121
+ if (this.options.paramsSerializer) {
122
+ const s = this.options.paramsSerializer();
123
+ i = `${i}?${f(s)}`;
124
+ }
129
125
  if (this.options.beforeSend) {
130
- const result = this.options.beforeSend(reportData);
131
- if (!result) {
126
+ const s = this.options.beforeSend(e);
127
+ if (!s)
132
128
  return;
133
- }
134
- reportData = result;
135
- }
136
- if (navigator.sendBeacon) {
137
- navigator.sendBeacon(this.options.url, JSON.stringify(reportData));
138
- } else {
139
- fetch(this.options.url, {
140
- method: "POST",
141
- body: JSON.stringify(reportData),
142
- headers: {
143
- "Content-Type": "application/json"
144
- },
145
- keepalive: true
146
- }).catch(() => {
147
- });
129
+ e = s;
148
130
  }
131
+ navigator.sendBeacon ? navigator.sendBeacon(i, JSON.stringify(e)) : fetch(i, {
132
+ method: "POST",
133
+ body: JSON.stringify(e),
134
+ headers: {
135
+ "Content-Type": "text/plain"
136
+ },
137
+ keepalive: !0
138
+ }).catch(() => {
139
+ });
149
140
  }
150
141
  /**
151
142
  * 标准化错误信息
152
143
  * @param error - 原始错误信息
153
144
  * @returns 标准化后的错误信息对象
154
145
  */
155
- normalizeError(error) {
156
- if (this.isErrorInfo(error)) {
157
- return {
158
- ...error,
159
- extraInfo: this.options.extraInfo,
160
- ...this.getClientInfo()
161
- };
162
- }
163
- if (error instanceof Error) {
164
- return {
165
- type: error.name,
166
- message: error.message,
167
- stack: error.stack,
168
- extraInfo: this.options.extraInfo,
169
- ...this.getClientInfo()
170
- };
171
- }
172
- return {
146
+ normalizeError(t) {
147
+ return this.isErrorInfo(t) ? {
148
+ ...t,
149
+ extraInfo: this.options.extraInfo,
150
+ ...this.getClientInfo()
151
+ } : t instanceof Error ? {
152
+ type: t.name,
153
+ message: t.message,
154
+ stack: t.stack,
155
+ extraInfo: this.options.extraInfo,
156
+ ...this.getClientInfo()
157
+ } : {
173
158
  type: "Error",
174
- message: error,
159
+ message: t,
175
160
  extraInfo: this.options.extraInfo,
176
161
  ...this.getClientInfo()
177
162
  };
@@ -181,16 +166,14 @@ const _ErrorMonitor = class _ErrorMonitor {
181
166
  * @param error - 待检查的错误信息
182
167
  * @returns 是否为 ErrorInfo 类型
183
168
  */
184
- isErrorInfo(error) {
185
- return typeof error === "object" && error !== null && "type" in error && "message" in error;
169
+ isErrorInfo(t) {
170
+ return typeof t == "object" && t !== null && "type" in t && "message" in t;
186
171
  }
187
- reportError(error) {
188
- if (!_ErrorMonitor.instance) {
172
+ reportError(t) {
173
+ if (!r.instance)
189
174
  throw new Error("ErrorMonitor未初始化,请先调用initialize方法");
190
- }
191
- const errorInfo = this.normalizeError(error);
192
- console.log("errorInfo", errorInfo);
193
- this.report(errorInfo);
175
+ const e = this.normalizeError(t);
176
+ console.log("errorInfo", e), this.report(e);
194
177
  }
195
178
  /**
196
179
  * 初始化错误监控实例
@@ -198,36 +181,32 @@ const _ErrorMonitor = class _ErrorMonitor {
198
181
  * @returns ErrorMonitor实例
199
182
  * @throws {Error} 当重复初始化或配置项缺失时抛出错误
200
183
  */
201
- static initialize(options) {
202
- if (_ErrorMonitor.instance) {
184
+ static initialize(t) {
185
+ if (r.instance)
203
186
  throw new Error("ErrorMonitor已经初始化,请勿重复初始化");
204
- }
205
- _ErrorMonitor.instance = new _ErrorMonitor(options);
206
- return _ErrorMonitor.instance;
187
+ return r.instance = new r(t), r.instance;
207
188
  }
208
189
  /**
209
190
  * 销毁实例,清理资源
210
191
  */
211
192
  static destroy() {
212
- if (_ErrorMonitor.instance) {
213
- _ErrorMonitor.instance = null;
214
- }
193
+ r.instance && (r.instance = null);
215
194
  }
216
- static captureError(error) {
217
- _ErrorMonitor.getInstance().reportError(error);
195
+ static captureError(t) {
196
+ r.getInstance().reportError(t);
218
197
  }
219
- emitError(error) {
220
- this.event.emit(this.ERROR_REPORTED, error);
198
+ emitError(t) {
199
+ this.event.emit(this.ERROR_REPORTED, t);
221
200
  }
222
- async getErrorSource(error) {
223
- return this.source.getErrorSource(error);
201
+ async getErrorSource(t) {
202
+ return this.source.getErrorSource(t);
224
203
  }
225
204
  get env() {
226
205
  return this.options.env;
227
206
  }
228
207
  };
229
- __publicField(_ErrorMonitor, "instance", null);
230
- let ErrorMonitor = _ErrorMonitor;
208
+ n(r, "instance", null);
209
+ let a = r;
231
210
  export {
232
- ErrorMonitor
211
+ a as ErrorMonitor
233
212
  };
@@ -1,10 +1,12 @@
1
- import { ErrorMonitor } from "./core.mjs";
2
- import { SourceError } from "./SourceError.mjs";
3
- import { VueError } from "./integrations/VueError.mjs";
4
- import { BrowserError } from "./integrations/BrowserError.mjs";
1
+ import { ErrorMonitor as e } from "./core.mjs";
2
+ import { SourceError as m } from "./SourceError.mjs";
3
+ import { VueError as f } from "./integrations/VueError.mjs";
4
+ import { BrowserError as E } from "./integrations/BrowserError.mjs";
5
+ import { timestampToBase66 as a } from "./utils.mjs";
5
6
  export {
6
- BrowserError,
7
- ErrorMonitor,
8
- SourceError,
9
- VueError
7
+ E as BrowserError,
8
+ e as ErrorMonitor,
9
+ m as SourceError,
10
+ f as VueError,
11
+ a as timestampToBase66
10
12
  };
@@ -1,76 +1,67 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- import { getClientInfo } from "../utils.mjs";
5
- class BrowserError {
1
+ var a = Object.defineProperty;
2
+ var m = (t, r, o) => r in t ? a(t, r, { enumerable: !0, configurable: !0, writable: !0, value: o }) : t[r] = o;
3
+ var i = (t, r, o) => m(t, typeof r != "symbol" ? r + "" : r, o);
4
+ import { getClientInfo as n } from "../utils.mjs";
5
+ class c {
6
6
  constructor() {
7
- __publicField(this, "name", "BrowserError");
8
- __publicField(this, "monitor", null);
7
+ i(this, "name", "BrowserError");
8
+ i(this, "monitor", null);
9
9
  }
10
- install(monitor) {
11
- this.monitor = monitor;
12
- window.addEventListener("error", (event) => {
13
- this.handleJsError(event);
14
- }, true);
15
- window.addEventListener("unhandledrejection", (event) => {
16
- this.handlePromiseError(event);
17
- });
18
- window.addEventListener("error", (event) => {
19
- if (event.target && event.target.tagName) {
20
- this.handleResourceError(event);
21
- }
22
- }, true);
10
+ install(r) {
11
+ this.monitor = r, window.addEventListener("error", (o) => {
12
+ this.handleJsError(o);
13
+ }, !0), window.addEventListener("unhandledrejection", (o) => {
14
+ this.handlePromiseError(o);
15
+ }), window.addEventListener("error", (o) => {
16
+ o.target && o.target.tagName && this.handleResourceError(o);
17
+ }, !0);
23
18
  }
24
19
  /**
25
20
  * 处理 JS 运行时错误
26
21
  * @param event - 错误事件对象
27
22
  */
28
- handleJsError(event) {
29
- var _a, _b;
30
- const errorInfo = {
31
- type: (_a = event.error) == null ? void 0 : _a.name,
32
- message: event.message,
23
+ handleJsError(r) {
24
+ var e, s;
25
+ const o = {
26
+ type: (e = r.error) == null ? void 0 : e.name,
27
+ message: r.message,
33
28
  source: {
34
- fileName: event.filename,
35
- lineNumber: event.lineno,
36
- columnNumber: event.colno
29
+ fileName: r.filename,
30
+ lineNumber: r.lineno,
31
+ columnNumber: r.colno
37
32
  },
38
- stack: (_b = event.error) == null ? void 0 : _b.stack,
39
- ...getClientInfo()
33
+ stack: (s = r.error) == null ? void 0 : s.stack,
34
+ ...n()
40
35
  };
41
- if (this.monitor)
42
- this.monitor.emitError(errorInfo);
36
+ this.monitor && this.monitor.emitError(o);
43
37
  }
44
38
  /**
45
39
  * 处理 Promise 异常
46
40
  * @param event - Promise 异常事件对象
47
41
  */
48
- handlePromiseError(event) {
49
- var _a, _b;
50
- const errorInfo = {
42
+ handlePromiseError(r) {
43
+ var e, s;
44
+ const o = {
51
45
  type: "NetworkError",
52
- message: ((_a = event.reason) == null ? void 0 : _a.message) || "Promise Error",
53
- stack: (_b = event.reason) == null ? void 0 : _b.stack,
54
- ...getClientInfo()
46
+ message: ((e = r.reason) == null ? void 0 : e.message) || "Promise Error",
47
+ stack: (s = r.reason) == null ? void 0 : s.stack,
48
+ ...n()
55
49
  };
56
- if (this.monitor)
57
- this.monitor.emitError(errorInfo);
50
+ this.monitor && this.monitor.emitError(o);
58
51
  }
59
52
  /**
60
53
  * 处理资源加载错误
61
54
  * @param event - 资源错误事件对象
62
55
  */
63
- handleResourceError(event) {
64
- const target = event.target;
65
- const errorInfo = {
56
+ handleResourceError(r) {
57
+ const o = r.target, e = {
66
58
  type: "ResourceError",
67
- message: `资源加载失败: ${target.tagName} ${target.src || target.href}`,
68
- ...getClientInfo()
59
+ message: `资源加载失败: ${o.tagName} ${o.src || o.href}`,
60
+ ...n()
69
61
  };
70
- if (this.monitor)
71
- this.monitor.emitError(errorInfo);
62
+ this.monitor && this.monitor.emitError(e);
72
63
  }
73
64
  }
74
65
  export {
75
- BrowserError
66
+ c as BrowserError
76
67
  };
@@ -1,27 +1,25 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
- class VueError {
5
- constructor(app) {
6
- __publicField(this, "name", "VueError");
7
- __publicField(this, "app");
8
- this.app = app;
1
+ var t = Object.defineProperty;
2
+ var o = (a, e, s) => e in a ? t(a, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : a[e] = s;
3
+ var r = (a, e, s) => o(a, typeof e != "symbol" ? e + "" : e, s);
4
+ class p {
5
+ constructor(e) {
6
+ r(this, "name", "VueError");
7
+ r(this, "app");
8
+ this.app = e;
9
9
  }
10
- install(monitor) {
11
- if (monitor.env === "production") {
12
- this.app.config.errorHandler = async (error) => {
13
- const source = await monitor.getErrorSource(error);
14
- console.log(source);
15
- monitor.emitError({
16
- type: error.name,
17
- message: error.message,
18
- stack: error.stack,
19
- source
20
- });
21
- };
22
- }
10
+ install(e) {
11
+ e.env === "production" && (this.app.config.errorHandler = async (s) => {
12
+ const c = await e.getErrorSource(s);
13
+ console.log(c), e.emitError({
14
+ type: s.name,
15
+ message: s.message,
16
+ stack: s.stack,
17
+ source: c,
18
+ level: "error"
19
+ });
20
+ });
23
21
  }
24
22
  }
25
23
  export {
26
- VueError
24
+ p as VueError
27
25
  };
@@ -1,4 +1,4 @@
1
- function getClientInfo() {
1
+ function x() {
2
2
  return {
3
3
  url: window.location.href,
4
4
  userAgent: navigator.userAgent,
@@ -6,6 +6,101 @@ function getClientInfo() {
6
6
  timestamp: Date.now()
7
7
  };
8
8
  }
9
+ function V(i, c = {}) {
10
+ const {
11
+ encode: t = !0,
12
+ encodeValuesOnly: f = !1,
13
+ arrayFormat: p = "indices",
14
+ skipNulls: g = !1,
15
+ delimiter: k = "&",
16
+ strictNullHandling: I = !1,
17
+ sort: w,
18
+ filter: $,
19
+ allowDots: y = !1,
20
+ serializeDate: S = (r) => r.toISOString(),
21
+ format: j = "RFC3986",
22
+ addQueryPrefix: C = !1,
23
+ charset: D = "utf-8"
24
+ } = c, l = [], E = D === "iso-8859-1" ? O : encodeURIComponent, a = (r) => t ? E(r) : r, d = (r, e, b = null) => {
25
+ const o = b ? `${b}[${r}]` : r;
26
+ if ($) {
27
+ const n = $(o, e);
28
+ if (n === void 0)
29
+ return;
30
+ e = n;
31
+ }
32
+ if (e === null) {
33
+ I ? l.push(a(o) + (f ? "" : "=")) : g || l.push(`${a(o)}=`);
34
+ return;
35
+ }
36
+ if ((typeof e == "boolean" || typeof e == "number") && (e = String(e)), e instanceof Date && (e = S(e)), Array.isArray(e)) {
37
+ if (e.length === 0 && g)
38
+ return;
39
+ switch (p) {
40
+ case "indices":
41
+ e.forEach((s, u) => {
42
+ d(String(u), s, o);
43
+ });
44
+ break;
45
+ case "brackets":
46
+ e.forEach((s) => {
47
+ d("", s, o);
48
+ });
49
+ break;
50
+ case "repeat":
51
+ e.forEach((s) => {
52
+ d(o, s);
53
+ });
54
+ break;
55
+ case "comma":
56
+ const n = e.join(",");
57
+ n.length > 0 && l.push(
58
+ `${a(o)}=${a(n)}`
59
+ );
60
+ break;
61
+ }
62
+ } else if (typeof e == "object")
63
+ Object.keys(e).forEach((n) => {
64
+ const s = y ? "." : "[", u = y ? "" : "]";
65
+ d(
66
+ n,
67
+ e[n],
68
+ `${o}${s}${n}${u}`
69
+ );
70
+ });
71
+ else {
72
+ const n = t && !f ? a(o) : o, s = a(String(e));
73
+ l.push(`${n}=${s}`);
74
+ }
75
+ };
76
+ let m = Object.keys(i);
77
+ w && (m = m.sort(w)), m.forEach((r) => {
78
+ const e = i[r];
79
+ d(r, e);
80
+ });
81
+ let h = l.join(k);
82
+ return C && h.length > 0 && (h = `?${h}`), h;
83
+ }
84
+ function O(i) {
85
+ return encodeURIComponent(i).replace(/%[0-9A-F]{2}/g, (c) => {
86
+ const t = Number.parseInt(c.substring(1), 16);
87
+ return t >= 128 && t <= 255 ? String.fromCharCode(t) : c;
88
+ });
89
+ }
90
+ function A(i, c = Date.now()) {
91
+ if (i.length !== 66)
92
+ throw new Error("BASE66_CHARS must contain exactly 66 characters");
93
+ let t = c, f = "";
94
+ if (t === 0)
95
+ return i[0];
96
+ for (; t > 0; ) {
97
+ const p = t % 66;
98
+ f = i[p] + f, t = Math.floor(t / 66);
99
+ }
100
+ return f;
101
+ }
9
102
  export {
10
- getClientInfo
103
+ x as getClientInfo,
104
+ V as stringify,
105
+ A as timestampToBase66
11
106
  };
package/dist/lib/index.js CHANGED
@@ -1,11 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- require("./src/index.js");
4
- const core = require("./src/core.js");
5
- const SourceError = require("./src/SourceError.js");
6
- const VueError = require("./src/integrations/VueError.js");
7
- const BrowserError = require("./src/integrations/BrowserError.js");
8
- exports.ErrorMonitor = core.ErrorMonitor;
9
- exports.SourceError = SourceError.SourceError;
10
- exports.VueError = VueError.VueError;
11
- exports.BrowserError = BrowserError.BrowserError;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});require("./src/index.js");const r=require("./src/utils.js"),e=require("./src/core.js"),o=require("./src/SourceError.js"),t=require("./src/integrations/VueError.js"),u=require("./src/integrations/BrowserError.js");exports.timestampToBase66=r.timestampToBase66;exports.ErrorMonitor=e.ErrorMonitor;exports.SourceError=o.SourceError;exports.VueError=t.VueError;exports.BrowserError=u.BrowserError;
@@ -1,77 +1 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
- class EventEmitter {
7
- constructor() {
8
- __publicField(this, "events", /* @__PURE__ */ new Map());
9
- }
10
- /**
11
- * 订阅事件
12
- * @param eventName 事件名称
13
- * @param handler 事件处理函数
14
- */
15
- on(eventName, handler) {
16
- if (!this.events.has(eventName)) {
17
- this.events.set(eventName, /* @__PURE__ */ new Set());
18
- }
19
- const handlers = this.events.get(eventName);
20
- handlers.add(handler);
21
- }
22
- /**
23
- * 取消订阅事件
24
- * @param eventName 事件名称
25
- * @param handler 事件处理函数
26
- */
27
- off(eventName, handler) {
28
- const handlers = this.events.get(eventName);
29
- if (handlers) {
30
- handlers.delete(handler);
31
- if (handlers.size === 0) {
32
- this.events.delete(eventName);
33
- }
34
- }
35
- }
36
- /**
37
- * 触发事件
38
- * @param eventName 事件名称
39
- * @param args 事件参数
40
- */
41
- emit(eventName, ...args) {
42
- const handlers = this.events.get(eventName);
43
- if (handlers) {
44
- handlers.forEach((handler) => {
45
- try {
46
- handler(...args);
47
- } catch (error) {
48
- console.error(`Error in event handler for ${eventName}:`, error);
49
- }
50
- });
51
- }
52
- }
53
- /**
54
- * 清除所有订阅
55
- */
56
- clear() {
57
- this.events.clear();
58
- }
59
- /**
60
- * 获取指定事件的订阅者数量
61
- * @param eventName 事件名称
62
- * @returns 订阅者数量
63
- */
64
- getSubscriberCount(eventName) {
65
- const handlers = this.events.get(eventName);
66
- return handlers ? handlers.size : 0;
67
- }
68
- /**
69
- * 检查是否有订阅者
70
- * @param eventName 事件名称
71
- * @returns 是否有订阅者
72
- */
73
- hasSubscribers(eventName) {
74
- return this.getSubscriberCount(eventName) > 0;
75
- }
76
- }
77
- exports.EventEmitter = EventEmitter;
1
+ "use strict";var o=Object.defineProperty;var c=(r,e,t)=>e in r?o(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t;var n=(r,e,t)=>c(r,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});class l{constructor(){n(this,"events",new Map)}on(e,t){this.events.has(e)||this.events.set(e,new Set),this.events.get(e).add(t)}off(e,t){const s=this.events.get(e);s&&(s.delete(t),s.size===0&&this.events.delete(e))}emit(e,...t){const s=this.events.get(e);s&&s.forEach(i=>{try{i(...t)}catch(h){console.error(`Error in event handler for ${e}:`,h)}})}clear(){this.events.clear()}getSubscriberCount(e){const t=this.events.get(e);return t?t.size:0}hasSubscribers(e){return this.getSubscriberCount(e)>0}}exports.EventEmitter=l;