@typescript-package/event-emitter 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -26,6 +26,9 @@ A **lightweight** TypeScript package for event emitter.
|
|
|
26
26
|
- Concrete
|
|
27
27
|
- [`EventEmitter`](#eventemitter)
|
|
28
28
|
- [`NamedEventEmitter`](#namedeventemitter)
|
|
29
|
+
- Type
|
|
30
|
+
- [`EventListeners`](#eventlisteners)
|
|
31
|
+
- [`ListenersFor`](#listenersfor)
|
|
29
32
|
- [Contributing](#contributing)
|
|
30
33
|
- [Code of Conduct](code-of-conduct)
|
|
31
34
|
- [Git](#git)
|
|
@@ -52,33 +55,33 @@ import {
|
|
|
52
55
|
} from '@typescript-package/event-emitter';
|
|
53
56
|
```
|
|
54
57
|
|
|
55
|
-
|
|
58
|
+
### Abstract
|
|
56
59
|
|
|
57
60
|
### `EventEmitterBase`
|
|
58
61
|
|
|
59
|
-
The base abstraction class for an event emitter pattern.
|
|
62
|
+
The base abstraction class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
60
63
|
|
|
61
64
|
```typescript
|
|
62
65
|
import { EventEmitter } from '@typescript-package/event-emitter';
|
|
63
66
|
```
|
|
64
67
|
|
|
65
|
-
[`EventEmitterBase`](https://github.com/typescript-package/
|
|
68
|
+
[`EventEmitterBase`](https://github.com/typescript-package/event-emitter/blob/main/src/lib/event-emitter.base.ts)
|
|
66
69
|
|
|
67
70
|
### `NamedEventEmitterBase`
|
|
68
71
|
|
|
69
|
-
A base abstraction class that implements a named event emitter pattern.
|
|
72
|
+
A base abstraction class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
70
73
|
|
|
71
74
|
```typescript
|
|
72
75
|
import { NamedEventEmitterBase } from '@typescript-package/event-emitter';
|
|
73
76
|
```
|
|
74
77
|
|
|
75
|
-
[`NamedEventEmitterBase`](https://github.com/typescript-package/
|
|
78
|
+
[`NamedEventEmitterBase`](https://github.com/typescript-package/event-emitter/blob/main/src/lib/named-event-emitter.base.ts)
|
|
76
79
|
|
|
77
|
-
|
|
80
|
+
### Concrete
|
|
78
81
|
|
|
79
82
|
### `EventEmitter`
|
|
80
83
|
|
|
81
|
-
A concrete class
|
|
84
|
+
A concrete class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
82
85
|
|
|
83
86
|
```typescript
|
|
84
87
|
import { EventEmitter } from '@typescript-package/event-emitter';
|
|
@@ -93,11 +96,11 @@ eventEmitter.emit('Hello, World!');
|
|
|
93
96
|
|
|
94
97
|
```
|
|
95
98
|
|
|
96
|
-
[`EventEmitter`](https://github.com/typescript-package/
|
|
99
|
+
[`EventEmitter`](https://github.com/typescript-package/event-emitter/blob/main/src/lib/event-emitter.class.ts)
|
|
97
100
|
|
|
98
101
|
### `NamedEventEmitter`
|
|
99
102
|
|
|
100
|
-
A concrete class that implements a named event emitter pattern.
|
|
103
|
+
A concrete class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
101
104
|
|
|
102
105
|
```typescript
|
|
103
106
|
import { NamedEventEmitter } from '@typescript-package/event-emitter';
|
|
@@ -126,7 +129,27 @@ eventEmitter.listeners('event1')?.forEach(listener => {
|
|
|
126
129
|
});
|
|
127
130
|
```
|
|
128
131
|
|
|
129
|
-
[`NamedEventEmitter`](https://github.com/typescript-package/
|
|
132
|
+
[`NamedEventEmitter`](https://github.com/typescript-package/event-emitter/blob/main/src/lib/named-event-emitter.class.ts)
|
|
133
|
+
|
|
134
|
+
### Type
|
|
135
|
+
|
|
136
|
+
### `EventListeners`
|
|
137
|
+
|
|
138
|
+
A type representing a partial mapping of event names to their listener arrays.
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
import { EventListeners } from '@typescript-package/event-emitter';
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### `ListenersFor`
|
|
145
|
+
|
|
146
|
+
A type representing the listeners for a specific event in a named event emitter, utilizing a specified listeners adapter and supporting asynchronous capabilities.
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
import { EventListeners } from '@typescript-package/event-emitter';
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
[`EventListeners`](https://github.com/typescript-package/event-emitter/blob/main/src/type/listeners-for.type.ts)
|
|
130
153
|
|
|
131
154
|
## Contributing
|
|
132
155
|
|
|
@@ -138,19 +161,23 @@ If you find this package useful and would like to support its and general develo
|
|
|
138
161
|
|
|
139
162
|
Support via:
|
|
140
163
|
|
|
141
|
-
- [
|
|
142
|
-
- [Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)
|
|
143
|
-
- [GitHub](https://github.com/sponsors/angular-package/sponsorships?sponsor=sciborrudnicki&tier_id=83618)
|
|
164
|
+
- [4Fund](https://4fund.com/bruubs)
|
|
144
165
|
- [DonorBox](https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o)
|
|
166
|
+
- [GitHub](https://github.com/sponsors/angular-package/sponsorships?sponsor=sciborrudnicki&tier_id=83618)
|
|
167
|
+
- [Ko-fi](https://ko-fi.com/sterblack)
|
|
168
|
+
- [OpenCollective](https://opencollective.com/sterblack)
|
|
145
169
|
- [Patreon](https://www.patreon.com/checkout/angularpackage?rid=0&fan_landing=true&view_as=public)
|
|
170
|
+
- [PayPal](https://paypal.me/sterblack)
|
|
171
|
+
- [Stripe](https://donate.stripe.com/dR614hfDZcJE3wAcMM)
|
|
172
|
+
- ~~[Revolut](https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29)~~
|
|
146
173
|
|
|
147
174
|
or via Trust Wallet
|
|
148
175
|
|
|
149
|
-
- [XLM](https://link.trustwallet.com/send?coin=148&address=GAFFFB7H3LG42O6JA63FJDRK4PP4JCNEOPHLGLLFH625X2KFYQ4UYVM4)
|
|
150
|
-
- [USDT (BEP20)](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94&token_id=0x55d398326f99059fF775485246999027B3197955)
|
|
151
|
-
- [ETH](https://link.trustwallet.com/send?coin=60&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94)
|
|
152
|
-
- [BTC](https://link.trustwallet.com/send?coin=0&address=bc1qnf709336tfl57ta5mfkf4t9fndhx7agxvv9svn)
|
|
153
176
|
- [BNB](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94)
|
|
177
|
+
- [BTC](https://link.trustwallet.com/send?coin=0&address=bc1qnf709336tfl57ta5mfkf4t9fndhx7agxvv9svn)
|
|
178
|
+
- [ETH](https://link.trustwallet.com/send?coin=60&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94)
|
|
179
|
+
- [USDT (BEP20)](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94&token_id=0x55d398326f99059fF775485246999027B3197955)
|
|
180
|
+
- [XLM](https://link.trustwallet.com/send?coin=148&address=GAFFFB7H3LG42O6JA63FJDRK4PP4JCNEOPHLGLLFH625X2KFYQ4UYVM4)
|
|
154
181
|
|
|
155
182
|
Thanks for your support!
|
|
156
183
|
|
|
@@ -1,31 +1,70 @@
|
|
|
1
|
-
import { Listeners } from '@typescript-package/listeners';
|
|
2
1
|
import { SetAdapter } from '@typescript-package/collection-adapter';
|
|
2
|
+
import { Listeners } from '@typescript-package/listeners';
|
|
3
|
+
|
|
4
|
+
class ListenersSetAdapter extends SetAdapter {
|
|
5
|
+
version = '1.0.0';
|
|
6
|
+
once(...listeners) {
|
|
7
|
+
listeners.forEach(listener => {
|
|
8
|
+
if (super.has(listener)) {
|
|
9
|
+
throw new Error('Listener already exists in the collection.');
|
|
10
|
+
}
|
|
11
|
+
const onceListener = (...args) => (super.delete(onceListener),
|
|
12
|
+
listener(...args));
|
|
13
|
+
super.add(onceListener);
|
|
14
|
+
});
|
|
15
|
+
return this;
|
|
16
|
+
}
|
|
17
|
+
snapshot() {
|
|
18
|
+
return Array.from(this.value);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
3
21
|
|
|
4
22
|
// Class.
|
|
5
23
|
/**
|
|
6
|
-
* @description The base abstraction class for an event emitter pattern.
|
|
24
|
+
* @description The base abstraction class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
7
25
|
* @export
|
|
8
26
|
* @abstract
|
|
9
27
|
* @class EventEmitterBase
|
|
10
|
-
* @template {ListenerFunction<any[]>}
|
|
28
|
+
* @template {ListenerFunction<any[]>} L The listener function type.
|
|
11
29
|
* @template [T=any] The type of the listeners underlying data.
|
|
12
30
|
* @template {boolean} [R=false] The async flag for the listeners.
|
|
13
|
-
* @template {ListenersAdapter<Parameters<
|
|
31
|
+
* @template {ListenersAdapter<Parameters<L>, L, T, R>} [A=ListenersAdapter<Parameters<L>, L, T, R>] The adapter type for the listeners.
|
|
14
32
|
*/
|
|
15
33
|
class EventEmitterBase {
|
|
16
34
|
/**
|
|
17
|
-
* @description
|
|
18
|
-
* @
|
|
35
|
+
* @description Gets the async flag for the listeners.
|
|
36
|
+
* @public
|
|
37
|
+
* @readonly
|
|
38
|
+
* @type {R}
|
|
19
39
|
*/
|
|
20
|
-
|
|
40
|
+
get async() {
|
|
41
|
+
return this.#listeners.async;
|
|
42
|
+
}
|
|
21
43
|
/**
|
|
22
|
-
* @description
|
|
23
|
-
* @
|
|
44
|
+
* @description Gets the listeners for a specific event type.
|
|
45
|
+
* @public
|
|
46
|
+
* @readonly
|
|
47
|
+
* @type {Listeners<A, L, Parameters<L>, T, R>} The listeners for the event.
|
|
24
48
|
*/
|
|
25
|
-
|
|
49
|
+
get listeners() {
|
|
50
|
+
return this.#listeners;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* @description Gets the adapter constructor.
|
|
54
|
+
* @protected
|
|
55
|
+
* @returns {new (...listeners: {}) => A} The adapter constructor.
|
|
56
|
+
*/
|
|
57
|
+
adapterCtor() {
|
|
58
|
+
return this.#adapterCtor;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* @description The adapter class used to manage listeners.
|
|
62
|
+
* @type {new (...listeners: L[]) => A}
|
|
63
|
+
*/
|
|
64
|
+
#adapterCtor;
|
|
26
65
|
/**
|
|
27
66
|
* @description The map of events to their listeners.
|
|
28
|
-
* @type {Listeners<Parameters<
|
|
67
|
+
* @type {Listeners<A, L, Parameters<L>, T, R>}
|
|
29
68
|
*/
|
|
30
69
|
#listeners;
|
|
31
70
|
/**
|
|
@@ -36,16 +75,13 @@ class EventEmitterBase {
|
|
|
36
75
|
/**
|
|
37
76
|
* Creates an instance of `EventEmitterBase`.
|
|
38
77
|
* @constructor
|
|
39
|
-
* @param {
|
|
40
|
-
* @param {
|
|
41
|
-
* @param {
|
|
42
|
-
* @param {new (...listeners: E[]) => A} adapter The adapter class to manage listeners.
|
|
43
|
-
* @param {?(E | E[])} [events] The initial listeners.
|
|
78
|
+
* @param {R} async Whether the emitter listeners operate asynchronously.
|
|
79
|
+
* @param {new (...listeners: L[]) => A} adapter The adapter class to manage listeners.
|
|
80
|
+
* @param {?(L | L[])} [listeners] The initial listeners.
|
|
44
81
|
*/
|
|
45
|
-
constructor(
|
|
46
|
-
this.#listeners = new Listeners(async
|
|
47
|
-
this.#
|
|
48
|
-
this.#async = async ?? false;
|
|
82
|
+
constructor(async, adapter, listeners) {
|
|
83
|
+
this.#listeners = new Listeners(async, adapter, ...(Array.isArray(listeners) ? listeners : listeners ? [listeners] : []));
|
|
84
|
+
this.#adapterCtor = adapter;
|
|
49
85
|
}
|
|
50
86
|
/**
|
|
51
87
|
* @description Removes all listeners for a specific event type.
|
|
@@ -66,7 +102,7 @@ class EventEmitterBase {
|
|
|
66
102
|
/**
|
|
67
103
|
* @description Emits an event, calling all listeners for that event type.
|
|
68
104
|
* @public
|
|
69
|
-
* @param {...Parameters<
|
|
105
|
+
* @param {...Parameters<L>} args The arguments for the event listeners.
|
|
70
106
|
*/
|
|
71
107
|
emit(...args) {
|
|
72
108
|
return !this.isPaused() && this.#listeners.forEach(listener => listener(...args)),
|
|
@@ -80,23 +116,15 @@ class EventEmitterBase {
|
|
|
80
116
|
isPaused() {
|
|
81
117
|
return this.#paused === true;
|
|
82
118
|
}
|
|
83
|
-
/**
|
|
84
|
-
* @description Gets the listeners for a specific event type.
|
|
85
|
-
* @public
|
|
86
|
-
* @returns {(ListenersFor<Event, E, T, R> | undefined)} The listeners for the event.
|
|
87
|
-
*/
|
|
88
|
-
listeners() {
|
|
89
|
-
return this.#listeners;
|
|
90
|
-
}
|
|
91
119
|
/**
|
|
92
120
|
* @description Emits an event asynchronously, calling all listeners for that event type.
|
|
93
121
|
* @public
|
|
94
122
|
* @async
|
|
95
|
-
* @param {...Parameters<
|
|
123
|
+
* @param {...Parameters<L>} args The arguments for the event listeners.
|
|
96
124
|
* @returns {this} The current instance.
|
|
97
125
|
*/
|
|
98
126
|
async emitAsync(...args) {
|
|
99
|
-
const listeners = this.listeners
|
|
127
|
+
const listeners = this.listeners;
|
|
100
128
|
if (!listeners || this.isPaused()) {
|
|
101
129
|
return this;
|
|
102
130
|
}
|
|
@@ -107,7 +135,7 @@ class EventEmitterBase {
|
|
|
107
135
|
/**
|
|
108
136
|
* @description Adds a listener for event.
|
|
109
137
|
* @public
|
|
110
|
-
* @param {
|
|
138
|
+
* @param {L} listener The listener function.
|
|
111
139
|
* @returns {this} The current instance.
|
|
112
140
|
*/
|
|
113
141
|
on(listener) {
|
|
@@ -117,7 +145,7 @@ class EventEmitterBase {
|
|
|
117
145
|
/**
|
|
118
146
|
* @description Adds a one-time listener for event.
|
|
119
147
|
* @public
|
|
120
|
-
* @param {
|
|
148
|
+
* @param {L} listener The listener function.
|
|
121
149
|
* @returns {this} The current instance.
|
|
122
150
|
*/
|
|
123
151
|
once(listener) {
|
|
@@ -127,7 +155,7 @@ class EventEmitterBase {
|
|
|
127
155
|
/**
|
|
128
156
|
* @description Removes a listener for event.
|
|
129
157
|
* @public
|
|
130
|
-
* @param {
|
|
158
|
+
* @param {L} listener The listener function.
|
|
131
159
|
* @returns {this} The current instance.
|
|
132
160
|
*/
|
|
133
161
|
off(listener) {
|
|
@@ -153,29 +181,29 @@ class EventEmitterBase {
|
|
|
153
181
|
|
|
154
182
|
// Class.
|
|
155
183
|
/**
|
|
156
|
-
* @description A base abstraction class that implements a named event emitter pattern.
|
|
184
|
+
* @description A base abstraction class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
157
185
|
* @export
|
|
158
186
|
* @abstract
|
|
159
187
|
* @class NamedEventEmitterBase
|
|
160
188
|
* @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
|
|
161
|
-
* @template [T=
|
|
162
|
-
* @template
|
|
163
|
-
* @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E],
|
|
189
|
+
* @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>] The adapter type for the listeners.
|
|
190
|
+
* @template [T=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.
|
|
191
|
+
* @template {boolean} [R=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false] The async flag for the listeners, inferred from the adapter if possible.
|
|
164
192
|
*/
|
|
165
193
|
class NamedEventEmitterBase {
|
|
166
194
|
/**
|
|
167
195
|
* @description The adapter class used to manage listeners.
|
|
168
196
|
* @type {new (...listeners: E[keyof E][]) => A}
|
|
169
197
|
*/
|
|
170
|
-
#
|
|
198
|
+
#adapterCtor;
|
|
171
199
|
/**
|
|
172
|
-
* @description
|
|
200
|
+
* @description The async flag for the listeners.
|
|
173
201
|
* @type {R}
|
|
174
202
|
*/
|
|
175
203
|
#async;
|
|
176
204
|
/**
|
|
177
205
|
* @description The map of events to their listeners.
|
|
178
|
-
* @type {Map<keyof E, Listeners<
|
|
206
|
+
* @type {Map<keyof E, Listeners<A, E[keyof E], Parameters<E[keyof E]>, T, R>>}
|
|
179
207
|
*/
|
|
180
208
|
#events = new Map();
|
|
181
209
|
/**
|
|
@@ -186,17 +214,15 @@ class NamedEventEmitterBase {
|
|
|
186
214
|
/**
|
|
187
215
|
* Creates an instance of `NamedEventEmitterBase`.
|
|
188
216
|
* @constructor
|
|
189
|
-
* @param {
|
|
190
|
-
* @param {
|
|
191
|
-
* @param {
|
|
192
|
-
* @param {new (...listeners: E[keyof E][]) => A} adapter The adapter class to manage listeners.
|
|
193
|
-
* @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial events and their listeners.
|
|
217
|
+
* @param {R} async Whether the emitter listeners operate asynchronously.
|
|
218
|
+
* @param {new (...listeners: E[keyof E][]) => A} adapter The adapter class to manage listeners.
|
|
219
|
+
* @param {?EventListeners<E>} [events] The initial events and their listeners.
|
|
194
220
|
*/
|
|
195
|
-
constructor(
|
|
196
|
-
this.#
|
|
197
|
-
this.#async = async
|
|
221
|
+
constructor(async, adapter, events) {
|
|
222
|
+
this.#adapterCtor = adapter;
|
|
223
|
+
this.#async = async;
|
|
198
224
|
for (const [event, listeners] of Object.entries(events ?? {})) {
|
|
199
|
-
this.#events.set(event, new Listeners(
|
|
225
|
+
this.#events.set(event, new Listeners(async, adapter));
|
|
200
226
|
for (const listener of listeners) {
|
|
201
227
|
this.#events.get(event).add(listener);
|
|
202
228
|
}
|
|
@@ -295,7 +321,7 @@ class NamedEventEmitterBase {
|
|
|
295
321
|
* @returns {this} The current instance.
|
|
296
322
|
*/
|
|
297
323
|
on(event, listener) {
|
|
298
|
-
return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#
|
|
324
|
+
return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapterCtor)),
|
|
299
325
|
this.#events.get(event).add(listener),
|
|
300
326
|
this;
|
|
301
327
|
}
|
|
@@ -308,7 +334,7 @@ class NamedEventEmitterBase {
|
|
|
308
334
|
* @returns {this} The current instance.
|
|
309
335
|
*/
|
|
310
336
|
once(event, listener) {
|
|
311
|
-
return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#
|
|
337
|
+
return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapterCtor)),
|
|
312
338
|
this.#events.get(event).once(listener),
|
|
313
339
|
this;
|
|
314
340
|
}
|
|
@@ -345,66 +371,41 @@ class NamedEventEmitterBase {
|
|
|
345
371
|
}
|
|
346
372
|
}
|
|
347
373
|
|
|
348
|
-
class ListenersSetAdapter extends SetAdapter {
|
|
349
|
-
version = '1.0.0';
|
|
350
|
-
once(...listeners) {
|
|
351
|
-
listeners.forEach(listener => {
|
|
352
|
-
if (this.has(listener)) {
|
|
353
|
-
throw new Error('Listener already exists in the collection.');
|
|
354
|
-
}
|
|
355
|
-
const onceListener = (...args) => (this.delete(onceListener),
|
|
356
|
-
listener(...args));
|
|
357
|
-
this.add(onceListener);
|
|
358
|
-
});
|
|
359
|
-
return this;
|
|
360
|
-
}
|
|
361
|
-
snapshot() {
|
|
362
|
-
return Array.from(this.value);
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
374
|
// Abstract.
|
|
367
375
|
/**
|
|
368
|
-
* @description A concrete class
|
|
376
|
+
* @description A concrete class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
369
377
|
* @export
|
|
370
378
|
* @class EventEmitter
|
|
371
|
-
* @template {ListenerFunction<any[]>}
|
|
372
|
-
* @template [
|
|
379
|
+
* @template {ListenerFunction<any[]>} L The listener function type.
|
|
380
|
+
* @template {ListenersAdapter<Parameters<L>, L, T, any>} [A=ListenersAdapter<Parameters<L>, L, any, any>] The adapter type for the listeners.
|
|
381
|
+
* @template [T=A extends ListenersAdapter<Parameters<L>, L, infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.
|
|
373
382
|
* @template {boolean} [R=false] The async flag for the listeners.
|
|
374
|
-
* @
|
|
375
|
-
* @extends {EventEmitterBase<E, any, R, A>}Base class abstracting common event emitter functionality.
|
|
383
|
+
* @extends {EventEmitterBase<L, T, R, A>}
|
|
376
384
|
*/
|
|
377
385
|
class EventEmitter extends EventEmitterBase {
|
|
378
386
|
/**
|
|
379
387
|
* Creates an instance of `EventEmitter`.
|
|
380
388
|
* @constructor
|
|
381
|
-
* @param {R}
|
|
382
|
-
* @param {
|
|
383
|
-
* @param {new (...listeners: E[]) => A} [adapter=ListenersSetAdapter as any]
|
|
384
|
-
*/
|
|
385
|
-
/**
|
|
386
|
-
* Creates an instance of `EventEmitter`.
|
|
387
|
-
* @constructor
|
|
388
|
-
* @param {{adapter?: new (...listeners: E[]) => A, async?: R, value?: T}} param0
|
|
389
|
-
* @param {new (...listeners: {}) => A} param0.adapter The adapter class to manage listeners.
|
|
389
|
+
* @param {{adapter?: new (...listeners: L[]) => A, async?: R}} param0 The options for the event emitter.
|
|
390
|
+
* @param {new (...listeners: L[]) => A} param0.adapter The adapter class to manage listeners.
|
|
390
391
|
* @param {R} param0.async Whether the emitter listeners operate asynchronously.
|
|
391
|
-
* @param {
|
|
392
|
-
* @param {?(E | E[])} [events] The initial listeners.
|
|
392
|
+
* @param {?(L | L[])} [listeners] The initial listeners.
|
|
393
393
|
*/
|
|
394
|
-
constructor({ adapter, async
|
|
395
|
-
super(
|
|
394
|
+
constructor({ adapter, async }, listeners) {
|
|
395
|
+
super(async ?? false, adapter ?? ListenersSetAdapter, listeners);
|
|
396
396
|
}
|
|
397
397
|
}
|
|
398
398
|
|
|
399
399
|
// Abstract.
|
|
400
400
|
/**
|
|
401
|
-
* @description A concrete class that implements a named event emitter pattern.
|
|
401
|
+
* @description A concrete class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
402
402
|
* @export
|
|
403
403
|
* @class NamedEventEmitter
|
|
404
404
|
* @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
|
|
405
|
-
* @template {
|
|
406
|
-
* @template
|
|
407
|
-
* @extends
|
|
405
|
+
* @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>] The adapter type for the listeners.
|
|
406
|
+
* @template [T=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.
|
|
407
|
+
* @template {boolean} [R=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false] The async flag for the listeners, inferred from the adapter if possible.
|
|
408
|
+
* @extends {NamedEventEmitterBase<E, A, T, R>} The base class for the named event emitter.
|
|
408
409
|
*/
|
|
409
410
|
class NamedEventEmitter extends NamedEventEmitterBase {
|
|
410
411
|
/**
|
|
@@ -414,8 +415,8 @@ class NamedEventEmitter extends NamedEventEmitterBase {
|
|
|
414
415
|
* @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial listeners.
|
|
415
416
|
* @param {new (...listeners: E[keyof E][]) => A} [adapter=ListenersSetAdapter as any] The adapter class to manage listeners.
|
|
416
417
|
*/
|
|
417
|
-
constructor({ adapter, async
|
|
418
|
-
super(
|
|
418
|
+
constructor({ adapter, async }, events) {
|
|
419
|
+
super(async ?? false, adapter ?? ListenersSetAdapter, events);
|
|
419
420
|
}
|
|
420
421
|
}
|
|
421
422
|
|
|
@@ -429,5 +430,5 @@ class NamedEventEmitter extends NamedEventEmitterBase {
|
|
|
429
430
|
* Generated bundle index. Do not edit.
|
|
430
431
|
*/
|
|
431
432
|
|
|
432
|
-
export { EventEmitter, EventEmitterBase, NamedEventEmitter, NamedEventEmitterBase };
|
|
433
|
+
export { EventEmitter, EventEmitterBase, ListenersSetAdapter, NamedEventEmitter, NamedEventEmitterBase };
|
|
433
434
|
//# sourceMappingURL=typescript-package-event-emitter.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"typescript-package-event-emitter.mjs","sources":["../../../package/event-emitter/src/lib/event-emitter.base.ts","../../../package/event-emitter/src/lib/named-event-emitter.base.ts","../../../package/event-emitter/src/adapter/lib/listeners-set.adapter.ts","../../../package/event-emitter/src/lib/event-emitter.class.ts","../../../package/event-emitter/src/lib/named-event-emitter.class.ts","../../../package/event-emitter/src/lib/index.ts","../../../package/event-emitter/src/public-api.ts","../../../package/event-emitter/src/typescript-package-event-emitter.ts"],"sourcesContent":["// Class.\nimport { Listeners } from \"@typescript-package/listeners\";\n// Type & Interface.\nimport { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\n/**\n * @description The base abstraction class for an event emitter pattern.\n * @export\n * @abstract\n * @class EventEmitterBase\n * @template {ListenerFunction<any[]>} E The listener function type.\n * @template [T=any] The type of the listeners underlying data.\n * @template {boolean} [R=false] The async flag for the listeners.\n * @template {ListenersAdapter<Parameters<E>, E, T, R>} [A=any] The adapter type for the listeners.\n */\nexport abstract class EventEmitterBase<\n E extends ListenerFunction<any[]>,\n T = any,\n R extends boolean = false,\n A extends ListenersAdapter<Parameters<E>, E, T, R> = any\n> {\n /**\n * @description The adapter class used to manage listeners.\n * @type {new (...listeners: E[]) => A}\n */\n #adapter: new (...listeners: E[]) => A;\n\n /**\n * @description Indicates whether the emitter listeners operate asynchronously.\n * @type {R}\n */\n #async: R;\n\n /**\n * @description The map of events to their listeners.\n * @type {Listeners<Parameters<E>, E, T, R, A>}\n */\n #listeners: Listeners<Parameters<E>, E, T, R, A>;\n\n /**\n * @description The paused state of the event emitter.\n * @type {boolean}\n */\n #paused: boolean = false;\n\n /**\n * Creates an instance of `EventEmitterBase`.\n * @constructor\n * @param {{async?: R, value?: T}} param0 The object with configuration options.\n * @param {R} param0.async Whether the emitter listeners operate asynchronously.\n * @param {T} param0.value The underlying data for the listeners for capture its type only.\n * @param {new (...listeners: E[]) => A} adapter The adapter class to manage listeners.\n * @param {?(E | E[])} [events] The initial listeners.\n */\n constructor(\n {async, value}: {async?: R, value?: T},\n adapter: new (...listeners: E[]) => A,\n events?: E | E[]\n ) {\n this.#listeners = new Listeners(async ?? false, adapter, ...(Array.isArray(events) ? events : events ? [events] : []));\n this.#adapter = adapter;\n this.#async = async ?? false as R;\n }\n\n /**\n * @description Removes all listeners for a specific event type.\n * @public\n * @returns {this} The current instance.\n */\n public clear(): this {\n return this.#listeners?.clear(), this;\n }\n\n /**\n * @description Gets the number of listeners for a specific event.\n * @public\n * @returns {number} The number of listeners for the event.\n */\n public count(): number {\n return this.#listeners.size;\n }\n\n /**\n * @description Emits an event, calling all listeners for that event type.\n * @public\n * @param {...Parameters<E>} args The arguments for the event listeners.\n */\n public emit(...args: Parameters<E>): this {\n return !this.isPaused() && this.#listeners.forEach(listener => listener(...args)),\n this\n }\n\n /**\n * @description Checks if the event is paused.\n * @public\n * @returns {boolean} Whether the event is paused.\n */\n public isPaused(): boolean {\n return this.#paused === true;\n }\n\n /**\n * @description Gets the listeners for a specific event type.\n * @public\n * @returns {(ListenersFor<Event, E, T, R> | undefined)} The listeners for the event.\n */\n public listeners(): Listeners<Parameters<E>, E, T, R, A> {\n return this.#listeners;\n }\n\n /**\n * @description Emits an event asynchronously, calling all listeners for that event type.\n * @public\n * @async\n * @param {...Parameters<E>} args The arguments for the event listeners.\n * @returns {this} The current instance.\n */\n public async emitAsync(...args: Parameters<E>): Promise<this> {\n const listeners = this.listeners();\n if (!listeners || this.isPaused()) {\n return this;\n }\n const snapshot = await listeners.snapshot();\n await Promise.all(snapshot.map(listener => Promise.resolve(listener(...args))));\n return this;\n }\n\n /**\n * @description Adds a listener for event.\n * @public\n * @param {E} listener The listener function.\n * @returns {this} The current instance.\n */\n public on(listener: E): this {\n return this.#listeners.add(listener),\n this;\n }\n\n /**\n * @description Adds a one-time listener for event.\n * @public\n * @param {E} listener The listener function.\n * @returns {this} The current instance.\n */\n public once(listener: E): this {\n return this.#listeners.once(listener),\n this;\n }\n\n /**\n * @description Removes a listener for event.\n * @public\n * @param {E} listener The listener function.\n * @returns {this} The current instance.\n */\n public off(listener: E): this {\n return this.#listeners.delete(listener), this;\n }\n\n /**\n * @description Pauses the event, preventing it from being emitted.\n * @public\n * @returns {this} The current instance.\n */\n public pause(): this {\n return this.#paused = true, this;\n }\n\n /**\n * @description Resumes the event if it was paused.\n * @public\n * @returns {this} The current instance.\n */\n public resume(): this {\n return this.#paused = false, this;\n }\n}\n","// Class.\nimport { Listeners } from \"@typescript-package/listeners\";\n// Type & Interface.\nimport { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\nimport { ListenersFor } from \"../type\";\n/**\n * @description A base abstraction class that implements a named event emitter pattern.\n * @export\n * @abstract\n * @class NamedEventEmitterBase\n * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.\n * @template [T=any] The type of the underlying data for the listeners.\n * @template {boolean} [R=false] The `boolean` type of the async flag for the listeners.\n * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=any] The adapter type for the listeners.\n */\nexport abstract class NamedEventEmitterBase<\n E extends Record<string, ListenerFunction<any[]>>,\n T = any,\n R extends boolean = false,\n A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = any\n> {\n /**\n * @description The adapter class used to manage listeners.\n * @type {new (...listeners: E[keyof E][]) => A}\n */\n #adapter: new (...listeners: E[keyof E][]) => A;\n\n /**\n * @description Indicates whether the emitter listeners operate asynchronously.\n * @type {R}\n */\n #async: R;\n\n /**\n * @description The map of events to their listeners.\n * @type {Map<keyof E, Listeners<Parameters<E[keyof E]>, E[keyof E], T, R, A>>}\n */\n #events: Map<keyof E, Listeners<Parameters<E[keyof E]>, E[keyof E], T, R, A>> = new Map();\n\n /**\n * @description The set of names of paused events.\n * @type {Set<keyof E>}\n */\n #pausedEvents: Set<keyof E> = new Set();\n\n /**\n * Creates an instance of `NamedEventEmitterBase`.\n * @constructor\n * @param {{async?: R, value?: T}} param0 \n * @param {R} param0.async Whether the emitter listeners operate asynchronously.\n * @param {T} param0.value The value underlying data for the listeners for capture its type only.\n * @param {new (...listeners: E[keyof E][]) => A} adapter The adapter class to manage listeners.\n * @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial events and their listeners.\n */\n constructor(\n {async, value}: {async?: R, value?: T},\n adapter: new (...listeners: E[keyof E][]) => A,\n events?: Partial<{ [K in keyof E]: E[K][] }>\n ) {\n this.#adapter = adapter;\n this.#async = async ?? false as R;\n for (const [event, listeners] of Object.entries(events ?? {})) {\n this.#events.set(event as keyof E, new Listeners(this.#async, this.#adapter));\n for (const listener of listeners!) {\n this.#events.get(event as keyof E)!.add(listener);\n }\n }\n }\n\n /**\n * @description Removes all listeners for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {this} The current instance.\n */\n public clear<Event extends keyof E>(event: Event): this {\n return this.#events.get(event)?.clear(), this;\n }\n\n /**\n * @description Removes all listeners for all event types.\n * @public\n * @returns {this} The current instance.\n */\n public clearAll(): this {\n return this.#events.clear(), this;\n }\n\n /**\n * @description Gets the number of listeners for a specific event.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {number} The number of listeners for the event.\n */\n public count<Event extends keyof E>(event: Event): number {\n return this.#events.get(event)?.size ?? 0;\n }\n\n /**\n * @description Emits an event, calling all listeners for that event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {...Parameters<E[Event]>} args The arguments for the event listeners.\n */\n public emit<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): this {\n return !this.isPaused(event) && this.#events.get(event)?.forEach(listener => listener(...args)),\n this\n }\n\n /**\n * @description Gets all event names with registered listeners.\n * @public\n * @returns {(keyof E)[]} Array of event names.\n */\n public eventNames(): (keyof E)[] {\n return Array.from(this.#events.keys());\n }\n\n /**\n * @description Checks if the event is paused.\n * @public\n * @param {keyof E} event The event name of key.\n * @returns {boolean} Whether the event is paused.\n */\n public isPaused(event: keyof E): boolean {\n return this.#pausedEvents.has(event);\n }\n\n /**\n * @description Gets the listeners for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {(ListenersFor<Event, E, T, R> | undefined)} The listeners for the event.\n */\n public listeners<Event extends keyof E>(event: Event): ListenersFor<Event, E, T, R> | undefined {\n return this.#events.get(event) as any;\n }\n\n /**\n * @description Emits an event asynchronously, calling all listeners for that event type.\n * @public\n * @async\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {...Parameters<E[Event]>} args The arguments for the event listeners.\n * @returns {this} The current instance.\n */\n public async emitAsync<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): Promise<this> {\n const listeners = this.listeners(event);\n if (!listeners || this.isPaused(event)) {\n return this;\n }\n const snapshot = await listeners.snapshot();\n await Promise.all(snapshot.map(listener => Promise.resolve(listener(...args))));\n return this;\n }\n\n /**\n * @description Adds a listener for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {E[Event]} listener The listener function.\n * @returns {this} The current instance.\n */\n public on<Event extends keyof E>(event: Event, listener: E[Event]): this {\n return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapter)),\n this.#events.get(event)!.add(listener),\n this;\n }\n\n /**\n * @description Adds a listener for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {E[Event]} listener The listener function.\n * @returns {this} The current instance.\n */\n public once<Event extends keyof E>(event: Event, listener: E[Event]): this {\n return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapter)),\n this.#events.get(event)!.once(listener),\n this;\n }\n\n /**\n * @description Removes a listener for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {E[Event]} listener The listener function.\n * @returns {this} The current instance.\n */\n public off<Event extends keyof E>(event: Event, listener: E[Event]): this {\n return this.#events.get(event)?.delete(listener), this;\n }\n\n /**\n * @description Pauses the event, preventing it from being emitted.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {this} The current instance.\n */\n public pause<Event extends keyof E>(event: Event): this {\n return this.#pausedEvents.add(event), this;\n }\n\n /**\n * @description Resumes the event if it was paused.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {this} The current instance.\n */\n public resume<Event extends keyof E>(event: Event): this {\n return this.#pausedEvents.delete(event), this;\n }\n}\n","import { ListenerFunction } from \"@typedly/listeners\";\nimport { SetAdapter } from \"@typescript-package/collection-adapter\";\n\nexport class ListenersSetAdapter<L extends ListenerFunction<any[]>> extends SetAdapter<L> {\n public override version: string = '1.0.0';\n public once(...listeners: L[]): this {\n listeners.forEach(listener => {\n if (this.has(listener)) {\n throw new Error('Listener already exists in the collection.');\n }\n\n const onceListener = (...args: any[]) => (\n this.delete(onceListener as L),\n listener(...args)\n );\n\n this.add(onceListener as L)\n\n });\n return this;\n }\n\n public snapshot(): L[] {\n return Array.from(this.value);\n }\n}","// Abstract.\nimport { EventEmitterBase } from \"./event-emitter.base\";\n// Class.\nimport { ListenersSetAdapter } from \"../adapter\";\n// Type & Interface.\nimport { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\n/**\n * @description A concrete class that implements an event emitter pattern.\n * @export\n * @class EventEmitter\n * @template {ListenerFunction<any[]>} E The object type of events.\n * @template [T=any] The type of the listeners underlying data.\n * @template {boolean} [R=false] The async flag for the listeners.\n * @template {ListenersAdapter<Parameters<E>, E, any, R >} [A=R extends false ? ListenersSetAdapter<E> : any] The adapter type for the listeners.\n * @extends {EventEmitterBase<E, any, R, A>}Base class abstracting common event emitter functionality.\n */\nexport class EventEmitter<\n E extends ListenerFunction<any[]>,\n T = any,\n R extends boolean = false,\n A extends ListenersAdapter<Parameters<E>, E, T, R > = R extends false ? ListenersSetAdapter<E> : any\n> extends EventEmitterBase<E, T, R, A> {\n /**\n * Creates an instance of `EventEmitter`.\n * @constructor\n * @param {R} async \n * @param {?(E | E[])} [events] \n * @param {new (...listeners: E[]) => A} [adapter=ListenersSetAdapter as any] \n */\n \n /**\n * Creates an instance of `EventEmitter`.\n * @constructor\n * @param {{adapter?: new (...listeners: E[]) => A, async?: R, value?: T}} param0 \n * @param {new (...listeners: {}) => A} param0.adapter The adapter class to manage listeners.\n * @param {R} param0.async Whether the emitter listeners operate asynchronously.\n * @param {T} param0.value The underlying data for the listeners for capture its type only.\n * @param {?(E | E[])} [events] The initial listeners.\n */\n constructor(\n {adapter, async, value}: {adapter?: new (...listeners: E[]) => A, async?: R, value?: T},\n events?: E | E[],\n ) {\n super({async, value}, adapter ?? ListenersSetAdapter as any, events);\n }\n}\n","// Abstract.\nimport { NamedEventEmitterBase } from \"./named-event-emitter.base\";\n// Class.\nimport { ListenersSetAdapter } from \"../adapter\";\n// Type & Interface.\nimport { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\n/**\n * @description A concrete class that implements a named event emitter pattern.\n * @export\n * @class NamedEventEmitter\n * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.\n * @template {boolean} [R=false] The `boolean` type of the async flag for the listeners.\n * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, R>} [A=R extends false ? ListenersSetAdapter<E[keyof E]> : any] \n * @extends {NamedEventEmitterBase<E, any, R, A>}\n */\nexport class NamedEventEmitter<\n E extends Record<string, ListenerFunction<any[]>>,\n T = any,\n R extends boolean = false,\n A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = R extends false ? ListenersSetAdapter<E[keyof E]> : any\n> extends NamedEventEmitterBase<E, T, R, A> {\n /**\n * Creates an instance of `NamedEventEmitter`.\n * @constructor\n * @param {R} async Whether the emitter listeners operate asynchronously.\n * @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial listeners.\n * @param {new (...listeners: E[keyof E][]) => A} [adapter=ListenersSetAdapter as any] The adapter class to manage listeners.\n */\n constructor(\n {adapter, async, value}: {adapter?: new (...listeners: E[keyof E][]) => A, async?: R, value?: T},\n events?: Partial<{ [K in keyof E]: E[K][] }>,\n ) {\n super({async}, adapter ?? ListenersSetAdapter as any, events);\n }\n}\n","// Abstract.\nexport { EventEmitterBase } from './event-emitter.base';\nexport { NamedEventEmitterBase } from './named-event-emitter.base';\n// Concrete.\nexport { EventEmitter } from './event-emitter.class';\nexport { NamedEventEmitter } from './named-event-emitter.class';\n","/*\n * Public API Surface of event-emitter\n */\n\nexport {\n // Abstract.\n EventEmitterBase,\n NamedEventEmitterBase,\n // Concrete.\n EventEmitter,\n NamedEventEmitter\n} from './lib';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;AAAA;AAIA;;;;;;;;;AASG;MACmB,gBAAgB,CAAA;AAMpC;;;AAGG;AACH,IAAA,QAAQ;AAER;;;AAGG;AACH,IAAA,MAAM;AAEN;;;AAGG;AACH,IAAA,UAAU;AAEV;;;AAGG;IACH,OAAO,GAAY,KAAK;AAExB;;;;;;;;AAQG;IACH,WAAA,CACE,EAAC,KAAK,EAAE,KAAK,EAAyB,EACtC,OAAqC,EACrC,MAAgB,EAAA;AAEhB,QAAA,IAAI,CAAC,UAAU,GAAG,IAAI,SAAS,CAAC,KAAK,IAAI,KAAK,EAAE,OAAO,EAAE,IAAI,KAAK,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC;AACtH,QAAA,IAAI,CAAC,QAAQ,GAAG,OAAO;AACvB,QAAA,IAAI,CAAC,MAAM,GAAG,KAAK,IAAI,KAAU;IACnC;AAEA;;;;AAIG;IACI,KAAK,GAAA;QACV,OAAO,IAAI,CAAC,UAAU,EAAE,KAAK,EAAE,EAAE,IAAI;IACvC;AAEA;;;;AAIG;IACI,KAAK,GAAA;AACV,QAAA,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI;IAC7B;AAEA;;;;AAIG;IACI,IAAI,CAAC,GAAG,IA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;;;;"}
|
|
1
|
+
{"version":3,"file":"typescript-package-event-emitter.mjs","sources":["../../../package/event-emitter/src/adapter/lib/listeners-set.adapter.ts","../../../package/event-emitter/src/lib/event-emitter.base.ts","../../../package/event-emitter/src/lib/named-event-emitter.base.ts","../../../package/event-emitter/src/lib/event-emitter.class.ts","../../../package/event-emitter/src/lib/named-event-emitter.class.ts","../../../package/event-emitter/src/lib/index.ts","../../../package/event-emitter/src/public-api.ts","../../../package/event-emitter/src/typescript-package-event-emitter.ts"],"sourcesContent":["import { ListenerFunction, ListenersAdapter } from \"@typedly/listeners\";\nimport { SetAdapter } from \"@typescript-package/collection-adapter\";\n\nexport class ListenersSetAdapter<\n L extends ListenerFunction<any[]>,\n T extends Set<L> = Set<L>\n> extends SetAdapter<L, T>\n implements ListenersAdapter<Parameters<L>, L, T, false> {\n public override version: string = '1.0.0';\n public once(...listeners: L[]): this {\n listeners.forEach(listener => {\n if (super.has(listener)) {\n throw new Error('Listener already exists in the collection.');\n }\n\n const onceListener = (...args: any[]) => (\n super.delete(onceListener as L),\n listener(...args)\n );\n\n super.add(onceListener as L);\n\n });\n return this;\n }\n\n public snapshot(): L[] {\n return Array.from(this.value);\n }\n}","// Class.\nimport { Listeners } from \"@typescript-package/listeners\";\n// Type & Interface.\nimport type { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\n/**\n * @description The base abstraction class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.\n * @export\n * @abstract\n * @class EventEmitterBase\n * @template {ListenerFunction<any[]>} L The listener function type.\n * @template [T=any] The type of the listeners underlying data.\n * @template {boolean} [R=false] The async flag for the listeners.\n * @template {ListenersAdapter<Parameters<L>, L, T, R>} [A=ListenersAdapter<Parameters<L>, L, T, R>] The adapter type for the listeners.\n */\nexport abstract class EventEmitterBase<\n L extends ListenerFunction<any[]>,\n T = any,\n R extends boolean = false,\n A extends ListenersAdapter<Parameters<L>, L, T, R> = ListenersAdapter<Parameters<L>, L, T, R>\n> {\n /**\n * @description Gets the async flag for the listeners.\n * @public\n * @readonly\n * @type {R}\n */\n public get async(): R {\n return this.#listeners.async;\n }\n\n /**\n * @description Gets the listeners for a specific event type.\n * @public\n * @readonly\n * @type {Listeners<A, L, Parameters<L>, T, R>} The listeners for the event.\n */\n public get listeners(): Listeners<A, L, Parameters<L>, T, R> {\n return this.#listeners;\n }\n\n /**\n * @description Gets the adapter constructor.\n * @protected\n * @returns {new (...listeners: {}) => A} The adapter constructor.\n */\n protected adapterCtor() {\n return this.#adapterCtor;\n }\n\n /**\n * @description The adapter class used to manage listeners.\n * @type {new (...listeners: L[]) => A}\n */\n #adapterCtor: new (...listeners: L[]) => A;\n\n /**\n * @description The map of events to their listeners.\n * @type {Listeners<A, L, Parameters<L>, T, R>}\n */\n #listeners: Listeners<A, L, Parameters<L>, T, R>;\n\n /**\n * @description The paused state of the event emitter.\n * @type {boolean}\n */\n #paused: boolean = false;\n\n /**\n * Creates an instance of `EventEmitterBase`.\n * @constructor\n * @param {R} async Whether the emitter listeners operate asynchronously.\n * @param {new (...listeners: L[]) => A} adapter The adapter class to manage listeners.\n * @param {?(L | L[])} [listeners] The initial listeners.\n */\n constructor(\n async: R,\n adapter: new (...listeners: L[]) => A,\n listeners?: L | L[]\n ) {\n this.#listeners = new Listeners(\n async,\n adapter,\n ...(Array.isArray(listeners) ? listeners : listeners ? [listeners] : [])\n );\n this.#adapterCtor = adapter;\n }\n\n /**\n * @description Removes all listeners for a specific event type.\n * @public\n * @returns {this} The current instance.\n */\n public clear(): this {\n return this.#listeners?.clear(), this;\n }\n\n /**\n * @description Gets the number of listeners for a specific event.\n * @public\n * @returns {number} The number of listeners for the event.\n */\n public count(): number {\n return this.#listeners.size;\n }\n\n /**\n * @description Emits an event, calling all listeners for that event type.\n * @public\n * @param {...Parameters<L>} args The arguments for the event listeners.\n */\n public emit(...args: Parameters<L>): this {\n return !this.isPaused() && this.#listeners.forEach(listener => listener(...args)),\n this\n }\n\n /**\n * @description Checks if the event is paused.\n * @public\n * @returns {boolean} Whether the event is paused.\n */\n public isPaused(): boolean {\n return this.#paused === true;\n }\n\n /**\n * @description Emits an event asynchronously, calling all listeners for that event type.\n * @public\n * @async\n * @param {...Parameters<L>} args The arguments for the event listeners.\n * @returns {this} The current instance.\n */\n public async emitAsync(...args: Parameters<L>): Promise<this> {\n const listeners = this.listeners;\n if (!listeners || this.isPaused()) {\n return this;\n }\n const snapshot = await listeners.snapshot();\n await Promise.all(snapshot.map(listener => Promise.resolve(listener(...args))));\n return this;\n }\n\n /**\n * @description Adds a listener for event.\n * @public\n * @param {L} listener The listener function.\n * @returns {this} The current instance.\n */\n public on(listener: L): this {\n return this.#listeners.add(listener),\n this;\n }\n\n /**\n * @description Adds a one-time listener for event.\n * @public\n * @param {L} listener The listener function.\n * @returns {this} The current instance.\n */\n public once(listener: L): this {\n return this.#listeners.once(listener),\n this;\n }\n\n /**\n * @description Removes a listener for event.\n * @public\n * @param {L} listener The listener function.\n * @returns {this} The current instance.\n */\n public off(listener: L): this {\n return this.#listeners.delete(listener), this;\n }\n\n /**\n * @description Pauses the event, preventing it from being emitted.\n * @public\n * @returns {this} The current instance.\n */\n public pause(): this {\n return this.#paused = true, this;\n }\n\n /**\n * @description Resumes the event if it was paused.\n * @public\n * @returns {this} The current instance.\n */\n public resume(): this {\n return this.#paused = false, this;\n }\n}\n","// Class.\nimport { Listeners } from \"@typescript-package/listeners\";\n// Type & Interface.\nimport type { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\nimport type { EventListeners, ListenersFor } from \"../type\";\n/**\n * @description A base abstraction class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.\n * @export\n * @abstract\n * @class NamedEventEmitterBase\n * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.\n * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>] The adapter type for the listeners.\n * @template [T=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.\n * @template {boolean} [R=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false] The async flag for the listeners, inferred from the adapter if possible.\n */\nexport abstract class NamedEventEmitterBase<\n E extends Record<string, ListenerFunction<any[]>>,\n A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>,\n T = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any,\n R extends boolean = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false\n> {\n /**\n * @description The adapter class used to manage listeners.\n * @type {new (...listeners: E[keyof E][]) => A}\n */\n #adapterCtor: new (...listeners: E[keyof E][]) => A;\n\n /**\n * @description The async flag for the listeners.\n * @type {R}\n */\n #async: R;\n\n /**\n * @description The map of events to their listeners.\n * @type {Map<keyof E, Listeners<A, E[keyof E], Parameters<E[keyof E]>, T, R>>}\n */\n #events: Map<keyof E, Listeners<A, E[keyof E], Parameters<E[keyof E]>, T, R>> = new Map();\n\n /**\n * @description The set of names of paused events.\n * @type {Set<keyof E>}\n */\n #pausedEvents: Set<keyof E> = new Set();\n \n /**\n * Creates an instance of `NamedEventEmitterBase`.\n * @constructor\n * @param {R} async Whether the emitter listeners operate asynchronously.\n * @param {new (...listeners: E[keyof E][]) => A} adapter The adapter class to manage listeners.\n * @param {?EventListeners<E>} [events] The initial events and their listeners.\n */\n constructor(\n async: R,\n adapter: new (...listeners: E[keyof E][]) => A,\n events?: EventListeners<E>\n ) {\n this.#adapterCtor = adapter;\n this.#async = async;\n for (const [event, listeners] of Object.entries(events ?? {})) {\n this.#events.set(event as keyof E, new Listeners(async, adapter));\n for (const listener of listeners!) {\n this.#events.get(event as keyof E)!.add(listener);\n }\n }\n }\n\n /**\n * @description Removes all listeners for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {this} The current instance.\n */\n public clear<Event extends keyof E>(event: Event): this {\n return this.#events.get(event)?.clear(), this;\n }\n\n /**\n * @description Removes all listeners for all event types.\n * @public\n * @returns {this} The current instance.\n */\n public clearAll(): this {\n return this.#events.clear(), this;\n }\n\n /**\n * @description Gets the number of listeners for a specific event.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {number} The number of listeners for the event.\n */\n public count<Event extends keyof E>(event: Event): number {\n return this.#events.get(event)?.size ?? 0;\n }\n\n /**\n * @description Emits an event, calling all listeners for that event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {...Parameters<E[Event]>} args The arguments for the event listeners.\n */\n public emit<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): this {\n return !this.isPaused(event) && this.#events.get(event)?.forEach(listener => listener(...args)),\n this\n }\n\n /**\n * @description Gets all event names with registered listeners.\n * @public\n * @returns {(keyof E)[]} Array of event names.\n */\n public eventNames(): (keyof E)[] {\n return Array.from(this.#events.keys());\n }\n\n /**\n * @description Checks if the event is paused.\n * @public\n * @param {keyof E} event The event name of key.\n * @returns {boolean} Whether the event is paused.\n */\n public isPaused(event: keyof E): boolean {\n return this.#pausedEvents.has(event);\n }\n\n /**\n * @description Gets the listeners for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {(ListenersFor<Event, E, T, R> | undefined)} The listeners for the event.\n */\n public listeners<Event extends keyof E>(event: Event): ListenersFor<Event, E, T, R> | undefined {\n return this.#events.get(event) as any;\n }\n\n /**\n * @description Emits an event asynchronously, calling all listeners for that event type.\n * @public\n * @async\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {...Parameters<E[Event]>} args The arguments for the event listeners.\n * @returns {this} The current instance.\n */\n public async emitAsync<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): Promise<this> {\n const listeners = this.listeners(event);\n if (!listeners || this.isPaused(event)) {\n return this;\n }\n const snapshot = await listeners.snapshot();\n await Promise.all(snapshot.map(listener => Promise.resolve(listener(...args))));\n return this;\n }\n\n /**\n * @description Adds a listener for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {E[Event]} listener The listener function.\n * @returns {this} The current instance.\n */\n public on<Event extends keyof E>(event: Event, listener: E[Event]): this {\n return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapterCtor)),\n this.#events.get(event)!.add(listener),\n this;\n }\n\n /**\n * @description Adds a listener for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {E[Event]} listener The listener function.\n * @returns {this} The current instance.\n */\n public once<Event extends keyof E>(event: Event, listener: E[Event]): this {\n return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapterCtor)),\n this.#events.get(event)!.once(listener),\n this;\n }\n\n /**\n * @description Removes a listener for a specific event type.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @param {E[Event]} listener The listener function.\n * @returns {this} The current instance.\n */\n public off<Event extends keyof E>(event: Event, listener: E[Event]): this {\n return this.#events.get(event)?.delete(listener), this;\n }\n\n /**\n * @description Pauses the event, preventing it from being emitted.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {this} The current instance.\n */\n public pause<Event extends keyof E>(event: Event): this {\n return this.#pausedEvents.add(event), this;\n }\n\n /**\n * @description Resumes the event if it was paused.\n * @public\n * @template {keyof E} Event The event key.\n * @param {Event} event The event name of key.\n * @returns {this} The current instance.\n */\n public resume<Event extends keyof E>(event: Event): this {\n return this.#pausedEvents.delete(event), this;\n }\n}\n","// Abstract.\nimport { EventEmitterBase } from \"./event-emitter.base\";\n// Class.\nimport { ListenersSetAdapter } from \"../adapter\";\n// Type & Interface.\nimport { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\n/**\n * @description A concrete class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.\n * @export\n * @class EventEmitter\n * @template {ListenerFunction<any[]>} L The listener function type.\n * @template {ListenersAdapter<Parameters<L>, L, T, any>} [A=ListenersAdapter<Parameters<L>, L, any, any>] The adapter type for the listeners.\n * @template [T=A extends ListenersAdapter<Parameters<L>, L, infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.\n * @template {boolean} [R=false] The async flag for the listeners.\n * @extends {EventEmitterBase<L, T, R, A>}\n */\nexport class EventEmitter<\n L extends ListenerFunction<any[]>,\n A extends ListenersAdapter<Parameters<L>, L, T, any> = ListenersAdapter<Parameters<L>, L, any, any>,\n T = A extends ListenersAdapter<Parameters<L>, L, infer U, any> ? U : any,\n R extends boolean = false,\n> extends EventEmitterBase<L, T, R, A> {\n /**\n * Creates an instance of `EventEmitter`.\n * @constructor\n * @param {{adapter?: new (...listeners: L[]) => A, async?: R}} param0 The options for the event emitter.\n * @param {new (...listeners: L[]) => A} param0.adapter The adapter class to manage listeners.\n * @param {R} param0.async Whether the emitter listeners operate asynchronously.\n * @param {?(L | L[])} [listeners] The initial listeners.\n */\n constructor(\n {adapter, async}: {adapter?: new (...listeners: L[]) => A, async?: R},\n listeners?: L | L[],\n ) {\n super(async ?? false as R, adapter ?? ListenersSetAdapter as any, listeners);\n }\n}\n","// Abstract.\nimport { NamedEventEmitterBase } from \"./named-event-emitter.base\";\n// Class.\nimport { ListenersSetAdapter } from \"../adapter\";\n// Type & Interface.\nimport { ListenersAdapter, ListenerFunction } from \"@typedly/listeners\";\n/**\n * @description A concrete class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.\n * @export\n * @class NamedEventEmitter\n * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.\n * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>] The adapter type for the listeners.\n * @template [T=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.\n * @template {boolean} [R=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false] The async flag for the listeners, inferred from the adapter if possible.\n * @extends {NamedEventEmitterBase<E, A, T, R>} The base class for the named event emitter.\n */\nexport class NamedEventEmitter<\n E extends Record<string, ListenerFunction<any[]>>,\n A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>,\n T = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any,\n R extends boolean = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false\n> extends NamedEventEmitterBase<E, A, T, R> {\n /**\n * Creates an instance of `NamedEventEmitter`.\n * @constructor\n * @param {R} async Whether the emitter listeners operate asynchronously.\n * @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial listeners.\n * @param {new (...listeners: E[keyof E][]) => A} [adapter=ListenersSetAdapter as any] The adapter class to manage listeners.\n */\n constructor(\n {adapter, async}: {adapter?: new (...listeners: E[keyof E][]) => A, async?: R},\n events?: Partial<{ [K in keyof E]: E[K][] }>,\n ) {\n super(async ?? false as R, adapter ?? ListenersSetAdapter as any, events);\n }\n}\n","// Abstract.\nexport { EventEmitterBase } from './event-emitter.base';\nexport { NamedEventEmitterBase } from './named-event-emitter.base';\n// Concrete.\nexport { EventEmitter } from './event-emitter.class';\nexport { NamedEventEmitter } from './named-event-emitter.class';\n","/*\n * Public API Surface of event-emitter\n */\n\nexport {\n ListenersSetAdapter\n} from './adapter';\n\nexport {\n // Abstract.\n EventEmitterBase,\n NamedEventEmitterBase,\n // Concrete.\n EventEmitter,\n NamedEventEmitter\n} from './lib';\n\nexport type {\n EventListeners,\n ListenersFor\n} from './type';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;AAGM,MAAO,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,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,IAAI;IAC5C;AAEA;;;;;;AAMG;AACI,IAAA,MAAM,CAAwB,KAAY,EAAA;QAC/C,OAAO,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,IAAI;IAC/C;AACD;;AC5ND;AAMA;;;;;;;;;AASG;AACG,MAAO,YAKX,SAAQ,gBAA4B,CAAA;AACpC;;;;;;;AAOG;AACH,IAAA,WAAA,CACE,EAAC,OAAO,EAAE,KAAK,EAAsD,EACrE,SAAmB,EAAA;QAEnB,KAAK,CAAC,KAAK,IAAI,KAAU,EAAE,OAAO,IAAI,mBAA0B,EAAE,SAAS,CAAC;IAC9E;AACD;;ACpCD;AAMA;;;;;;;;;AASG;AACG,MAAO,iBAKX,SAAQ,qBAAiC,CAAA;AACzC;;;;;;AAMG;AACH,IAAA,WAAA,CACE,EAAC,OAAO,EAAE,KAAK,EAA+D,EAC9E,MAA4C,EAAA;QAE5C,KAAK,CAAC,KAAK,IAAI,KAAU,EAAE,OAAO,IAAI,mBAA0B,EAAE,MAAM,CAAC;IAC3E;AACD;;ACnCD;;ACAA;;AAEG;;ACFH;;AAEG;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typescript-package/event-emitter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"author": "wwwdev.io <dev@wwwdev.io>",
|
|
5
5
|
"description": "A lightweight TypeScript package for event emitter.",
|
|
6
6
|
"license": "MIT",
|
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"registry": "https://registry.npmjs.org"
|
|
10
10
|
},
|
|
11
11
|
"peerDependencies": {
|
|
12
|
-
"@typescript-package/collection-adapter": "^0.
|
|
13
|
-
"@typescript-package/listeners": "^0.0
|
|
12
|
+
"@typescript-package/collection-adapter": "^0.3.0",
|
|
13
|
+
"@typescript-package/listeners": "^1.0.0"
|
|
14
14
|
},
|
|
15
15
|
"repository": {
|
|
16
16
|
"type": "git",
|
|
@@ -30,6 +30,26 @@
|
|
|
30
30
|
"type": "stripe",
|
|
31
31
|
"url": "https://donate.stripe.com/dR614hfDZcJE3wAcMM"
|
|
32
32
|
},
|
|
33
|
+
{
|
|
34
|
+
"type": "Donorbox",
|
|
35
|
+
"url": "https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=once&amount=1"
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
"type": "Open Collective",
|
|
39
|
+
"url": "https://opencollective.com/sterblack"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"type": "Ko-fi",
|
|
43
|
+
"url": "https://ko-fi.com/sterblack"
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
"type": "4Fund",
|
|
47
|
+
"url": "https://4fund.com/bruubs"
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"type": "paypal",
|
|
51
|
+
"url": "https://paypal.me/sterblack"
|
|
52
|
+
},
|
|
33
53
|
{
|
|
34
54
|
"type": "individual",
|
|
35
55
|
"url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29"
|
|
@@ -1,32 +1,53 @@
|
|
|
1
|
-
import { Listeners } from '@typescript-package/listeners';
|
|
2
1
|
import { ListenerFunction, ListenersAdapter } from '@typedly/listeners';
|
|
3
2
|
import { SetAdapter } from '@typescript-package/collection-adapter';
|
|
3
|
+
import { Listeners } from '@typescript-package/listeners';
|
|
4
|
+
|
|
5
|
+
declare class ListenersSetAdapter<L extends ListenerFunction<any[]>, T extends Set<L> = Set<L>> extends SetAdapter<L, T> implements ListenersAdapter<Parameters<L>, L, T, false> {
|
|
6
|
+
version: string;
|
|
7
|
+
once(...listeners: L[]): this;
|
|
8
|
+
snapshot(): L[];
|
|
9
|
+
}
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
|
-
* @description The base abstraction class for an event emitter pattern.
|
|
12
|
+
* @description The base abstraction class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
7
13
|
* @export
|
|
8
14
|
* @abstract
|
|
9
15
|
* @class EventEmitterBase
|
|
10
|
-
* @template {ListenerFunction<any[]>}
|
|
16
|
+
* @template {ListenerFunction<any[]>} L The listener function type.
|
|
11
17
|
* @template [T=any] The type of the listeners underlying data.
|
|
12
18
|
* @template {boolean} [R=false] The async flag for the listeners.
|
|
13
|
-
* @template {ListenersAdapter<Parameters<
|
|
19
|
+
* @template {ListenersAdapter<Parameters<L>, L, T, R>} [A=ListenersAdapter<Parameters<L>, L, T, R>] The adapter type for the listeners.
|
|
14
20
|
*/
|
|
15
|
-
declare abstract class EventEmitterBase<
|
|
21
|
+
declare abstract class EventEmitterBase<L extends ListenerFunction<any[]>, T = any, R extends boolean = false, A extends ListenersAdapter<Parameters<L>, L, T, R> = ListenersAdapter<Parameters<L>, L, T, R>> {
|
|
16
22
|
#private;
|
|
23
|
+
/**
|
|
24
|
+
* @description Gets the async flag for the listeners.
|
|
25
|
+
* @public
|
|
26
|
+
* @readonly
|
|
27
|
+
* @type {R}
|
|
28
|
+
*/
|
|
29
|
+
get async(): R;
|
|
30
|
+
/**
|
|
31
|
+
* @description Gets the listeners for a specific event type.
|
|
32
|
+
* @public
|
|
33
|
+
* @readonly
|
|
34
|
+
* @type {Listeners<A, L, Parameters<L>, T, R>} The listeners for the event.
|
|
35
|
+
*/
|
|
36
|
+
get listeners(): Listeners<A, L, Parameters<L>, T, R>;
|
|
37
|
+
/**
|
|
38
|
+
* @description Gets the adapter constructor.
|
|
39
|
+
* @protected
|
|
40
|
+
* @returns {new (...listeners: {}) => A} The adapter constructor.
|
|
41
|
+
*/
|
|
42
|
+
protected adapterCtor(): new (...listeners: L[]) => A;
|
|
17
43
|
/**
|
|
18
44
|
* Creates an instance of `EventEmitterBase`.
|
|
19
45
|
* @constructor
|
|
20
|
-
* @param {
|
|
21
|
-
* @param {
|
|
22
|
-
* @param {
|
|
23
|
-
* @param {new (...listeners: E[]) => A} adapter The adapter class to manage listeners.
|
|
24
|
-
* @param {?(E | E[])} [events] The initial listeners.
|
|
46
|
+
* @param {R} async Whether the emitter listeners operate asynchronously.
|
|
47
|
+
* @param {new (...listeners: L[]) => A} adapter The adapter class to manage listeners.
|
|
48
|
+
* @param {?(L | L[])} [listeners] The initial listeners.
|
|
25
49
|
*/
|
|
26
|
-
constructor(
|
|
27
|
-
async?: R;
|
|
28
|
-
value?: T;
|
|
29
|
-
}, adapter: new (...listeners: E[]) => A, events?: E | E[]);
|
|
50
|
+
constructor(async: R, adapter: new (...listeners: L[]) => A, listeners?: L | L[]);
|
|
30
51
|
/**
|
|
31
52
|
* @description Removes all listeners for a specific event type.
|
|
32
53
|
* @public
|
|
@@ -42,50 +63,44 @@ declare abstract class EventEmitterBase<E extends ListenerFunction<any[]>, T = a
|
|
|
42
63
|
/**
|
|
43
64
|
* @description Emits an event, calling all listeners for that event type.
|
|
44
65
|
* @public
|
|
45
|
-
* @param {...Parameters<
|
|
66
|
+
* @param {...Parameters<L>} args The arguments for the event listeners.
|
|
46
67
|
*/
|
|
47
|
-
emit(...args: Parameters<
|
|
68
|
+
emit(...args: Parameters<L>): this;
|
|
48
69
|
/**
|
|
49
70
|
* @description Checks if the event is paused.
|
|
50
71
|
* @public
|
|
51
72
|
* @returns {boolean} Whether the event is paused.
|
|
52
73
|
*/
|
|
53
74
|
isPaused(): boolean;
|
|
54
|
-
/**
|
|
55
|
-
* @description Gets the listeners for a specific event type.
|
|
56
|
-
* @public
|
|
57
|
-
* @returns {(ListenersFor<Event, E, T, R> | undefined)} The listeners for the event.
|
|
58
|
-
*/
|
|
59
|
-
listeners(): Listeners<Parameters<E>, E, T, R, A>;
|
|
60
75
|
/**
|
|
61
76
|
* @description Emits an event asynchronously, calling all listeners for that event type.
|
|
62
77
|
* @public
|
|
63
78
|
* @async
|
|
64
|
-
* @param {...Parameters<
|
|
79
|
+
* @param {...Parameters<L>} args The arguments for the event listeners.
|
|
65
80
|
* @returns {this} The current instance.
|
|
66
81
|
*/
|
|
67
|
-
emitAsync(...args: Parameters<
|
|
82
|
+
emitAsync(...args: Parameters<L>): Promise<this>;
|
|
68
83
|
/**
|
|
69
84
|
* @description Adds a listener for event.
|
|
70
85
|
* @public
|
|
71
|
-
* @param {
|
|
86
|
+
* @param {L} listener The listener function.
|
|
72
87
|
* @returns {this} The current instance.
|
|
73
88
|
*/
|
|
74
|
-
on(listener:
|
|
89
|
+
on(listener: L): this;
|
|
75
90
|
/**
|
|
76
91
|
* @description Adds a one-time listener for event.
|
|
77
92
|
* @public
|
|
78
|
-
* @param {
|
|
93
|
+
* @param {L} listener The listener function.
|
|
79
94
|
* @returns {this} The current instance.
|
|
80
95
|
*/
|
|
81
|
-
once(listener:
|
|
96
|
+
once(listener: L): this;
|
|
82
97
|
/**
|
|
83
98
|
* @description Removes a listener for event.
|
|
84
99
|
* @public
|
|
85
|
-
* @param {
|
|
100
|
+
* @param {L} listener The listener function.
|
|
86
101
|
* @returns {this} The current instance.
|
|
87
102
|
*/
|
|
88
|
-
off(listener:
|
|
103
|
+
off(listener: L): this;
|
|
89
104
|
/**
|
|
90
105
|
* @description Pauses the event, preventing it from being emitted.
|
|
91
106
|
* @public
|
|
@@ -101,42 +116,44 @@ declare abstract class EventEmitterBase<E extends ListenerFunction<any[]>, T = a
|
|
|
101
116
|
}
|
|
102
117
|
|
|
103
118
|
/**
|
|
104
|
-
* @description
|
|
119
|
+
* @description A type representing a partial mapping of event names to their listener arrays.
|
|
120
|
+
* @export
|
|
121
|
+
* @template E
|
|
122
|
+
*/
|
|
123
|
+
type EventListeners<E> = Partial<{
|
|
124
|
+
[K in keyof E]: E[K][] | E[K];
|
|
125
|
+
}>;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @description A type representing the listeners for a specific event in a named event emitter, utilizing a specified listeners adapter and supporting asynchronous capabilities.
|
|
105
129
|
* @export
|
|
106
130
|
* @template {keyof E} Event
|
|
107
131
|
* @template {Record<string, ListenerFunction<any[]>>} E
|
|
108
132
|
* @template [T=any]
|
|
109
133
|
* @template {boolean} [R=false]
|
|
110
134
|
*/
|
|
111
|
-
type ListenersFor<Event extends keyof E, E extends Record<string, ListenerFunction<any[]>>, T = any, R extends boolean = false
|
|
135
|
+
type ListenersFor<Event extends keyof E, E extends Record<string, ListenerFunction<any[]>>, T = any, R extends boolean = false, A extends ListenersAdapter<Parameters<E[Event]>, E[Event], T, R> = ListenersAdapter<Parameters<E[Event]>, E[Event], T, R>> = Listeners<A, E[Event], Parameters<E[Event]>, T, R>;
|
|
112
136
|
|
|
113
137
|
/**
|
|
114
|
-
* @description A base abstraction class that implements a named event emitter pattern.
|
|
138
|
+
* @description A base abstraction class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
115
139
|
* @export
|
|
116
140
|
* @abstract
|
|
117
141
|
* @class NamedEventEmitterBase
|
|
118
142
|
* @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
|
|
119
|
-
* @template [T=
|
|
120
|
-
* @template
|
|
121
|
-
* @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E],
|
|
143
|
+
* @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>] The adapter type for the listeners.
|
|
144
|
+
* @template [T=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.
|
|
145
|
+
* @template {boolean} [R=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false] The async flag for the listeners, inferred from the adapter if possible.
|
|
122
146
|
*/
|
|
123
|
-
declare abstract class NamedEventEmitterBase<E extends Record<string, ListenerFunction<any[]>>, T = any, R extends boolean =
|
|
147
|
+
declare abstract class NamedEventEmitterBase<E extends Record<string, ListenerFunction<any[]>>, A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>, T = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any, R extends boolean = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false> {
|
|
124
148
|
#private;
|
|
125
149
|
/**
|
|
126
150
|
* Creates an instance of `NamedEventEmitterBase`.
|
|
127
151
|
* @constructor
|
|
128
|
-
* @param {
|
|
129
|
-
* @param {
|
|
130
|
-
* @param {
|
|
131
|
-
* @param {new (...listeners: E[keyof E][]) => A} adapter The adapter class to manage listeners.
|
|
132
|
-
* @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial events and their listeners.
|
|
152
|
+
* @param {R} async Whether the emitter listeners operate asynchronously.
|
|
153
|
+
* @param {new (...listeners: E[keyof E][]) => A} adapter The adapter class to manage listeners.
|
|
154
|
+
* @param {?EventListeners<E>} [events] The initial events and their listeners.
|
|
133
155
|
*/
|
|
134
|
-
constructor(
|
|
135
|
-
async?: R;
|
|
136
|
-
value?: T;
|
|
137
|
-
}, adapter: new (...listeners: E[keyof E][]) => A, events?: Partial<{
|
|
138
|
-
[K in keyof E]: E[K][];
|
|
139
|
-
}>);
|
|
156
|
+
constructor(async: R, adapter: new (...listeners: E[keyof E][]) => A, events?: EventListeners<E>);
|
|
140
157
|
/**
|
|
141
158
|
* @description Removes all listeners for a specific event type.
|
|
142
159
|
* @public
|
|
@@ -243,56 +260,42 @@ declare abstract class NamedEventEmitterBase<E extends Record<string, ListenerFu
|
|
|
243
260
|
resume<Event extends keyof E>(event: Event): this;
|
|
244
261
|
}
|
|
245
262
|
|
|
246
|
-
declare class ListenersSetAdapter<L extends ListenerFunction<any[]>> extends SetAdapter<L> {
|
|
247
|
-
version: string;
|
|
248
|
-
once(...listeners: L[]): this;
|
|
249
|
-
snapshot(): L[];
|
|
250
|
-
}
|
|
251
|
-
|
|
252
263
|
/**
|
|
253
|
-
* @description A concrete class
|
|
264
|
+
* @description A concrete class for an event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
254
265
|
* @export
|
|
255
266
|
* @class EventEmitter
|
|
256
|
-
* @template {ListenerFunction<any[]>}
|
|
257
|
-
* @template [
|
|
267
|
+
* @template {ListenerFunction<any[]>} L The listener function type.
|
|
268
|
+
* @template {ListenersAdapter<Parameters<L>, L, T, any>} [A=ListenersAdapter<Parameters<L>, L, any, any>] The adapter type for the listeners.
|
|
269
|
+
* @template [T=A extends ListenersAdapter<Parameters<L>, L, infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.
|
|
258
270
|
* @template {boolean} [R=false] The async flag for the listeners.
|
|
259
|
-
* @
|
|
260
|
-
* @extends {EventEmitterBase<E, any, R, A>}Base class abstracting common event emitter functionality.
|
|
271
|
+
* @extends {EventEmitterBase<L, T, R, A>}
|
|
261
272
|
*/
|
|
262
|
-
declare class EventEmitter<
|
|
263
|
-
/**
|
|
264
|
-
* Creates an instance of `EventEmitter`.
|
|
265
|
-
* @constructor
|
|
266
|
-
* @param {R} async
|
|
267
|
-
* @param {?(E | E[])} [events]
|
|
268
|
-
* @param {new (...listeners: E[]) => A} [adapter=ListenersSetAdapter as any]
|
|
269
|
-
*/
|
|
273
|
+
declare class EventEmitter<L extends ListenerFunction<any[]>, A extends ListenersAdapter<Parameters<L>, L, T, any> = ListenersAdapter<Parameters<L>, L, any, any>, T = A extends ListenersAdapter<Parameters<L>, L, infer U, any> ? U : any, R extends boolean = false> extends EventEmitterBase<L, T, R, A> {
|
|
270
274
|
/**
|
|
271
275
|
* Creates an instance of `EventEmitter`.
|
|
272
276
|
* @constructor
|
|
273
|
-
* @param {{adapter?: new (...listeners:
|
|
274
|
-
* @param {new (...listeners:
|
|
277
|
+
* @param {{adapter?: new (...listeners: L[]) => A, async?: R}} param0 The options for the event emitter.
|
|
278
|
+
* @param {new (...listeners: L[]) => A} param0.adapter The adapter class to manage listeners.
|
|
275
279
|
* @param {R} param0.async Whether the emitter listeners operate asynchronously.
|
|
276
|
-
* @param {
|
|
277
|
-
* @param {?(E | E[])} [events] The initial listeners.
|
|
280
|
+
* @param {?(L | L[])} [listeners] The initial listeners.
|
|
278
281
|
*/
|
|
279
|
-
constructor({ adapter, async
|
|
280
|
-
adapter?: new (...listeners:
|
|
282
|
+
constructor({ adapter, async }: {
|
|
283
|
+
adapter?: new (...listeners: L[]) => A;
|
|
281
284
|
async?: R;
|
|
282
|
-
|
|
283
|
-
}, events?: E | E[]);
|
|
285
|
+
}, listeners?: L | L[]);
|
|
284
286
|
}
|
|
285
287
|
|
|
286
288
|
/**
|
|
287
|
-
* @description A concrete class that implements a named event emitter pattern.
|
|
289
|
+
* @description A concrete class that implements a named event emitter pattern with replaceable listeners adapter and asynchronous capabilities.
|
|
288
290
|
* @export
|
|
289
291
|
* @class NamedEventEmitter
|
|
290
292
|
* @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
|
|
291
|
-
* @template {
|
|
292
|
-
* @template
|
|
293
|
-
* @extends
|
|
293
|
+
* @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>] The adapter type for the listeners.
|
|
294
|
+
* @template [T=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any] The type of the listeners underlying data, inferred from the adapter if possible.
|
|
295
|
+
* @template {boolean} [R=A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false] The async flag for the listeners, inferred from the adapter if possible.
|
|
296
|
+
* @extends {NamedEventEmitterBase<E, A, T, R>} The base class for the named event emitter.
|
|
294
297
|
*/
|
|
295
|
-
declare class NamedEventEmitter<E extends Record<string, ListenerFunction<any[]>>, T = any,
|
|
298
|
+
declare class NamedEventEmitter<E extends Record<string, ListenerFunction<any[]>>, A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, any>, T = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], infer U, any> ? U : any, R extends boolean = A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, infer V> ? V : false> extends NamedEventEmitterBase<E, A, T, R> {
|
|
296
299
|
/**
|
|
297
300
|
* Creates an instance of `NamedEventEmitter`.
|
|
298
301
|
* @constructor
|
|
@@ -300,13 +303,13 @@ declare class NamedEventEmitter<E extends Record<string, ListenerFunction<any[]>
|
|
|
300
303
|
* @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial listeners.
|
|
301
304
|
* @param {new (...listeners: E[keyof E][]) => A} [adapter=ListenersSetAdapter as any] The adapter class to manage listeners.
|
|
302
305
|
*/
|
|
303
|
-
constructor({ adapter, async
|
|
306
|
+
constructor({ adapter, async }: {
|
|
304
307
|
adapter?: new (...listeners: E[keyof E][]) => A;
|
|
305
308
|
async?: R;
|
|
306
|
-
value?: T;
|
|
307
309
|
}, events?: Partial<{
|
|
308
310
|
[K in keyof E]: E[K][];
|
|
309
311
|
}>);
|
|
310
312
|
}
|
|
311
313
|
|
|
312
|
-
export { EventEmitter, EventEmitterBase, NamedEventEmitter, NamedEventEmitterBase };
|
|
314
|
+
export { EventEmitter, EventEmitterBase, ListenersSetAdapter, NamedEventEmitter, NamedEventEmitterBase };
|
|
315
|
+
export type { EventListeners, ListenersFor };
|