@polyv/utils 2.1.0-beta.1 → 2.2.0-beta.1
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/dist/cjs/event.d.ts +113 -0
- package/dist/cjs/event.js +1 -0
- package/dist/es/event.d.ts +113 -0
- package/dist/es/event.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本模块提供事件触发器类。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```js
|
|
7
|
+
* import { EventEmitter } from '@polyv/utils/es/event';
|
|
8
|
+
*
|
|
9
|
+
* const emitter = new EventEmitter();
|
|
10
|
+
* const context = { name: 'my name' };
|
|
11
|
+
*
|
|
12
|
+
* function eventHandler(e) {
|
|
13
|
+
* console.log(e);
|
|
14
|
+
* console.log(this === context); // true
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // listen event
|
|
18
|
+
* emitter.on('foo', eventHandler, context);
|
|
19
|
+
*
|
|
20
|
+
* // listen once event
|
|
21
|
+
* emitter.once('foo', eventHandler, context);
|
|
22
|
+
*
|
|
23
|
+
* // fire event
|
|
24
|
+
* emitter.emit('foo', { a: 1 });
|
|
25
|
+
*
|
|
26
|
+
* // unlisten event
|
|
27
|
+
* emitter.off('foo', eventHandler);
|
|
28
|
+
*
|
|
29
|
+
* // ---------- use typescript define event parameter types ---------- //
|
|
30
|
+
* type EventParams = {
|
|
31
|
+
* foo: string;
|
|
32
|
+
* boo: number;
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* const emitter = new EventEmitter<EventParams>();
|
|
36
|
+
*
|
|
37
|
+
* emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
|
|
38
|
+
*
|
|
39
|
+
* emitter.emit('foo', 18); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
|
|
40
|
+
*
|
|
41
|
+
* // ---------- use typescript define event enum ---------- //
|
|
42
|
+
* enum TestEvent {
|
|
43
|
+
* Foo = 'foo',
|
|
44
|
+
* Bar = 'bar',
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* type TestEventParams = {
|
|
48
|
+
* [TestEvent.Foo]: string;
|
|
49
|
+
* [TestEvent.Bar]: number;
|
|
50
|
+
* };
|
|
51
|
+
*
|
|
52
|
+
* const emitter = new EventEmitter<TestEventParams, TestEvent>();
|
|
53
|
+
*
|
|
54
|
+
* emitter.emit(TestEvent.Foo, 'abc'); // ok
|
|
55
|
+
*
|
|
56
|
+
* emitter.emit('foo', 'abc'); // Error: Argument of type "'foo'" is not assignable to parameter of type 'TestEvent'. (2345)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* 事件类型
|
|
61
|
+
*/
|
|
62
|
+
export declare type EventType = string | symbol;
|
|
63
|
+
/**
|
|
64
|
+
* 事件参数关系类型
|
|
65
|
+
*/
|
|
66
|
+
export declare type EventRelationsType = Record<EventType, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* 事件回调函数类型
|
|
69
|
+
*/
|
|
70
|
+
export declare type EventHandler<Relations extends EventRelationsType, E extends EventType> = (params: Relations[E]) => unknown;
|
|
71
|
+
export declare class EventEmitter<Relations extends EventRelationsType = EventRelationsType, Events extends EventType = string> {
|
|
72
|
+
/**
|
|
73
|
+
* 事件回调存储器
|
|
74
|
+
* @ignore
|
|
75
|
+
*/
|
|
76
|
+
private __eventStore;
|
|
77
|
+
/**
|
|
78
|
+
* 添加监听事件
|
|
79
|
+
* @ignore
|
|
80
|
+
*/
|
|
81
|
+
private __addOnEvent;
|
|
82
|
+
/**
|
|
83
|
+
* 监听事件
|
|
84
|
+
* @param event 事件名
|
|
85
|
+
* @param handler 回调函数
|
|
86
|
+
* @param context this 上下文
|
|
87
|
+
*/
|
|
88
|
+
on<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
89
|
+
/**
|
|
90
|
+
* 监听事件(一次性)
|
|
91
|
+
* @param event 事件名
|
|
92
|
+
* @param handler 回调函数
|
|
93
|
+
* @param context this 上下文
|
|
94
|
+
*/
|
|
95
|
+
once<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
96
|
+
/**
|
|
97
|
+
* 移除事件监听
|
|
98
|
+
* @param event 事件名
|
|
99
|
+
* @param handler 回调函数
|
|
100
|
+
*/
|
|
101
|
+
off<E extends Events>(event: E, handler: unknown): void;
|
|
102
|
+
/**
|
|
103
|
+
* 触发事件
|
|
104
|
+
* @param event 事件名
|
|
105
|
+
* @param params 回调参数
|
|
106
|
+
*/
|
|
107
|
+
emit<E extends Events>(event: E, params: Relations[E]): void;
|
|
108
|
+
emit<E extends Events>(event: undefined extends Relations[E] ? E : never): void;
|
|
109
|
+
/**
|
|
110
|
+
* 销毁实例
|
|
111
|
+
*/
|
|
112
|
+
destroy(): void;
|
|
113
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.EventEmitter=void 0;var EventEmitter=function(){function t(){this.__eventStore={}}return t.prototype.__addOnEvent=function(t,e,n,o){if(e&&n){var r=this.__eventStore[e];r||(r=[]),r.push({type:t,handler:n,callbackHandler:n.bind(o)}),this.__eventStore[e]=r}},t.prototype.on=function(t,e,n){void 0===n&&(n=this),this.__addOnEvent("normal",t,e,n)},t.prototype.once=function(t,e,n){void 0===n&&(n=this),this.__addOnEvent("once",t,e,n)},t.prototype.off=function(t,e){var n=this.__eventStore[t];n&&(n=n.filter((function(t){return t.handler!==e})),this.__eventStore[t]=n)},t.prototype.emit=function(t,e){var n=this,o=this.__eventStore[t];o&&o.forEach((function(o){var r=o.type,i=o.handler,v=o.callbackHandler;"function"==typeof v&&v(e),"once"===r&&n.off(t,i)}))},t.prototype.destroy=function(){this.__eventStore={}},t}();exports.EventEmitter=EventEmitter;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本模块提供事件触发器类。
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```js
|
|
7
|
+
* import { EventEmitter } from '@polyv/utils/es/event';
|
|
8
|
+
*
|
|
9
|
+
* const emitter = new EventEmitter();
|
|
10
|
+
* const context = { name: 'my name' };
|
|
11
|
+
*
|
|
12
|
+
* function eventHandler(e) {
|
|
13
|
+
* console.log(e);
|
|
14
|
+
* console.log(this === context); // true
|
|
15
|
+
* }
|
|
16
|
+
*
|
|
17
|
+
* // listen event
|
|
18
|
+
* emitter.on('foo', eventHandler, context);
|
|
19
|
+
*
|
|
20
|
+
* // listen once event
|
|
21
|
+
* emitter.once('foo', eventHandler, context);
|
|
22
|
+
*
|
|
23
|
+
* // fire event
|
|
24
|
+
* emitter.emit('foo', { a: 1 });
|
|
25
|
+
*
|
|
26
|
+
* // unlisten event
|
|
27
|
+
* emitter.off('foo', eventHandler);
|
|
28
|
+
*
|
|
29
|
+
* // ---------- use typescript define event parameter types ---------- //
|
|
30
|
+
* type EventParams = {
|
|
31
|
+
* foo: string;
|
|
32
|
+
* boo: number;
|
|
33
|
+
* };
|
|
34
|
+
*
|
|
35
|
+
* const emitter = new EventEmitter<EventParams>();
|
|
36
|
+
*
|
|
37
|
+
* emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
|
|
38
|
+
*
|
|
39
|
+
* emitter.emit('foo', 18); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
|
|
40
|
+
*
|
|
41
|
+
* // ---------- use typescript define event enum ---------- //
|
|
42
|
+
* enum TestEvent {
|
|
43
|
+
* Foo = 'foo',
|
|
44
|
+
* Bar = 'bar',
|
|
45
|
+
* }
|
|
46
|
+
*
|
|
47
|
+
* type TestEventParams = {
|
|
48
|
+
* [TestEvent.Foo]: string;
|
|
49
|
+
* [TestEvent.Bar]: number;
|
|
50
|
+
* };
|
|
51
|
+
*
|
|
52
|
+
* const emitter = new EventEmitter<TestEventParams, TestEvent>();
|
|
53
|
+
*
|
|
54
|
+
* emitter.emit(TestEvent.Foo, 'abc'); // ok
|
|
55
|
+
*
|
|
56
|
+
* emitter.emit('foo', 'abc'); // Error: Argument of type "'foo'" is not assignable to parameter of type 'TestEvent'. (2345)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
/**
|
|
60
|
+
* 事件类型
|
|
61
|
+
*/
|
|
62
|
+
export declare type EventType = string | symbol;
|
|
63
|
+
/**
|
|
64
|
+
* 事件参数关系类型
|
|
65
|
+
*/
|
|
66
|
+
export declare type EventRelationsType = Record<EventType, unknown>;
|
|
67
|
+
/**
|
|
68
|
+
* 事件回调函数类型
|
|
69
|
+
*/
|
|
70
|
+
export declare type EventHandler<Relations extends EventRelationsType, E extends EventType> = (params: Relations[E]) => unknown;
|
|
71
|
+
export declare class EventEmitter<Relations extends EventRelationsType = EventRelationsType, Events extends EventType = string> {
|
|
72
|
+
/**
|
|
73
|
+
* 事件回调存储器
|
|
74
|
+
* @ignore
|
|
75
|
+
*/
|
|
76
|
+
private __eventStore;
|
|
77
|
+
/**
|
|
78
|
+
* 添加监听事件
|
|
79
|
+
* @ignore
|
|
80
|
+
*/
|
|
81
|
+
private __addOnEvent;
|
|
82
|
+
/**
|
|
83
|
+
* 监听事件
|
|
84
|
+
* @param event 事件名
|
|
85
|
+
* @param handler 回调函数
|
|
86
|
+
* @param context this 上下文
|
|
87
|
+
*/
|
|
88
|
+
on<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
89
|
+
/**
|
|
90
|
+
* 监听事件(一次性)
|
|
91
|
+
* @param event 事件名
|
|
92
|
+
* @param handler 回调函数
|
|
93
|
+
* @param context this 上下文
|
|
94
|
+
*/
|
|
95
|
+
once<E extends Events>(event: E, handler: EventHandler<Relations, E>, context?: unknown): void;
|
|
96
|
+
/**
|
|
97
|
+
* 移除事件监听
|
|
98
|
+
* @param event 事件名
|
|
99
|
+
* @param handler 回调函数
|
|
100
|
+
*/
|
|
101
|
+
off<E extends Events>(event: E, handler: unknown): void;
|
|
102
|
+
/**
|
|
103
|
+
* 触发事件
|
|
104
|
+
* @param event 事件名
|
|
105
|
+
* @param params 回调参数
|
|
106
|
+
*/
|
|
107
|
+
emit<E extends Events>(event: E, params: Relations[E]): void;
|
|
108
|
+
emit<E extends Events>(event: undefined extends Relations[E] ? E : never): void;
|
|
109
|
+
/**
|
|
110
|
+
* 销毁实例
|
|
111
|
+
*/
|
|
112
|
+
destroy(): void;
|
|
113
|
+
}
|
package/dist/es/event.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export class EventEmitter{constructor(){this.__eventStore={}}__addOnEvent(t,e,n,o){if(!e||!n)return;let r=this.__eventStore[e];r||(r=[]),r.push({type:t,handler:n,callbackHandler:n.bind(o)}),this.__eventStore[e]=r}on(t,e,n=this){this.__addOnEvent("normal",t,e,n)}once(t,e,n=this){this.__addOnEvent("once",t,e,n)}off(t,e){let n=this.__eventStore[t];n&&(n=n.filter((t=>t.handler!==e)),this.__eventStore[t]=n)}emit(t,e){const n=this.__eventStore[t];n&&n.forEach((n=>{const{type:o,handler:r,callbackHandler:_}=n;"function"==typeof _&&_(e),"once"===o&&this.off(t,r)}))}destroy(){this.__eventStore={}}}
|