@polyv/utils-event 3.0.0

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.
package/index.cjs ADDED
@@ -0,0 +1 @@
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=class{constructor(){this.__eventStore={}}__addOnEvent(e,t,n,r){if(!t||!n)return;let i=this.__eventStore[t];i||=[],i.push({type:e,handler:n,callbackHandler:n.bind(r)}),this.__eventStore[t]=i}on(e,t,n=this){this.__addOnEvent(`normal`,e,t,n)}once(e,t,n=this){this.__addOnEvent(`once`,e,t,n)}off(e,t){let n=this.__eventStore[e];n&&(n=n.filter(e=>e.handler!==t),this.__eventStore[e]=n)}emit(e,t,n){let r=this.__eventStore[e];r&&r.forEach(r=>{try{let{type:i,handler:a,callbackHandler:o}=r;typeof o==`function`&&o(t,n),i===`once`&&this.off(e,a)}catch(e){console.error(e)}})}destroy(){this.__eventStore={}}};exports.EventEmitter=e;
package/index.d.ts ADDED
@@ -0,0 +1,63 @@
1
+ /**
2
+ * 本模块提供事件触发器类。
3
+ * @packageDocumentation
4
+ */
5
+ /**
6
+ * 事件类型
7
+ */
8
+ export type EventType = number | string | symbol;
9
+ /**
10
+ * 事件参数关系类型
11
+ */
12
+ export type EventRelationsType = Record<EventType, unknown>;
13
+ /**
14
+ * 事件回调后的类 response 函数
15
+ */
16
+ export type EventHandlerCb = (res: unknown) => void;
17
+ /**
18
+ * 事件回调函数类型
19
+ */
20
+ export type EventHandler<Relations extends EventRelationsType, E extends EventType> = (params: Relations[E], cb?: EventHandlerCb) => unknown;
21
+ export declare class EventEmitter<Relations extends EventRelationsType = EventRelationsType, Events extends EventType = string> {
22
+ /**
23
+ * 事件回调存储器
24
+ * @ignore
25
+ */
26
+ private __eventStore;
27
+ /**
28
+ * 添加监听事件
29
+ * @ignore
30
+ */
31
+ private __addOnEvent;
32
+ /**
33
+ * 监听事件
34
+ * @param event 事件名
35
+ * @param handler 回调函数
36
+ * @param context this 上下文
37
+ */
38
+ on<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
39
+ /**
40
+ * 监听事件(一次性)
41
+ * @param event 事件名
42
+ * @param handler 回调函数
43
+ * @param context this 上下文
44
+ */
45
+ once<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
46
+ /**
47
+ * 移除事件监听
48
+ * @param event 事件名
49
+ * @param handler 回调函数
50
+ */
51
+ off<E extends Events>(event: E, handler: unknown): void;
52
+ /**
53
+ * 触发事件
54
+ * @param event 事件名
55
+ * @param params 回调参数
56
+ */
57
+ emit<E extends Events>(event: E, params: Relations[E], cb?: EventHandlerCb): void;
58
+ emit<E extends Events>(event: undefined extends Relations[E] ? E : never): void;
59
+ /**
60
+ * 销毁实例
61
+ */
62
+ destroy(): void;
63
+ }
package/index.js ADDED
@@ -0,0 +1,41 @@
1
+ //#region src/index.ts
2
+ var e = class {
3
+ constructor() {
4
+ this.__eventStore = {};
5
+ }
6
+ __addOnEvent(e, t, n, r) {
7
+ if (!t || !n) return;
8
+ let i = this.__eventStore[t];
9
+ i ||= [], i.push({
10
+ type: e,
11
+ handler: n,
12
+ callbackHandler: n.bind(r)
13
+ }), this.__eventStore[t] = i;
14
+ }
15
+ on(e, t, n = this) {
16
+ this.__addOnEvent("normal", e, t, n);
17
+ }
18
+ once(e, t, n = this) {
19
+ this.__addOnEvent("once", e, t, n);
20
+ }
21
+ off(e, t) {
22
+ let n = this.__eventStore[e];
23
+ n && (n = n.filter((e) => e.handler !== t), this.__eventStore[e] = n);
24
+ }
25
+ emit(e, t, n) {
26
+ let r = this.__eventStore[e];
27
+ r && r.forEach((r) => {
28
+ try {
29
+ let { type: i, handler: a, callbackHandler: o } = r;
30
+ typeof o == "function" && o(t, n), i === "once" && this.off(e, a);
31
+ } catch (e) {
32
+ console.error(e);
33
+ }
34
+ });
35
+ }
36
+ destroy() {
37
+ this.__eventStore = {};
38
+ }
39
+ };
40
+ //#endregion
41
+ export { e as EventEmitter };
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@polyv/utils-event",
3
+ "version": "3.0.0",
4
+ "description": "Event emitter utilities for Polyv frontend development.",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "module": "./index.js",
8
+ "types": "./index.d.ts",
9
+ "miniprogram": "./",
10
+ "exports": {
11
+ ".": {
12
+ "types": "./index.d.ts",
13
+ "import": "./index.js",
14
+ "require": "./index.cjs"
15
+ }
16
+ },
17
+ "sideEffects": false
18
+ }