events-ex 2.0.1 → 2.1.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,20 @@
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.1.1](https://github.com/snowyu/events-ex.js/compare/v2.1.0...v2.1.1) (2025-10-24)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **ts:** forget to add removeListener declaration ([9c254b2](https://github.com/snowyu/events-ex.js/commit/9c254b2e1f1ab5e437c0099f002776a49e976c5b))
11
+
12
+ ## [2.1.0](https://github.com/snowyu/events-ex.js/compare/v2.0.1...v2.1.0) (2025-06-09)
13
+
14
+
15
+ ### Features
16
+
17
+ * add optional index argument to on/off funcs ([0b91827](https://github.com/snowyu/events-ex.js/commit/0b91827e05cb252edf645820db74fd36496ed56d))
18
+
5
19
  ## [2.0.1](https://github.com/snowyu/events-ex.js/compare/v2.0.0...v2.0.1) (2025-03-08)
6
20
 
7
21
 
package/README.cn.md CHANGED
@@ -1,16 +1,7 @@
1
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
-
4
3
  浏览器友好的增强的**事件**[能力][Ability]和类。 它主要是从 [event-emitter][event-emitter] 修改而来的。 本库可以为你的任何类添加(注入)**事件**[能力][Ability]。
5
4
 
6
-
7
- TODO: 异步事件,添加`emitAsync`方法. 没这么简单,因为要支持bubbling,所以必须顺序执行事件.
8
- 将`bubbling`作为功能选项.如果没有启用,就可以乱发了.
9
-
10
- 当关闭`bubbling`的时候,那么是否还需要用`event`对象传递.
11
-
12
- 首先完成异步支持.已经完成.`emitAsync`方法已经加上.
13
-
14
5
  ### Features
15
6
 
16
7
  * 重写核心架构
@@ -23,7 +14,7 @@ TODO: 异步事件,添加`emitAsync`方法. 没这么简单,因为要支持bubbl
23
14
  ### 区别
24
15
 
25
16
  * 与 [Node 事件模块](https://nodejs.org/api/events.html) 的区别
26
- * **`改变`**: 事件支持冒泡机制与中断
17
+ * 🔁 **`改变`**: 事件支持冒泡机制与中断
27
18
  * 事件对象(`Event Object`)作为监听器的 "this" 对象。
28
19
  * `result` 属性: 可选, 如果设置,则将该结果返回到事件发射器(`Event Emitter`)。
29
20
  * `stopped` 属性: 可选, 如果设置为 `true`,则会阻止剩余的监听器被执行。
@@ -32,7 +23,14 @@ TODO: 异步事件,添加`emitAsync`方法. 没这么简单,因为要支持bubbl
32
23
  * **`改变`**: `emit` 方法返回监听器回调函数的结果而不是成功状态。
33
24
  * **`改变`**: 监听器回调函数的 `this` 对象是 `Event Object` 事件对象而不是事件发射器对象。
34
25
  * 事件发射器对象被放入 `Event` 对象的 `target` 属性中。
35
- * 添加了`emitAsync`方法,支持异步事件
26
+ * 添加了`emitAsync`方法,采用瀑布流式顺序执行异步事件,支持冒泡机制与中断。
27
+ * 非常适合用于需要等待多个异步任务完成的场景(如数据验证、插件系统等)。
28
+ * 事件监听器`on/once(event: string|RegExp, listener, index?:number)`
29
+ * 📌 支持第三个参数 index(可选),允许你在监听器数组中指定插入位置。
30
+ * 对于需要精确控制监听器调用顺序的场景非常有用(如前置拦截逻辑)。
31
+ * 🧪 event 支持正则表达式匹配事件名
32
+ * 监听器可使用正则表达式绑定多个相关事件,提升灵活性。
33
+ * 适用于统一处理一类命名模式的事件(如日志、状态变更等)。
36
34
  * 与 [event-emitter](https://github.com/medikoo/event-emitter) 的区别
37
35
  * **`改变`**: 事件支持冒泡机制(如上所述)
38
36
  * 添加了默认最大监听器数量的类属性,以保持与 Node 事件模块的兼容性。
@@ -41,16 +39,24 @@ TODO: 异步事件,添加`emitAsync`方法. 没这么简单,因为要支持bubbl
41
39
  * 添加了 `listeners()` 方法,以保持与 Node 事件模块的兼容性。
42
40
  * 添加了 `listenerCount()` 类方法,以保持与 Node 事件模块的兼容性。
43
41
  * 添加了`emitAsync`方法,支持异步事件
44
-
45
- 注意: 时间内部引发错误不会中断通知,但是会在通知结束时 emit 错误事件(`emit('error', error, 'notify', eventName, listener, args)`)
42
+ * 🔗 事件管道与统一:pipe() 与 unify()
43
+ * `pipe(source, target)`:将一个 emitter 的事件转发到另一个 emitter。
44
+ * `unify(emitter1, emitter2)`:双向同步事件流,适用于构建共享状态或通信桥梁。
45
+ * 📦 丰富的工具函数
46
+ * 提供如 `allOff()`, `hasListeners()`, `listenerCount()` 等辅助函数,便于调试与管理事件生命周期。
47
+ * 有助于构建更健壮的事件驱动系统。
48
+ * 🔌 模块化能力注入:`eventable()`
49
+ * 不必继承基类,可通过 eventable(MyClass) 将事件能力注入任意类。
50
+ * 支持配置只注入特定方法,避免污染原型链。
51
+
52
+ 注意: 事件内部引发错误不会中断通知,但是会在通知结束时 emit 错误事件(`emit('error', error, 'notify', eventName, listener, args)`)
46
53
 
47
54
  ### 安装
48
55
 
49
- ```
56
+ ```bash
50
57
  npm install events-ex@alpha
51
58
  ```
52
59
 
53
-
54
60
  ### 用法
55
61
 
56
62
  直接继承使用 `EventEmitter` 类
@@ -95,7 +101,6 @@ my.emit('event1');
95
101
  import {EventEmitter, states} from 'events-ex';
96
102
  import {isObject} from 'util-ex';
97
103
 
98
- states.ABORT = -1
99
104
  class MyDb extends EventEmitter {
100
105
  get(key) {
101
106
  // Demo the event object bubbling usage:
package/README.md CHANGED
@@ -1,6 +1,5 @@
1
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
-
4
3
  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.
5
4
 
6
5
  ### Features
@@ -25,6 +24,16 @@ Browser-friendly enhanced event emitter [ability][Ability] and class. It's modif
25
24
  * **`broken change`**: The `this` object of listeners' callback function is the `Event` Object instead of the emitter object.
26
25
  * The emitter object is put into the `target` property of the `Event` Object.
27
26
  * Adds async event emitting via `emitAsync` method.
27
+ * ⚡ Added `emitAsync` Method:
28
+ * Ensures all async listeners are executed **sequentially** (in registration and bubbling order) before returning results.
29
+ * Ideal for scenarios requiring ordered async execution (e.g., middleware-style validation, plugin pipelines, state transitions).
30
+ * Listener APIs: `on/once(event: string|RegExp, listener, index?: number)`
31
+ * 📌 **Index Parameter** (Optional):
32
+ * Allows specifying the insertion position in the listener array.
33
+ * Useful for precise control over listener execution order (e.g., pre-interception logic).
34
+ * 🧪 **Regex Event Matching**:
35
+ * Listeners can bind to multiple events via regex patterns.
36
+ * Great for handling events with naming patterns (e.g., logs, state changes).
28
37
  * Difference with [event-emitter](https://github.com/medikoo/event-emitter)
29
38
  + **`broken change`**: The event supports bubbling and interruption(see above)
30
39
  + Adds the defaultMaxListeners class property to keep compatibility with node events.
@@ -33,6 +42,15 @@ Browser-friendly enhanced event emitter [ability][Ability] and class. It's modif
33
42
  + Adds listeners() method to keep compatibility with node events.
34
43
  + Adds listenerCount() class method to keep compatibility with node events.
35
44
  * Adds async event emitting via `emitAsync` method.
45
+ * 🔗 **Event Piping & Unification**:
46
+ * `pipe(source, target)`: Forwards events from one emitter to another.
47
+ * `unify(emitter1, emitter2)`: Bi-directional event synchronization (e.g., shared state management).
48
+ * 📦 **Utility Functions**:
49
+ * Includes `allOff()`, `hasListeners()`, `listenerCount()` for debugging and lifecycle management.
50
+ * Enhances robustness in event-driven architectures.
51
+ * 🔌 **Modular Ability Injection**:
52
+ * `eventable(MyClass)`: Inject event capabilities into any class without inheritance.
53
+ * Configurable inclusion/exclusion of methods to avoid prototype pollution.
36
54
 
37
55
  Note: The listener throw error should not broke the notification, but it will emit error(`emit('error', error, 'notify', eventName, listener, args)`) after notification.
38
56
 
@@ -87,7 +105,6 @@ Bubbling event usage:
87
105
  import {EventEmitter, states} from 'events-ex';
88
106
  import {isObject} from 'util-ex';
89
107
 
90
- states.ABORT = -1
91
108
  class MyDb extends EventEmitter {
92
109
  get(key) {
93
110
  // Demo the event object bubbling usage:
package/docs/README.md CHANGED
@@ -26,6 +26,16 @@ Browser-friendly enhanced event emitter [ability][Ability] and class. It's modif
26
26
  * **`broken change`**: The `this` object of listeners' callback function is the `Event` Object instead of the emitter object.
27
27
  * The emitter object is put into the `target` property of the `Event` Object.
28
28
  * Adds async event emitting via `emitAsync` method.
29
+ * ⚡ Added `emitAsync` Method:
30
+ * Ensures all async listeners are executed **sequentially** (in registration and bubbling order) before returning results.
31
+ * Ideal for scenarios requiring ordered async execution (e.g., middleware-style validation, plugin pipelines, state transitions).
32
+ * Listener APIs: `on/once(event: string|RegExp, listener, index?: number)`
33
+ * 📌 **Index Parameter** (Optional):
34
+ * Allows specifying the insertion position in the listener array.
35
+ * Useful for precise control over listener execution order (e.g., pre-interception logic).
36
+ * 🧪 **Regex Event Matching**:
37
+ * Listeners can bind to multiple events via regex patterns.
38
+ * Great for handling events with naming patterns (e.g., logs, state changes).
29
39
  * Difference with [event-emitter](https://github.com/medikoo/event-emitter)
30
40
  + **`broken change`**: The event supports bubbling and interruption(see above)
31
41
  + Adds the defaultMaxListeners class property to keep compatibility with node events.
@@ -34,6 +44,15 @@ Browser-friendly enhanced event emitter [ability][Ability] and class. It's modif
34
44
  + Adds listeners() method to keep compatibility with node events.
35
45
  + Adds listenerCount() class method to keep compatibility with node events.
36
46
  * Adds async event emitting via `emitAsync` method.
47
+ * 🔗 **Event Piping & Unification**:
48
+ * `pipe(source, target)`: Forwards events from one emitter to another.
49
+ * `unify(emitter1, emitter2)`: Bi-directional event synchronization (e.g., shared state management).
50
+ * 📦 **Utility Functions**:
51
+ * Includes `allOff()`, `hasListeners()`, `listenerCount()` for debugging and lifecycle management.
52
+ * Enhances robustness in event-driven architectures.
53
+ * 🔌 **Modular Ability Injection**:
54
+ * `eventable(MyClass)`: Inject event capabilities into any class without inheritance.
55
+ * Configurable inclusion/exclusion of methods to avoid prototype pollution.
37
56
 
38
57
  Note: The listener throw error should not broke the notification, but it will emit error(`emit('error', error, 'notify', eventName, listener, args)`) after notification.
39
58
 
@@ -87,7 +106,6 @@ Bubbling event usage:
87
106
  import {EventEmitter, states} from 'events-ex';
88
107
  import {isObject} from 'util-ex';
89
108
 
90
- states.ABORT = -1
91
109
  class MyDb extends EventEmitter {
92
110
  get(key) {
93
111
  // Demo the event object bubbling usage:
@@ -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/0aebeb6/src/event.js#L8)
52
+ [src/event.js:8](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/event.js#L38)
64
+ [src/event.js:38](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/event.js#L32)
76
+ [src/event.js:32](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/event.js#L26)
88
+ [src/event.js:26](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/event.js#L44)
100
+ [src/event.js:44](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/event.js#L51)
118
+ [src/event.js:51](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/event.js#L20)
141
+ [src/event.js:20](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/event-emitter.js#L8)
29
+ [src/event-emitter.js:8](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.js#L8)
@@ -26,6 +26,7 @@ Class that represents an event emitter.
26
26
  - [on](event_emitter.EventEmitter.md#on)
27
27
  - [once](event_emitter.EventEmitter.md#once)
28
28
  - [removeAllListeners](event_emitter.EventEmitter.md#removealllisteners)
29
+ - [removeListener](event_emitter.EventEmitter.md#removelistener)
29
30
  - [setMaxListeners](event_emitter.EventEmitter.md#setmaxlisteners)
30
31
  - [listenerCount](event_emitter.EventEmitter.md#listenercount-1)
31
32
 
@@ -47,7 +48,7 @@ Class that represents an event emitter.
47
48
 
48
49
  #### Defined in
49
50
 
50
- [src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L7)
51
+ [src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L7)
51
52
 
52
53
  ## Methods
53
54
 
@@ -72,7 +73,7 @@ The result of the event.
72
73
 
73
74
  #### Defined in
74
75
 
75
- [src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L38)
76
+ [src/event-emitter.d.ts:48](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L48)
76
77
 
77
78
  ___
78
79
 
@@ -97,7 +98,7 @@ A promise that resolves with the result of the event.
97
98
 
98
99
  #### Defined in
99
100
 
100
- [src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L44)
101
+ [src/event-emitter.d.ts:54](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L54)
101
102
 
102
103
  ___
103
104
 
@@ -121,7 +122,7 @@ Returns the count of listeners that are registered to listen for the specified e
121
122
 
122
123
  #### Defined in
123
124
 
124
- [src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L66)
125
+ [src/event-emitter.d.ts:83](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L83)
125
126
 
126
127
  ___
127
128
 
@@ -145,7 +146,7 @@ Returns an array of functions that are registered to listen for the specified ev
145
146
 
146
147
  #### Defined in
147
148
 
148
- [src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L59)
149
+ [src/event-emitter.d.ts:76](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L76)
149
150
 
150
151
  ___
151
152
 
@@ -172,9 +173,13 @@ The EventEmitter instance to allow chaining.
172
173
 
173
174
  If the listener is not a function.
174
175
 
176
+ **`See`**
177
+
178
+ [removeListener](event_emitter.EventEmitter.md#removelistener)
179
+
175
180
  #### Defined in
176
181
 
177
- [src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L32)
182
+ [src/event-emitter.d.ts:33](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L33)
178
183
 
179
184
  ___
180
185
 
@@ -203,7 +208,7 @@ If the listener is not a function.
203
208
 
204
209
  #### Defined in
205
210
 
206
- [src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L16)
211
+ [src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L16)
207
212
 
208
213
  ___
209
214
 
@@ -232,7 +237,7 @@ If the listener is not a function.
232
237
 
233
238
  #### Defined in
234
239
 
235
- [src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L24)
240
+ [src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L24)
236
241
 
237
242
  ___
238
243
 
@@ -256,7 +261,40 @@ Removes all listeners for a specific event or all events from an event emitter.
256
261
 
257
262
  #### Defined in
258
263
 
259
- [src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L51)
264
+ [src/event-emitter.d.ts:61](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L61)
265
+
266
+ ___
267
+
268
+ ### removeListener
269
+
270
+ ▸ **removeListener**(`eventName`, `listener`): [`EventEmitter`](event_emitter.EventEmitter.md)
271
+
272
+ Removes a listener function from the specified event type.
273
+
274
+ #### Parameters
275
+
276
+ | Name | Type | Description |
277
+ | :------ | :------ | :------ |
278
+ | `eventName` | `string` \| `RegExp` | - |
279
+ | `listener` | `Function` | The listener function to be removed. |
280
+
281
+ #### Returns
282
+
283
+ [`EventEmitter`](event_emitter.EventEmitter.md)
284
+
285
+ The EventEmitter instance to allow chaining.
286
+
287
+ **`Throws`**
288
+
289
+ If the listener is not a function.
290
+
291
+ **`See`**
292
+
293
+ [off](event_emitter.EventEmitter.md#off)
294
+
295
+ #### Defined in
296
+
297
+ [src/event-emitter.d.ts:42](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L42)
260
298
 
261
299
  ___
262
300
 
@@ -264,19 +302,27 @@ ___
264
302
 
265
303
  ▸ **setMaxListeners**(`n`): [`EventEmitter`](event_emitter.EventEmitter.md)
266
304
 
305
+ Sets the maximum number of listeners allowed for the event emitter.
306
+
267
307
  #### Parameters
268
308
 
269
- | Name | Type |
270
- | :------ | :------ |
271
- | `n` | `number` |
309
+ | Name | Type | Description |
310
+ | :------ | :------ | :------ |
311
+ | `n` | `number` | The maximum number of listeners to set. Must be a positive integer. |
272
312
 
273
313
  #### Returns
274
314
 
275
315
  [`EventEmitter`](event_emitter.EventEmitter.md)
276
316
 
317
+ The [EventEmitter](event_emitter.EventEmitter.md) instance for method chaining.
318
+
319
+ **`Throws`**
320
+
321
+ If `n` is not a positive integer.
322
+
277
323
  #### Defined in
278
324
 
279
- [src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L52)
325
+ [src/event-emitter.d.ts:69](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L69)
280
326
 
281
327
  ___
282
328
 
@@ -303,4 +349,4 @@ use emitter.listenerCount instead
303
349
 
304
350
  #### Defined in
305
351
 
306
- [src/event-emitter.d.ts:74](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/event-emitter.d.ts#L74)
352
+ [src/event-emitter.d.ts:91](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/event-emitter.d.ts#L91)
@@ -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/0aebeb6/src/all-off.js#L12)
44
+ [src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/consts.js#L13)
20
+ [src/consts.js:13](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/consts.js#L6)
39
+ [src/consts.js:6](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/consts.js#L6)
@@ -41,11 +41,11 @@ Renames and re-exports [getEventableMethods](default_methods.md#geteventablemeth
41
41
  | `listenerCount` | (`emitter`: `any`, `type`: `any`) => `number` |
42
42
  | `listeners` | (`type`: `any`) => `any` |
43
43
  | `off` | (`type`: `string` \| `RegExp`, `listener`: `Function`) => [`EventEmitter`](../classes/event_emitter.EventEmitter.md) |
44
- | `on` | (`type`: `string` \| `RegExp`, `listener`: `Function`) => [`EventEmitter`](../classes/event_emitter.EventEmitter.md) |
45
- | `once` | (`type`: `string` \| `RegExp`, `listener`: `Function`) => [`EventEmitter`](../classes/event_emitter.EventEmitter.md) |
44
+ | `on` | (`type`: `string` \| `RegExp`, `listener`: `Function`, `index?`: `number`) => [`EventEmitter`](../classes/event_emitter.EventEmitter.md) |
45
+ | `once` | (`type`: `string` \| `RegExp`, `listener`: `Function`, `index?`: `number`) => [`EventEmitter`](../classes/event_emitter.EventEmitter.md) |
46
46
  | `removeAllListeners` | (`type`: `string` \| `RegExp`) => [`EventEmitter`](../classes/event_emitter.EventEmitter.md) |
47
- | `setMaxListeners` | (`n`: `any`) => \{ on(type: string \| RegExp, listener: Function): EventEmitter; once(type: string \| RegExp, listener: Function): EventEmitter; ... 6 more ...; removeAllListeners(type: string \| RegExp): EventEmitter; } |
47
+ | `setMaxListeners` | (`n`: `any`) => \{ on(type: string \| RegExp, listener: Function, index?: number): EventEmitter; once(type: string \| RegExp, listener: Function, index?: number): EventEmitter; ... 6 more ...; removeAllListeners(type: string \| RegExp): EventEmitter; } |
48
48
 
49
49
  #### Defined in
50
50
 
51
- [src/default-methods.js:14](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/default-methods.js#L14)
51
+ [src/default-methods.js:14](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/default-methods.js#L14)
@@ -46,4 +46,4 @@ Throws a TypeError if `obj` is null or undefined.
46
46
 
47
47
  #### Defined in
48
48
 
49
- [src/has-listeners.js:20](https://github.com/snowyu/events-ex.js/blob/0aebeb6/src/has-listeners.js#L20)
49
+ [src/has-listeners.js:20](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/pipe.js#L21)
49
+ [src/pipe.js:21](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/pipe-async.js#L21)
49
+ [src/pipe-async.js:21](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/unify.js#L17)
46
+ [src/unify.js:17](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/util/array-remove.js#L5)
39
+ [src/util/array-remove.js:5](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/util/object-for-each.js#L6)
41
+ [src/util/object-for-each.js:6](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/util/string-pad.js#L8)
40
+ [src/util/string-pad.js:8](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/util/to-int.js#L2)
39
+ [src/util/to-int.js:2](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/util/valid-callable.js#L1)
39
+ [src/util/valid-callable.js:1](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/util/valid-object.js#L3)
39
+ [src/util/valid-object.js:3](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/wrap-event-emitter.js#L7)
33
+ [src/wrap-event-emitter.js:7](https://github.com/snowyu/events-ex.js/blob/9c254b2/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/0aebeb6/src/wrap-event-emitter.js#L34)
57
+ [src/wrap-event-emitter.js:34](https://github.com/snowyu/events-ex.js/blob/9c254b2/src/wrap-event-emitter.js#L34)
@@ -3,18 +3,20 @@ export function getEventableMethods(aClass: any): {
3
3
  * Adds a listener function to the specified event type.
4
4
  * @param {string|RegExp} type - The event type to listen for.
5
5
  * @param {Function} listener - The listener function to be called when the event is emitted.
6
+ * @param {number} [index] - The index at which to insert the listener. If not specified, the listener will be added at the end of the listeners array.
6
7
  * @returns {import('./event-emitter').EventEmitter} The EventEmitter instance to allow chaining.
7
8
  * @throws {TypeError} If the listener is not a function.
8
9
  */
9
- on(type: string | RegExp, listener: Function): import('./event-emitter').EventEmitter;
10
+ on(type: string | RegExp, listener: Function, index?: number): import('./event-emitter').EventEmitter;
10
11
  /**
11
12
  * Adds a one-time listener function to the specified event type.
12
13
  * @param {string|RegExp} type - The event type to listen for.
13
14
  * @param {Function} listener - The listener function to be called once when the event is emitted.
15
+ * @param {number} [index] - The index at which to insert the listener. If not specified, the listener will be added at the end of the listeners array.
14
16
  * @returns {import('./event-emitter').EventEmitter} The EventEmitter instance to allow chaining.
15
17
  * @throws {TypeError} If the listener is not a function.
16
18
  */
17
- once(type: string | RegExp, listener: Function): import('./event-emitter').EventEmitter;
19
+ once(type: string | RegExp, listener: Function, index?: number): import('./event-emitter').EventEmitter;
18
20
  /**
19
21
  * Emits the specified event type with the given arguments.
20
22
  * @param {...*} args - The event type followed by any number of arguments to be passed to the listener functions.
@@ -21,10 +21,11 @@ function getEventableMethods(aClass) {
21
21
  * Adds a listener function to the specified event type.
22
22
  * @param {string|RegExp} type - The event type to listen for.
23
23
  * @param {Function} listener - The listener function to be called when the event is emitted.
24
+ * @param {number} [index] - The index at which to insert the listener. If not specified, the listener will be added at the end of the listeners array.
24
25
  * @returns {import('./event-emitter').EventEmitter} The EventEmitter instance to allow chaining.
25
26
  * @throws {TypeError} If the listener is not a function.
26
27
  */
27
- on(type, listener) {
28
+ on(type, listener, index) {
28
29
  if (!(0, _utilEx.isFunction)(listener)) {
29
30
  throw new TypeError(listener + ' is not a function');
30
31
  }
@@ -46,7 +47,11 @@ function getEventableMethods(aClass) {
46
47
  if (!data[type]) {
47
48
  data[type] = listener;
48
49
  } else if ((0, _utilEx.isObject)(data[type])) {
49
- data[type].push(listener);
50
+ if (typeof index === 'number') {
51
+ data[type].splice(index, 0, listener);
52
+ } else {
53
+ data[type].push(listener);
54
+ }
50
55
  } else {
51
56
  data[type] = [data[type], listener];
52
57
  }
@@ -69,10 +74,11 @@ function getEventableMethods(aClass) {
69
74
  * Adds a one-time listener function to the specified event type.
70
75
  * @param {string|RegExp} type - The event type to listen for.
71
76
  * @param {Function} listener - The listener function to be called once when the event is emitted.
77
+ * @param {number} [index] - The index at which to insert the listener. If not specified, the listener will be added at the end of the listeners array.
72
78
  * @returns {import('./event-emitter').EventEmitter} The EventEmitter instance to allow chaining.
73
79
  * @throws {TypeError} If the listener is not a function.
74
80
  */
75
- once(type, listener) {
81
+ once(type, listener, index) {
76
82
  if (!(0, _utilEx.isFunction)(listener)) {
77
83
  throw new TypeError(listener + ' is not a function');
78
84
  }
@@ -86,7 +92,7 @@ function getEventableMethods(aClass) {
86
92
  }
87
93
  }
88
94
  _once.listener = listener;
89
- this.on(type, _once);
95
+ this.on(type, _once, index);
90
96
  return this;
91
97
  },
92
98
  /**
@@ -28,8 +28,18 @@ export class EventEmitter {
28
28
  * @param {Function} listener - The listener function to be removed.
29
29
  * @returns {EventEmitter} The EventEmitter instance to allow chaining.
30
30
  * @throws {TypeError} If the listener is not a function.
31
+ * @see {@link removeListener}
31
32
  */
32
33
  off(eventName: string|RegExp, listener: ListenerCallbackFunc): EventEmitter;
34
+ /**
35
+ * Removes a listener function from the specified event type.
36
+ * @param {string|RegExp} type - The event type to remove the listener from.
37
+ * @param {Function} listener - The listener function to be removed.
38
+ * @returns {EventEmitter} The EventEmitter instance to allow chaining.
39
+ * @throws {TypeError} If the listener is not a function.
40
+ * @see {@link off}
41
+ */
42
+ removeListener(eventName: string|RegExp, listener: ListenerCallbackFunc): EventEmitter;
33
43
  /**
34
44
  * Emits the specified event type with the given arguments.
35
45
  * @param {...*} args - The event type followed by any number of arguments to be passed to the listener functions.
@@ -49,6 +59,13 @@ export class EventEmitter {
49
59
  * @returns {EventEmitter} - The event emitter with all listeners removed.
50
60
  */
51
61
  removeAllListeners(eventName?: string|RegExp): EventEmitter;
62
+ /**
63
+ * Sets the maximum number of listeners allowed for the event emitter.
64
+ *
65
+ * @param {number} n - The maximum number of listeners to set. Must be a positive integer.
66
+ * @returns {EventEmitter} The {@link EventEmitter} instance for method chaining.
67
+ * @throws {TypeError} If `n` is not a positive integer.
68
+ */
52
69
  setMaxListeners(n: number): EventEmitter;
53
70
  /**
54
71
  * Returns an array of functions that are registered to listen for the specified event.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "events-ex",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "description": "Browser-friendly enhanced events most compatible with standard node.js, it's powerful eventable ability.",
5
5
  "contributors": [
6
6
  {
@@ -17,10 +17,11 @@ export function getEventableMethods(aClass) {
17
17
  * Adds a listener function to the specified event type.
18
18
  * @param {string|RegExp} type - The event type to listen for.
19
19
  * @param {Function} listener - The listener function to be called when the event is emitted.
20
+ * @param {number} [index] - The index at which to insert the listener. If not specified, the listener will be added at the end of the listeners array.
20
21
  * @returns {import('./event-emitter').EventEmitter} The EventEmitter instance to allow chaining.
21
22
  * @throws {TypeError} If the listener is not a function.
22
23
  */
23
- on(type, listener) {
24
+ on(type, listener, index) {
24
25
  if (!isFunction(listener)) {throw new TypeError(listener + ' is not a function')}
25
26
  let data
26
27
  if (!this.hasOwnProperty('_events')) {
@@ -40,7 +41,11 @@ export function getEventableMethods(aClass) {
40
41
  if (!data[type]) {
41
42
  data[type] = listener
42
43
  } else if (isObject(data[type])) {
43
- data[type].push(listener)
44
+ if (typeof index === 'number') {
45
+ data[type].splice(index, 0, listener)
46
+ } else {
47
+ data[type].push(listener)
48
+ }
44
49
  } else {
45
50
  data[type] = [data[type], listener]
46
51
  }
@@ -69,10 +74,11 @@ export function getEventableMethods(aClass) {
69
74
  * Adds a one-time listener function to the specified event type.
70
75
  * @param {string|RegExp} type - The event type to listen for.
71
76
  * @param {Function} listener - The listener function to be called once when the event is emitted.
77
+ * @param {number} [index] - The index at which to insert the listener. If not specified, the listener will be added at the end of the listeners array.
72
78
  * @returns {import('./event-emitter').EventEmitter} The EventEmitter instance to allow chaining.
73
79
  * @throws {TypeError} If the listener is not a function.
74
80
  */
75
- once(type, listener) {
81
+ once(type, listener, index) {
76
82
  if (!isFunction(listener)) {throw new TypeError(listener + ' is not a function' )}
77
83
  let fired = false
78
84
  const self = this
@@ -85,7 +91,7 @@ export function getEventableMethods(aClass) {
85
91
  }
86
92
  }
87
93
  _once.listener = listener
88
- this.on(type, _once)
94
+ this.on(type, _once, index)
89
95
  return this
90
96
  },
91
97
 
@@ -28,8 +28,18 @@ export class EventEmitter {
28
28
  * @param {Function} listener - The listener function to be removed.
29
29
  * @returns {EventEmitter} The EventEmitter instance to allow chaining.
30
30
  * @throws {TypeError} If the listener is not a function.
31
+ * @see {@link removeListener}
31
32
  */
32
33
  off(eventName: string|RegExp, listener: ListenerCallbackFunc): EventEmitter;
34
+ /**
35
+ * Removes a listener function from the specified event type.
36
+ * @param {string|RegExp} type - The event type to remove the listener from.
37
+ * @param {Function} listener - The listener function to be removed.
38
+ * @returns {EventEmitter} The EventEmitter instance to allow chaining.
39
+ * @throws {TypeError} If the listener is not a function.
40
+ * @see {@link off}
41
+ */
42
+ removeListener(eventName: string|RegExp, listener: ListenerCallbackFunc): EventEmitter;
33
43
  /**
34
44
  * Emits the specified event type with the given arguments.
35
45
  * @param {...*} args - The event type followed by any number of arguments to be passed to the listener functions.
@@ -49,6 +59,13 @@ export class EventEmitter {
49
59
  * @returns {EventEmitter} - The event emitter with all listeners removed.
50
60
  */
51
61
  removeAllListeners(eventName?: string|RegExp): EventEmitter;
62
+ /**
63
+ * Sets the maximum number of listeners allowed for the event emitter.
64
+ *
65
+ * @param {number} n - The maximum number of listeners to set. Must be a positive integer.
66
+ * @returns {EventEmitter} The {@link EventEmitter} instance for method chaining.
67
+ * @throws {TypeError} If `n` is not a positive integer.
68
+ */
52
69
  setMaxListeners(n: number): EventEmitter;
53
70
  /**
54
71
  * Returns an array of functions that are registered to listen for the specified event.