events-ex 2.0.1 → 2.1.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/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.1.0](https://github.com/snowyu/events-ex.js/compare/v2.0.1...v2.1.0) (2025-06-09)
6
+
7
+
8
+ ### Features
9
+
10
+ * add optional index argument to on/off funcs ([0b91827](https://github.com/snowyu/events-ex.js/commit/0b91827e05cb252edf645820db74fd36496ed56d))
11
+
5
12
  ## [2.0.1](https://github.com/snowyu/events-ex.js/compare/v2.0.0...v2.0.1) (2025-03-08)
6
13
 
7
14
 
package/README.cn.md CHANGED
@@ -23,7 +23,7 @@ TODO: 异步事件,添加`emitAsync`方法. 没这么简单,因为要支持bubbl
23
23
  ### 区别
24
24
 
25
25
  * 与 [Node 事件模块](https://nodejs.org/api/events.html) 的区别
26
- * **`改变`**: 事件支持冒泡机制与中断
26
+ * 🔁 **`改变`**: 事件支持冒泡机制与中断
27
27
  * 事件对象(`Event Object`)作为监听器的 "this" 对象。
28
28
  * `result` 属性: 可选, 如果设置,则将该结果返回到事件发射器(`Event Emitter`)。
29
29
  * `stopped` 属性: 可选, 如果设置为 `true`,则会阻止剩余的监听器被执行。
@@ -32,7 +32,14 @@ TODO: 异步事件,添加`emitAsync`方法. 没这么简单,因为要支持bubbl
32
32
  * **`改变`**: `emit` 方法返回监听器回调函数的结果而不是成功状态。
33
33
  * **`改变`**: 监听器回调函数的 `this` 对象是 `Event Object` 事件对象而不是事件发射器对象。
34
34
  * 事件发射器对象被放入 `Event` 对象的 `target` 属性中。
35
- * 添加了`emitAsync`方法,支持异步事件
35
+ * 添加了`emitAsync`方法,确保所有异步监听器完成后再返回结果。
36
+ * 非常适合用于需要等待多个异步任务完成的场景(如数据验证、插件系统等)。
37
+ * 事件监听器`on/once(event: string|RegExp, listener, index?:number)`
38
+ * 📌 支持第三个参数 index(可选),允许你在监听器数组中指定插入位置。
39
+ * 对于需要精确控制监听器调用顺序的场景非常有用(如前置拦截逻辑)。
40
+ * 🧪 event 支持正则表达式匹配事件名
41
+ * 监听器可使用正则表达式绑定多个相关事件,提升灵活性。
42
+ * 适用于统一处理一类命名模式的事件(如日志、状态变更等)。
36
43
  * 与 [event-emitter](https://github.com/medikoo/event-emitter) 的区别
37
44
  * **`改变`**: 事件支持冒泡机制(如上所述)
38
45
  * 添加了默认最大监听器数量的类属性,以保持与 Node 事件模块的兼容性。
@@ -41,16 +48,24 @@ TODO: 异步事件,添加`emitAsync`方法. 没这么简单,因为要支持bubbl
41
48
  * 添加了 `listeners()` 方法,以保持与 Node 事件模块的兼容性。
42
49
  * 添加了 `listenerCount()` 类方法,以保持与 Node 事件模块的兼容性。
43
50
  * 添加了`emitAsync`方法,支持异步事件
44
-
45
- 注意: 时间内部引发错误不会中断通知,但是会在通知结束时 emit 错误事件(`emit('error', error, 'notify', eventName, listener, args)`)
51
+ * 🔗 事件管道与统一:pipe() 与 unify()
52
+ * `pipe(source, target)`:将一个 emitter 的事件转发到另一个 emitter。
53
+ * `unify(emitter1, emitter2)`:双向同步事件流,适用于构建共享状态或通信桥梁。
54
+ * 📦 丰富的工具函数
55
+ * 提供如 `allOff()`, `hasListeners()`, `listenerCount()` 等辅助函数,便于调试与管理事件生命周期。
56
+ * 有助于构建更健壮的事件驱动系统。
57
+ * 🔌 模块化能力注入:`eventable()`
58
+ * 不必继承基类,可通过 eventable(MyClass) 将事件能力注入任意类。
59
+ * 支持配置只注入特定方法,避免污染原型链。
60
+
61
+ 注意: 事件内部引发错误不会中断通知,但是会在通知结束时 emit 错误事件(`emit('error', error, 'notify', eventName, listener, args)`)
46
62
 
47
63
  ### 安装
48
64
 
49
- ```
65
+ ```bash
50
66
  npm install events-ex@alpha
51
67
  ```
52
68
 
53
-
54
69
  ### 用法
55
70
 
56
71
  直接继承使用 `EventEmitter` 类
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 complete before returning results.
29
+ * Ideal for scenarios requiring sequential async tasks (e.g., data validation, plugin systems).
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
 
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 complete before returning results.
31
+ * Ideal for scenarios requiring sequential async tasks (e.g., data validation, plugin systems).
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
 
@@ -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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L7)
50
+ [src/event-emitter.d.ts:7](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L38)
75
+ [src/event-emitter.d.ts:38](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L44)
100
+ [src/event-emitter.d.ts:44](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L66)
124
+ [src/event-emitter.d.ts:66](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L59)
148
+ [src/event-emitter.d.ts:59](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L32)
177
+ [src/event-emitter.d.ts:32](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L16)
206
+ [src/event-emitter.d.ts:16](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L24)
235
+ [src/event-emitter.d.ts:24](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L51)
259
+ [src/event-emitter.d.ts:51](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L52)
279
+ [src/event-emitter.d.ts:52](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/event-emitter.d.ts#L74)
306
+ [src/event-emitter.d.ts:74](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/0aebeb6/src/all-off.js#L12)
44
+ [src/all-off.js:12](https://github.com/snowyu/events-ex.js/blob/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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/a6cce2d/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
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "events-ex",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
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