events-ex 2.0.0 → 2.0.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/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
+ ## [2.0.1](https://github.com/snowyu/events-ex.js/compare/v2.0.0...v2.0.1) (2025-03-08)
6
+
7
+
8
+ ### Refactor
9
+
10
+ * hasListeners for performance ([3e49967](https://github.com/snowyu/events-ex.js/commit/3e49967296cb1d75291091f82232212d5f63af78))
11
+
5
12
  ## [2.0.0](https://github.com/snowyu/events-ex.js/compare/v2.0.0-alpha.15...v2.0.0) (2024-08-29)
6
13
 
7
14
  ## [2.0.0-alpha.15](https://github.com/snowyu/events-ex.js/compare/v2.0.0-alpha.14...v2.0.0-alpha.15) (2024-04-05)
package/README.cn.md CHANGED
@@ -1,4 +1,4 @@
1
- ### events-ex [![Build Status](https://img.shields.io/travis/snowyu/events-ex.js/master.png)](http://travis-ci.org/snowyu/events-ex.js) [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex)
1
+ ### events-ex [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex)
2
2
 
3
3
 
4
4
  浏览器友好的增强的**事件**[能力][Ability]和类。 它主要是从 [event-emitter][event-emitter] 修改而来的。 本库可以为你的任何类添加(注入)**事件**[能力][Ability]。
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ### events-ex [![Build Status](https://img.shields.io/travis/snowyu/events-ex.js/master.png)](http://travis-ci.org/snowyu/events-ex.js) [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex)
1
+ ### events-ex [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex)
2
2
 
3
3
 
4
4
  Browser-friendly enhanced event emitter [ability][Ability] and class. It's modified from [event-emitter][event-emitter] mainly. It can add/inject the event-able [ability][Ability] to your any class.
package/docs/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  events-ex / [Exports](modules.md)
2
2
 
3
- ### events-ex [![Build Status](https://img.shields.io/travis/snowyu/events-ex.js/master.png)](http://travis-ci.org/snowyu/events-ex.js) [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex)
3
+ ### events-ex [![npm](https://img.shields.io/npm/v/events-ex.svg)](https://npmjs.org/package/events-ex) [![downloads](https://img.shields.io/npm/dm/events-ex.svg)](https://npmjs.org/package/events-ex) [![license](https://img.shields.io/npm/l/events-ex.svg)](https://npmjs.org/package/events-ex)
4
4
 
5
5
  Browser-friendly enhanced event emitter [ability][Ability] and class. It's modified from [event-emitter][event-emitter] mainly. It can add/inject the event-able [ability][Ability] to your any class.
6
6
 
@@ -49,7 +49,7 @@ Event Object that contains information about the event, such as the target eleme
49
49
 
50
50
  #### Defined in
51
51
 
52
- [src/event.js:8](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event.js#L8)
52
+ [src/event.js:8](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event.js#L8)
53
53
 
54
54
  ## Properties
55
55
 
@@ -61,7 +61,7 @@ Keep your event result here if any.
61
61
 
62
62
  #### Defined in
63
63
 
64
- [src/event.js:38](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event.js#L38)
64
+ [src/event.js:38](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event.js#L38)
65
65
 
66
66
  ___
67
67
 
@@ -73,7 +73,7 @@ Whether stop the bubbling event
73
73
 
74
74
  #### Defined in
75
75
 
76
- [src/event.js:32](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event.js#L32)
76
+ [src/event.js:32](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event.js#L32)
77
77
 
78
78
  ___
79
79
 
@@ -85,7 +85,7 @@ Who trigger the event
85
85
 
86
86
  #### Defined in
87
87
 
88
- [src/event.js:26](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event.js#L26)
88
+ [src/event.js:26](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event.js#L26)
89
89
 
90
90
  ___
91
91
 
@@ -97,7 +97,7 @@ The type of the event.
97
97
 
98
98
  #### Defined in
99
99
 
100
- [src/event.js:44](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event.js#L44)
100
+ [src/event.js:44](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event.js#L44)
101
101
 
102
102
  ## Methods
103
103
 
@@ -115,7 +115,7 @@ The result of the event.
115
115
 
116
116
  #### Defined in
117
117
 
118
- [src/event.js:51](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event.js#L51)
118
+ [src/event.js:51](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event.js#L51)
119
119
 
120
120
  ___
121
121
 
@@ -138,4 +138,4 @@ Initializes the event with the target object.
138
138
 
139
139
  #### Defined in
140
140
 
141
- [src/event.js:20](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event.js#L20)
141
+ [src/event.js:20](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event.js#L20)
@@ -26,4 +26,4 @@ Class that represents an event emitter.
26
26
 
27
27
  #### Defined in
28
28
 
29
- [src/event-emitter.js:8](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.js#L8)
29
+ [src/event-emitter.js:8](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.js#L8)
@@ -47,7 +47,7 @@ Class that represents an event emitter.
47
47
 
48
48
  #### Defined in
49
49
 
50
- [src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L7)
50
+ [src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L7)
51
51
 
52
52
  ## Methods
53
53
 
@@ -72,7 +72,7 @@ The result of the event.
72
72
 
73
73
  #### Defined in
74
74
 
75
- [src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L38)
75
+ [src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L38)
76
76
 
77
77
  ___
78
78
 
@@ -97,7 +97,7 @@ A promise that resolves with the result of the event.
97
97
 
98
98
  #### Defined in
99
99
 
100
- [src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L44)
100
+ [src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L44)
101
101
 
102
102
  ___
103
103
 
@@ -121,7 +121,7 @@ Returns the count of listeners that are registered to listen for the specified e
121
121
 
122
122
  #### Defined in
123
123
 
124
- [src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L66)
124
+ [src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L66)
125
125
 
126
126
  ___
127
127
 
@@ -145,7 +145,7 @@ Returns an array of functions that are registered to listen for the specified ev
145
145
 
146
146
  #### Defined in
147
147
 
148
- [src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L59)
148
+ [src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L59)
149
149
 
150
150
  ___
151
151
 
@@ -174,7 +174,7 @@ If the listener is not a function.
174
174
 
175
175
  #### Defined in
176
176
 
177
- [src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L32)
177
+ [src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L32)
178
178
 
179
179
  ___
180
180
 
@@ -203,7 +203,7 @@ If the listener is not a function.
203
203
 
204
204
  #### Defined in
205
205
 
206
- [src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L16)
206
+ [src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L16)
207
207
 
208
208
  ___
209
209
 
@@ -232,7 +232,7 @@ If the listener is not a function.
232
232
 
233
233
  #### Defined in
234
234
 
235
- [src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L24)
235
+ [src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L24)
236
236
 
237
237
  ___
238
238
 
@@ -256,7 +256,7 @@ Removes all listeners for a specific event or all events from an event emitter.
256
256
 
257
257
  #### Defined in
258
258
 
259
- [src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L51)
259
+ [src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L51)
260
260
 
261
261
  ___
262
262
 
@@ -276,7 +276,7 @@ ___
276
276
 
277
277
  #### Defined in
278
278
 
279
- [src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L52)
279
+ [src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L52)
280
280
 
281
281
  ___
282
282
 
@@ -303,4 +303,4 @@ use emitter.listenerCount instead
303
303
 
304
304
  #### Defined in
305
305
 
306
- [src/event-emitter.d.ts:74](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/event-emitter.d.ts#L74)
306
+ [src/event-emitter.d.ts:74](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L74)
@@ -41,4 +41,4 @@ Removes all listeners for a specific event or all events from an event emitter.
41
41
 
42
42
  #### Defined in
43
43
 
44
- [src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/all-off.js#L12)
44
+ [src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/all-off.js#L12)
@@ -17,7 +17,7 @@
17
17
 
18
18
  #### Defined in
19
19
 
20
- [src/consts.js:13](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/consts.js#L13)
20
+ [src/consts.js:13](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/consts.js#L13)
21
21
 
22
22
  ___
23
23
 
@@ -36,4 +36,4 @@ ___
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/consts.js:6](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/consts.js#L6)
39
+ [src/consts.js:6](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/consts.js#L6)
@@ -48,4 +48,4 @@ Renames and re-exports [getEventableMethods](default_methods.md#geteventablemeth
48
48
 
49
49
  #### Defined in
50
50
 
51
- [src/default-methods.js:14](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/default-methods.js#L14)
51
+ [src/default-methods.js:14](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/default-methods.js#L14)
@@ -22,19 +22,71 @@ Renames and re-exports [eventable](eventable.md#eventable)
22
22
 
23
23
  ### eventable
24
24
 
25
- ▸ **eventable**(`targetClass?`, `options?`): `Function`
25
+ ▸ **eventable**\<`T`\>(`targetClass?`, `options?`): `EnhancedClass`\<`T`, (`aClass?`: `Function`) => `Function`\>
26
26
 
27
- #### Parameters
27
+ Adds event-emitting capabilities to a class by injecting necessary methods and properties.
28
+
29
+ This function uses `createAbilityInjector` from `custom-ability` to inject event-related methods
30
+ into the target class. The injected methods include standard EventEmitter functionality such as
31
+ `on`, `off`, `emit`, `emitAsync`, `once`, `listeners`, `@listenerCount` and more. It also ensures compatibility with Node.js `EventEmitter`
32
+ by including methods like `listenerCount`, `setMaxListeners`, `addListener`, `removeListener`, and `removeAllListeners`.
33
+
34
+ #### Type parameters
28
35
 
29
36
  | Name | Type |
30
37
  | :------ | :------ |
31
- | `targetClass?` | `Function` |
32
- | `options?` | `AbilityOptions` |
38
+ | `T` | extends `ClassEx` |
39
+
40
+ #### Parameters
41
+
42
+ | Name | Type | Description |
43
+ | :------ | :------ | :------ |
44
+ | `targetClass?` | `T` | - |
45
+ | `options?` | `AbilityOptions` | Optional configuration for the injection process: * `include` (string[]\|string): Specifies which methods should be added. Static methods should use the prefix '@'. * `exclude` (string[]\|string): Specifies which methods should not be added. Static methods should use the prefix '@'. * `methods` (Object): Custom methods to override or extend the default behavior. Use `this.super()` to call the original method and `this.self` to access the original context. * `classMethods` (Object): Custom static methods to be added to the class. |
33
46
 
34
47
  #### Returns
35
48
 
36
- `Function`
49
+ `EnhancedClass`\<`T`, (`aClass?`: `Function`) => `Function`\>
50
+
51
+ The same `aClass` class with event capabilities injected. The return value is the modified `aClass` itself.
52
+
53
+ **`Function`**
54
+
55
+ eventable
56
+
57
+ **`Example`**
58
+
59
+ ```ts
60
+ import { eventable } from 'events-ex';
61
+
62
+ class MyClass {}
63
+
64
+ // Inject only specific methods: 'on', 'off', 'emit', 'emitAsync', and the static 'listenerCount'
65
+ eventable(MyClass, { include: ['on', 'off', 'emit', 'emitAsync', '@listenerCount'] });
66
+ ```
67
+
68
+ **`Example`**
69
+
70
+ ```ts
71
+ import { eventable } from 'events-ex';
72
+
73
+ class OtherClass {
74
+ exec() {
75
+ console.log("Original exec");
76
+ }
77
+ }
78
+
79
+ // Inject event capabilities and override the `exec` method
80
+ eventable(OtherClass, {
81
+ methods: {
82
+ exec() {
83
+ console.log("New exec");
84
+ this.super(); // Calls the original `exec` method
85
+ }
86
+ }
87
+ });
88
+ ```
37
89
 
38
90
  #### Defined in
39
91
 
40
- node_modules/custom-ability/lib/custom-ability.d.ts:28
92
+ node_modules/custom-ability/lib/custom-ability.d.ts:34
@@ -22,19 +22,28 @@ Renames and re-exports [hasListeners](has_listeners.md#haslisteners)
22
22
 
23
23
  ### hasListeners
24
24
 
25
- ▸ **hasListeners**(`obj`, `...args`): `boolean`
25
+ ▸ **hasListeners**(`obj`, `type?`): `boolean`
26
+
27
+ Checks if an object has event listeners.
26
28
 
27
29
  #### Parameters
28
30
 
29
- | Name | Type |
30
- | :------ | :------ |
31
- | `obj` | `any` |
32
- | `...args` | `any` |
31
+ | Name | Type | Description |
32
+ | :------ | :------ | :------ |
33
+ | `obj` | `any` | The object to check. Must not be null or undefined. |
34
+ | `type?` | `string` | Optional parameter specifying the event type. If provided, checks for the existence of listeners for this specific type. |
33
35
 
34
36
  #### Returns
35
37
 
36
38
  `boolean`
37
39
 
40
+ - If `type` is provided, returns whether listeners for the specified event type exist.
41
+ - If `type` is not provided, returns whether the object has any event listeners.
42
+
43
+ **`Throws`**
44
+
45
+ Throws a TypeError if `obj` is null or undefined.
46
+
38
47
  #### Defined in
39
48
 
40
- [src/has-listeners.js:10](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/has-listeners.js#L10)
49
+ [src/has-listeners.js:20](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/has-listeners.js#L20)
@@ -46,4 +46,4 @@ Creates a pipeline between two event emitters, so that any events emitted by the
46
46
 
47
47
  #### Defined in
48
48
 
49
- [src/pipe.js:21](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/pipe.js#L21)
49
+ [src/pipe.js:21](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/pipe.js#L21)
@@ -46,4 +46,4 @@ Creates a pipeline between two event emitters, so that any events emitted by the
46
46
 
47
47
  #### Defined in
48
48
 
49
- [src/pipe-async.js:21](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/pipe-async.js#L21)
49
+ [src/pipe-async.js:21](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/pipe-async.js#L21)
@@ -43,4 +43,4 @@ Unifies the event listeners of two event emitter objects so that they share the
43
43
 
44
44
  #### Defined in
45
45
 
46
- [src/unify.js:17](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/unify.js#L17)
46
+ [src/unify.js:17](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/unify.js#L17)
@@ -36,4 +36,4 @@ Renames and re-exports [remove](util_array_remove.md#remove)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/array-remove.js:5](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/util/array-remove.js#L5)
39
+ [src/util/array-remove.js:5](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/util/array-remove.js#L5)
@@ -38,4 +38,4 @@ Renames and re-exports [forEach](util_object_for_each.md#foreach)
38
38
 
39
39
  #### Defined in
40
40
 
41
- [src/util/object-for-each.js:6](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/util/object-for-each.js#L6)
41
+ [src/util/object-for-each.js:6](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/util/object-for-each.js#L6)
@@ -37,4 +37,4 @@ Renames and re-exports [pad](util_string_pad.md#pad)
37
37
 
38
38
  #### Defined in
39
39
 
40
- [src/util/string-pad.js:8](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/util/string-pad.js#L8)
40
+ [src/util/string-pad.js:8](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/util/string-pad.js#L8)
@@ -36,4 +36,4 @@ Renames and re-exports [toInt](util_to_int.md#toint)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/to-int.js:2](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/util/to-int.js#L2)
39
+ [src/util/to-int.js:2](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/util/to-int.js#L2)
@@ -36,4 +36,4 @@ Renames and re-exports [validCallable](util_valid_callable.md#validcallable)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/valid-callable.js:1](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/util/valid-callable.js#L1)
39
+ [src/util/valid-callable.js:1](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/util/valid-callable.js#L1)
@@ -36,4 +36,4 @@ Renames and re-exports [validObject](util_valid_object.md#validobject)
36
36
 
37
37
  #### Defined in
38
38
 
39
- [src/util/valid-object.js:3](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/util/valid-object.js#L3)
39
+ [src/util/valid-object.js:3](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/util/valid-object.js#L3)
@@ -30,7 +30,7 @@ Renames and re-exports [wrapEventEmitter](wrap_event_emitter.md#wrapeventemitter
30
30
 
31
31
  #### Defined in
32
32
 
33
- [src/wrap-event-emitter.js:7](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/wrap-event-emitter.js#L7)
33
+ [src/wrap-event-emitter.js:7](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/wrap-event-emitter.js#L7)
34
34
 
35
35
  ## Functions
36
36
 
@@ -54,4 +54,4 @@ o or new Event instance
54
54
 
55
55
  #### Defined in
56
56
 
57
- [src/wrap-event-emitter.js:34](https://github.com/snowyu/events-ex.js/blob/5b9fac0/src/wrap-event-emitter.js#L34)
57
+ [src/wrap-event-emitter.js:34](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/wrap-event-emitter.js#L34)
@@ -1,2 +1,57 @@
1
- export const eventable: import("custom-ability").AbilityFn;
1
+ /**
2
+ * Adds event-emitting capabilities to a class by injecting necessary methods and properties.
3
+ *
4
+ * This function uses `createAbilityInjector` from `custom-ability` to inject event-related methods
5
+ * into the target class. The injected methods include standard EventEmitter functionality such as
6
+ * `on`, `off`, `emit`, `emitAsync`, `once`, `listeners`, `@listenerCount` and more. It also ensures compatibility with Node.js `EventEmitter`
7
+ * by including methods like `listenerCount`, `setMaxListeners`, `addListener`, `removeListener`, and `removeAllListeners`.
8
+ *
9
+ * @function eventable
10
+ * @param {Class} [aClass] - The target class to which event capabilities will be injected. if no class is provided, a new class with eventable will be created.
11
+ * @param {Object} [options] - Optional configuration for the injection process:
12
+ * * `include` (string[]|string): Specifies which methods should be added.
13
+ * Static methods should use the prefix '@'.
14
+ * * `exclude` (string[]|string): Specifies which methods should not be added.
15
+ * Static methods should use the prefix '@'.
16
+ * * `methods` (Object): Custom methods to override or extend the default behavior.
17
+ * Use `this.super()` to call the original method and `this.self` to access the original context.
18
+ * * `classMethods` (Object): Custom static methods to be added to the class.
19
+ * @returns {Class} The same `aClass` class with event capabilities injected. The return value is the modified `aClass` itself.
20
+ *
21
+ * @example
22
+ * import { eventable } from 'events-ex';
23
+ *
24
+ * class MyClass {}
25
+ *
26
+ * // Inject only specific methods: 'on', 'off', 'emit', 'emitAsync', and the static 'listenerCount'
27
+ * eventable(MyClass, { include: ['on', 'off', 'emit', 'emitAsync', '@listenerCount'] });
28
+ *
29
+ * @example
30
+ * import { eventable } from 'events-ex';
31
+ *
32
+ * class OtherClass {
33
+ * exec() {
34
+ * console.log("Original exec");
35
+ * }
36
+ * }
37
+ *
38
+ * // Inject event capabilities and override the `exec` method
39
+ * eventable(OtherClass, {
40
+ * methods: {
41
+ * exec() {
42
+ * console.log("New exec");
43
+ * this.super(); // Calls the original `exec` method
44
+ * }
45
+ * }
46
+ * });
47
+ */
48
+ export const eventable: <T extends Function | (new (...args: any[]) => any)>(targetClass?: T, options?: import("custom-ability").AbilityOptions) => T & typeof getEventableClass & (T extends new (...args: any[]) => any ? InstanceType<T> : T);
2
49
  export default eventable;
50
+ /**
51
+ * Creates an Eventable class with event-emitting capabilities.
52
+ *
53
+ * @param {Function} [aClass] - The target class to which event capabilities will be injected.
54
+ * If not provided, a new base class (`Eventable`) is created and used.
55
+ * @returns {Function} A class with event-emitting capabilities.
56
+ */
57
+ declare function getEventableClass(aClass?: Function): Function;
package/lib/eventable.js CHANGED
@@ -8,7 +8,18 @@ var _customAbility = require("custom-ability");
8
8
  var _defineProperty = _interopRequireDefault(require("util-ex/lib/defineProperty"));
9
9
  var _defaultMethods = _interopRequireDefault(require("./default-methods"));
10
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
+ /**
12
+ * Creates an Eventable class with event-emitting capabilities.
13
+ *
14
+ * @param {Function} [aClass] - The target class to which event capabilities will be injected.
15
+ * If not provided, a new base class (`Eventable`) is created and used.
16
+ * @returns {Function} A class with event-emitting capabilities.
17
+ */
11
18
  function getEventableClass(aClass) {
19
+ /**
20
+ * Base Eventable class that provides event-related functionality.
21
+ * @class
22
+ */
12
23
  function Eventable() {}
13
24
  if (aClass == null) {
14
25
  aClass = Eventable;
@@ -31,5 +42,53 @@ function getEventableClass(aClass) {
31
42
  return Eventable;
32
43
  }
33
44
  ;
45
+
46
+ /**
47
+ * Adds event-emitting capabilities to a class by injecting necessary methods and properties.
48
+ *
49
+ * This function uses `createAbilityInjector` from `custom-ability` to inject event-related methods
50
+ * into the target class. The injected methods include standard EventEmitter functionality such as
51
+ * `on`, `off`, `emit`, `emitAsync`, `once`, `listeners`, `@listenerCount` and more. It also ensures compatibility with Node.js `EventEmitter`
52
+ * by including methods like `listenerCount`, `setMaxListeners`, `addListener`, `removeListener`, and `removeAllListeners`.
53
+ *
54
+ * @function eventable
55
+ * @param {Class} [aClass] - The target class to which event capabilities will be injected. if no class is provided, a new class with eventable will be created.
56
+ * @param {Object} [options] - Optional configuration for the injection process:
57
+ * * `include` (string[]|string): Specifies which methods should be added.
58
+ * Static methods should use the prefix '@'.
59
+ * * `exclude` (string[]|string): Specifies which methods should not be added.
60
+ * Static methods should use the prefix '@'.
61
+ * * `methods` (Object): Custom methods to override or extend the default behavior.
62
+ * Use `this.super()` to call the original method and `this.self` to access the original context.
63
+ * * `classMethods` (Object): Custom static methods to be added to the class.
64
+ * @returns {Class} The same `aClass` class with event capabilities injected. The return value is the modified `aClass` itself.
65
+ *
66
+ * @example
67
+ * import { eventable } from 'events-ex';
68
+ *
69
+ * class MyClass {}
70
+ *
71
+ * // Inject only specific methods: 'on', 'off', 'emit', 'emitAsync', and the static 'listenerCount'
72
+ * eventable(MyClass, { include: ['on', 'off', 'emit', 'emitAsync', '@listenerCount'] });
73
+ *
74
+ * @example
75
+ * import { eventable } from 'events-ex';
76
+ *
77
+ * class OtherClass {
78
+ * exec() {
79
+ * console.log("Original exec");
80
+ * }
81
+ * }
82
+ *
83
+ * // Inject event capabilities and override the `exec` method
84
+ * eventable(OtherClass, {
85
+ * methods: {
86
+ * exec() {
87
+ * console.log("New exec");
88
+ * this.super(); // Calls the original `exec` method
89
+ * }
90
+ * }
91
+ * });
92
+ */
34
93
  const eventable = exports.eventable = (0, _customAbility.createAbilityInjector)(getEventableClass, true);
35
94
  var _default = exports.default = eventable;
@@ -1,2 +1,12 @@
1
- export function hasListeners(obj: any, ...args: any[]): boolean;
1
+ /**
2
+ * Checks if an object has event listeners.
3
+ *
4
+ * @param {Object} obj - The object to check. Must not be null or undefined.
5
+ * @param {string} [type] - Optional parameter specifying the event type. If provided, checks for the existence of listeners for this specific type.
6
+ * @returns {boolean}
7
+ * - If `type` is provided, returns whether listeners for the specified event type exist.
8
+ * - If `type` is not provided, returns whether the object has any event listeners.
9
+ * @throws {TypeError} Throws a TypeError if `obj` is null or undefined.
10
+ */
11
+ export function hasListeners(obj: any, type?: string): boolean;
2
12
  export default hasListeners;
@@ -10,15 +10,23 @@ const hasOwnProperty = Object.prototype.hasOwnProperty;
10
10
  function isValue(v) {
11
11
  return v !== null && v !== undefined;
12
12
  }
13
- function hasListeners(obj /* , type */) {
13
+
14
+ /**
15
+ * Checks if an object has event listeners.
16
+ *
17
+ * @param {Object} obj - The object to check. Must not be null or undefined.
18
+ * @param {string} [type] - Optional parameter specifying the event type. If provided, checks for the existence of listeners for this specific type.
19
+ * @returns {boolean}
20
+ * - If `type` is provided, returns whether listeners for the specified event type exist.
21
+ * - If `type` is not provided, returns whether the object has any event listeners.
22
+ * @throws {TypeError} Throws a TypeError if `obj` is null or undefined.
23
+ */
24
+ function hasListeners(obj, type) {
14
25
  if (!isValue(obj)) {
15
26
  throw new TypeError("Cannot use null or undefined");
16
27
  }
17
- const type = arguments[1];
18
- if (arguments.length > 1) {
19
- return hasOwnProperty.call(obj, '_events') && Boolean(obj._events[type]);
20
- }
21
- return obj.hasOwnProperty('_events') && !(0, _utilEx.isEmptyObject)(obj._events);
28
+ const result = type != null ? hasOwnProperty.call(obj, '_events') && obj._events[type] : obj.hasOwnProperty('_events') && !(0, _utilEx.isEmptyObject)(obj._events);
29
+ return result;
22
30
  }
23
31
  ;
24
32
  var _default = exports.default = hasListeners;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "events-ex",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Browser-friendly enhanced events most compatible with standard node.js, it's powerful eventable ability.",
5
5
  "contributors": [
6
6
  {
@@ -37,7 +37,7 @@
37
37
  "main": "./lib/index.js",
38
38
  "module": "./src/index.js",
39
39
  "dependencies": {
40
- "custom-ability": "^2.0.0",
40
+ "custom-ability": "^2.0.1",
41
41
  "util-ex": "^2.0.0"
42
42
  },
43
43
  "devDependencies": {
@@ -51,7 +51,7 @@
51
51
  "eslint": "^8.56.0",
52
52
  "eslint-config-prettier": "^9.1.0",
53
53
  "eslint-plugin-tsdoc": "^0.2.17",
54
- "mocha": "^10.2.0",
54
+ "mocha": "^10.6.0",
55
55
  "prettier": "^3.1.1",
56
56
  "typedoc": "^0.25.4",
57
57
  "typedoc-plugin-markdown": "^3.17.1",
package/src/eventable.js CHANGED
@@ -2,7 +2,18 @@ import {createAbilityInjector} from 'custom-ability'
2
2
  import defineProperty from 'util-ex/lib/defineProperty'
3
3
  import defaultMethods from './default-methods'
4
4
 
5
+ /**
6
+ * Creates an Eventable class with event-emitting capabilities.
7
+ *
8
+ * @param {Function} [aClass] - The target class to which event capabilities will be injected.
9
+ * If not provided, a new base class (`Eventable`) is created and used.
10
+ * @returns {Function} A class with event-emitting capabilities.
11
+ */
5
12
  function getEventableClass(aClass) {
13
+ /**
14
+ * Base Eventable class that provides event-related functionality.
15
+ * @class
16
+ */
6
17
  function Eventable() {}
7
18
 
8
19
  if (aClass == null) {aClass = Eventable}
@@ -25,5 +36,52 @@ function getEventableClass(aClass) {
25
36
  return Eventable;
26
37
  };
27
38
 
39
+ /**
40
+ * Adds event-emitting capabilities to a class by injecting necessary methods and properties.
41
+ *
42
+ * This function uses `createAbilityInjector` from `custom-ability` to inject event-related methods
43
+ * into the target class. The injected methods include standard EventEmitter functionality such as
44
+ * `on`, `off`, `emit`, `emitAsync`, `once`, `listeners`, `@listenerCount` and more. It also ensures compatibility with Node.js `EventEmitter`
45
+ * by including methods like `listenerCount`, `setMaxListeners`, `addListener`, `removeListener`, and `removeAllListeners`.
46
+ *
47
+ * @function eventable
48
+ * @param {Class} [aClass] - The target class to which event capabilities will be injected. if no class is provided, a new class with eventable will be created.
49
+ * @param {Object} [options] - Optional configuration for the injection process:
50
+ * * `include` (string[]|string): Specifies which methods should be added.
51
+ * Static methods should use the prefix '@'.
52
+ * * `exclude` (string[]|string): Specifies which methods should not be added.
53
+ * Static methods should use the prefix '@'.
54
+ * * `methods` (Object): Custom methods to override or extend the default behavior.
55
+ * Use `this.super()` to call the original method and `this.self` to access the original context.
56
+ * * `classMethods` (Object): Custom static methods to be added to the class.
57
+ * @returns {Class} The same `aClass` class with event capabilities injected. The return value is the modified `aClass` itself.
58
+ *
59
+ * @example
60
+ * import { eventable } from 'events-ex';
61
+ *
62
+ * class MyClass {}
63
+ *
64
+ * // Inject only specific methods: 'on', 'off', 'emit', 'emitAsync', and the static 'listenerCount'
65
+ * eventable(MyClass, { include: ['on', 'off', 'emit', 'emitAsync', '@listenerCount'] });
66
+ *
67
+ * @example
68
+ * import { eventable } from 'events-ex';
69
+ *
70
+ * class OtherClass {
71
+ * exec() {
72
+ * console.log("Original exec");
73
+ * }
74
+ * }
75
+ *
76
+ * // Inject event capabilities and override the `exec` method
77
+ * eventable(OtherClass, {
78
+ * methods: {
79
+ * exec() {
80
+ * console.log("New exec");
81
+ * this.super(); // Calls the original `exec` method
82
+ * }
83
+ * }
84
+ * });
85
+ */
28
86
  export const eventable = createAbilityInjector(getEventableClass, true);
29
87
  export default eventable;
@@ -7,14 +7,21 @@ function isValue(v) {
7
7
  return v!== null && v!== undefined
8
8
  }
9
9
 
10
- export function hasListeners(obj/* , type */) {
10
+ /**
11
+ * Checks if an object has event listeners.
12
+ *
13
+ * @param {Object} obj - The object to check. Must not be null or undefined.
14
+ * @param {string} [type] - Optional parameter specifying the event type. If provided, checks for the existence of listeners for this specific type.
15
+ * @returns {boolean}
16
+ * - If `type` is provided, returns whether listeners for the specified event type exist.
17
+ * - If `type` is not provided, returns whether the object has any event listeners.
18
+ * @throws {TypeError} Throws a TypeError if `obj` is null or undefined.
19
+ */
20
+ export function hasListeners(obj, type) {
11
21
  if (!isValue(obj)) {throw new TypeError("Cannot use null or undefined")}
12
22
 
13
- const type = arguments[1];
14
- if (arguments.length > 1) {
15
- return hasOwnProperty.call(obj, '_events') && Boolean(obj._events[type]);
16
- }
17
- return obj.hasOwnProperty('_events') && !isEmptyObject(obj._events);
23
+ const result = type != null ? hasOwnProperty.call(obj, '_events') && obj._events[type] : obj.hasOwnProperty('_events') && !isEmptyObject(obj._events);
24
+ return result;
18
25
  };
19
26
 
20
27
  export default hasListeners