@typescript-package/event-emitter 0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 typescript-package
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,220 @@
1
+
2
+ <a href="https://www.typescriptlang.org/">
3
+ <img
4
+ src="https://raw.githubusercontent.com/typescript-package/core/refs/heads/main/ts-package-barcode-logo-512.png"
5
+ width="20%"
6
+ title="@typescript-package/event-emitter - A lightweight TypeScript package for event emitter."
7
+ />
8
+ </a>
9
+
10
+ ## @typescript-package/event-emitter
11
+
12
+ <!-- npm badge -->
13
+ [![npm version][typescript-package-npm-badge-svg]][typescript-package-npm-badge]
14
+ [![GitHub issues][typescript-package-badge-issues]][typescript-package-issues]
15
+ [![GitHub license][typescript-package-badge-license]][typescript-package-license]
16
+
17
+ A **lightweight** TypeScript package for event emitter.
18
+
19
+ ## Table of contents
20
+
21
+ - [Installation](#installation)
22
+ - [Api](#api)
23
+ - Abstract
24
+ - [`EventEmitterBase`](#eventemitterbase)
25
+ - [`NamedEventEmitterBase`](#namedeventemitterbase)
26
+ - Concrete
27
+ - [`EventEmitter`](#eventemitter)
28
+ - [`NamedEventEmitter`](#namedeventemitter)
29
+ - [Contributing](#contributing)
30
+ - [Code of Conduct](code-of-conduct)
31
+ - [Git](#git)
32
+ - [Commit](#commit)
33
+ - [Versioning](#versioning)
34
+ - [License](#license)
35
+
36
+ ## Installation
37
+
38
+ ```bash
39
+ npm install @typescript-package/event-emitter --save-peer
40
+ ```
41
+
42
+ ## Api
43
+
44
+ ```typescript
45
+ import {
46
+ // Abstract.
47
+ EventEmitterBase,
48
+ NamedEventEmitterBase,
49
+ // Concrete.
50
+ EventEmitter,
51
+ NamedEventEmitter,
52
+ } from '@typescript-package/event-emitter';
53
+ ```
54
+
55
+ ## Abstract
56
+
57
+ ### `EventEmitterBase`
58
+
59
+ The base abstraction class for an event emitter pattern.
60
+
61
+ ```typescript
62
+ import { EventEmitter } from '@typescript-package/event-emitter';
63
+ ```
64
+
65
+ [`EventEmitterBase`](https://github.com/typescript-package/hooks/blob/main/src/lib/event-emitter.base.ts)
66
+
67
+ ### `NamedEventEmitterBase`
68
+
69
+ A base abstraction class that implements a named event emitter pattern.
70
+
71
+ ```typescript
72
+ import { NamedEventEmitterBase } from '@typescript-package/event-emitter';
73
+ ```
74
+
75
+ [`NamedEventEmitterBase`](https://github.com/typescript-package/hooks/blob/main/src/lib/named-event-emitter.base.ts)
76
+
77
+ ## Concrete
78
+
79
+ ### `EventEmitter`
80
+
81
+ A concrete class that implements an event emitter pattern.
82
+
83
+ ```typescript
84
+ import { EventEmitter } from '@typescript-package/event-emitter';
85
+
86
+ const eventEmitter = new EventEmitter({
87
+ // adapter: ListenersSetAdapter,
88
+ async: false
89
+ });
90
+
91
+ eventEmitter.on((msg) => console.log(`Received: ${msg}`));
92
+ eventEmitter.emit('Hello, World!');
93
+
94
+ ```
95
+
96
+ [`EventEmitter`](https://github.com/typescript-package/hooks/blob/main/src/lib/event-emitter.class.ts)
97
+
98
+ ### `NamedEventEmitter`
99
+
100
+ A concrete class that implements a named event emitter pattern.
101
+
102
+ ```typescript
103
+ import { NamedEventEmitter } from '@typescript-package/event-emitter';
104
+
105
+ const eventEmitter = new NamedEventEmitter({async: false}, {
106
+ 'event1': [(msg: string) => {
107
+ console.log(`Listener 1: ${msg}`);
108
+ }],
109
+ 'event2': [(num: number) => {
110
+ console.log(`Event 2 received number: ${num}`);
111
+ }]
112
+ });
113
+
114
+ eventEmitter.clear('event1')
115
+ eventEmitter.on('event1', (msg: string) => {
116
+ console.log(`Listener 2: ${msg}`);
117
+ });
118
+
119
+ eventEmitter.on('event2', (num: number) => {
120
+ console.log(`Event 2 received number: ${num}`);
121
+ });
122
+
123
+ eventEmitter.emit('event1', 'Hello, World!');
124
+ eventEmitter.listeners('event1')?.forEach(listener => {
125
+ listener('Hello, World!');
126
+ });
127
+ ```
128
+
129
+ [`NamedEventEmitter`](https://github.com/typescript-package/hooks/blob/main/src/lib/named-event-emitter.class.ts)
130
+
131
+ ## Contributing
132
+
133
+ Your contributions are valued! If you'd like to contribute, please feel free to submit a pull request. Help is always appreciated.
134
+
135
+ ## Support
136
+
137
+ If you find this package useful and would like to support its and general development, you can contribute through one of the following payment methods. Your support helps maintain the packages and continue adding new.
138
+
139
+ Support via:
140
+
141
+ - [Stripe](https://donate.stripe.com/dR614hfDZcJE3wAcMM)
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)
144
+ - [DonorBox](https://donorbox.org/become-a-sponsor-to-the-angular-package?default_interval=o)
145
+ - [Patreon](https://www.patreon.com/checkout/angularpackage?rid=0&fan_landing=true&view_as=public)
146
+
147
+ or via Trust Wallet
148
+
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
+ - [BNB](https://link.trustwallet.com/send?coin=20000714&address=0xA0c22A2bc7E37C1d5992dFDFFeD5E6f9298E1b94)
154
+
155
+ Thanks for your support!
156
+
157
+ ## Code of Conduct
158
+
159
+ By participating in this project, you agree to follow **[Code of Conduct](https://www.contributor-covenant.org/version/2/1/code_of_conduct/)**.
160
+
161
+ ## GIT
162
+
163
+ ### Commit
164
+
165
+ Please follow the following commit message conventions:
166
+
167
+ - [AngularJS Git Commit Message Conventions][git-commit-angular]
168
+ - [Karma Git Commit Msg][git-commit-karma]
169
+ - [Conventional Commits][git-commit-conventional]
170
+
171
+ ### Versioning
172
+
173
+ The package follows [Semantic Versioning 2.0.0][git-semver] for all releases. The versioning format is:
174
+
175
+ **Given a version number MAJOR.MINOR.PATCH, increment the:**
176
+
177
+ - MAJOR version when you make incompatible API changes,
178
+ - MINOR version when you add functionality in a backwards-compatible manner, and
179
+ - PATCH version when you make backwards-compatible bug fixes.
180
+
181
+ Additional labels for pre-release and build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
182
+
183
+ **FAQ**
184
+ How should I deal with revisions in the 0.y.z initial development phase?
185
+
186
+ > The simplest thing to do is start your initial development release at 0.1.0 and then increment the minor version for each subsequent release.
187
+
188
+ How do I know when to release 1.0.0?
189
+
190
+ > If your software is being used in production, it should probably already be 1.0.0. If you have a stable API on which users have come to depend, you should be 1.0.0. If you’re worrying a lot about backwards compatibility, you should probably already be 1.0.0.
191
+
192
+ ## License
193
+
194
+ MIT © typescript-package ([license][typescript-package-license])
195
+
196
+ <!-- This package: typescript-package -->
197
+ <!-- GitHub: badges -->
198
+ [typescript-package-badge-issues]: https://img.shields.io/github/issues/typescript-package/event-emitter
199
+ [isscript-package-badge-forks]: https://img.shields.io/github/forks/typescript-package/event-emitter
200
+ [typescript-package-badge-stars]: https://img.shields.io/github/stars/typescript-package/event-emitter
201
+ [typescript-package-badge-license]: https://img.shields.io/github/license/typescript-package/event-emitter
202
+ <!-- GitHub: badges links -->
203
+ [typescript-package-issues]: https://github.com/typescript-package/event-emitter/issues
204
+ [typescript-package-forks]: https://github.com/typescript-package/event-emitter/network
205
+ [typescript-package-license]: https://github.com/typescript-package/event-emitter/blob/master/LICENSE
206
+ [typescript-package-stars]: https://github.com/typescript-package/event-emitter/stargazers
207
+ <!-- This package -->
208
+
209
+ <!-- Package: typescript-package -->
210
+ <!-- npm -->
211
+ [typescript-package-npm-badge-svg]: https://badge.fury.io/js/@typescript-package%2Fevent-emitter.svg
212
+ [typescript-package-npm-badge]: https://badge.fury.io/js/@typescript-package%2Fevent-emitter
213
+
214
+ <!-- GIT -->
215
+ [git-semver]: http://semver.org/
216
+
217
+ <!-- GIT: commit -->
218
+ [git-commit-angular]: https://gist.github.com/stephenparish/9941e89d80e2bc58a153
219
+ [git-commit-karma]: http://karma-runner.github.io/0.10/dev/git-commit-msg.html
220
+ [git-commit-conventional]: https://www.conventionalcommits.org/en/v1.0.0/
@@ -0,0 +1,433 @@
1
+ import { Listeners } from '@typescript-package/listeners';
2
+ import { SetAdapter } from '@typescript-package/collection-adapter';
3
+
4
+ // Class.
5
+ /**
6
+ * @description The base abstraction class for an event emitter pattern.
7
+ * @export
8
+ * @abstract
9
+ * @class EventEmitterBase
10
+ * @template {ListenerFunction<any[]>} E The listener function type.
11
+ * @template [T=any] The type of the listeners underlying data.
12
+ * @template {boolean} [R=false] The async flag for the listeners.
13
+ * @template {ListenersAdapter<Parameters<E>, E, T, R>} [A=any] The adapter type for the listeners.
14
+ */
15
+ class EventEmitterBase {
16
+ /**
17
+ * @description The adapter class used to manage listeners.
18
+ * @type {new (...listeners: E[]) => A}
19
+ */
20
+ #adapter;
21
+ /**
22
+ * @description Indicates whether the emitter listeners operate asynchronously.
23
+ * @type {R}
24
+ */
25
+ #async;
26
+ /**
27
+ * @description The map of events to their listeners.
28
+ * @type {Listeners<Parameters<E>, E, T, R, A>}
29
+ */
30
+ #listeners;
31
+ /**
32
+ * @description The paused state of the event emitter.
33
+ * @type {boolean}
34
+ */
35
+ #paused = false;
36
+ /**
37
+ * Creates an instance of `EventEmitterBase`.
38
+ * @constructor
39
+ * @param {{async?: R, value?: T}} param0 The object with configuration options.
40
+ * @param {R} param0.async Whether the emitter listeners operate asynchronously.
41
+ * @param {T} param0.value The underlying data for the listeners for capture its type only.
42
+ * @param {new (...listeners: E[]) => A} adapter The adapter class to manage listeners.
43
+ * @param {?(E | E[])} [events] The initial listeners.
44
+ */
45
+ constructor({ async, value }, adapter, events) {
46
+ this.#listeners = new Listeners(async ?? false, adapter, ...(Array.isArray(events) ? events : events ? [events] : []));
47
+ this.#adapter = adapter;
48
+ this.#async = async ?? false;
49
+ }
50
+ /**
51
+ * @description Removes all listeners for a specific event type.
52
+ * @public
53
+ * @returns {this} The current instance.
54
+ */
55
+ clear() {
56
+ return this.#listeners?.clear(), this;
57
+ }
58
+ /**
59
+ * @description Gets the number of listeners for a specific event.
60
+ * @public
61
+ * @returns {number} The number of listeners for the event.
62
+ */
63
+ count() {
64
+ return this.#listeners.size;
65
+ }
66
+ /**
67
+ * @description Emits an event, calling all listeners for that event type.
68
+ * @public
69
+ * @param {...Parameters<E>} args The arguments for the event listeners.
70
+ */
71
+ emit(...args) {
72
+ return !this.isPaused() && this.#listeners.forEach(listener => listener(...args)),
73
+ this;
74
+ }
75
+ /**
76
+ * @description Checks if the event is paused.
77
+ * @public
78
+ * @returns {boolean} Whether the event is paused.
79
+ */
80
+ isPaused() {
81
+ return this.#paused === true;
82
+ }
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
+ /**
92
+ * @description Emits an event asynchronously, calling all listeners for that event type.
93
+ * @public
94
+ * @async
95
+ * @param {...Parameters<E>} args The arguments for the event listeners.
96
+ * @returns {this} The current instance.
97
+ */
98
+ async emitAsync(...args) {
99
+ const listeners = this.listeners();
100
+ if (!listeners || this.isPaused()) {
101
+ return this;
102
+ }
103
+ const snapshot = await listeners.snapshot();
104
+ await Promise.all(snapshot.map(listener => Promise.resolve(listener(...args))));
105
+ return this;
106
+ }
107
+ /**
108
+ * @description Adds a listener for event.
109
+ * @public
110
+ * @param {E} listener The listener function.
111
+ * @returns {this} The current instance.
112
+ */
113
+ on(listener) {
114
+ return this.#listeners.add(listener),
115
+ this;
116
+ }
117
+ /**
118
+ * @description Adds a one-time listener for event.
119
+ * @public
120
+ * @param {E} listener The listener function.
121
+ * @returns {this} The current instance.
122
+ */
123
+ once(listener) {
124
+ return this.#listeners.once(listener),
125
+ this;
126
+ }
127
+ /**
128
+ * @description Removes a listener for event.
129
+ * @public
130
+ * @param {E} listener The listener function.
131
+ * @returns {this} The current instance.
132
+ */
133
+ off(listener) {
134
+ return this.#listeners.delete(listener), this;
135
+ }
136
+ /**
137
+ * @description Pauses the event, preventing it from being emitted.
138
+ * @public
139
+ * @returns {this} The current instance.
140
+ */
141
+ pause() {
142
+ return this.#paused = true, this;
143
+ }
144
+ /**
145
+ * @description Resumes the event if it was paused.
146
+ * @public
147
+ * @returns {this} The current instance.
148
+ */
149
+ resume() {
150
+ return this.#paused = false, this;
151
+ }
152
+ }
153
+
154
+ // Class.
155
+ /**
156
+ * @description A base abstraction class that implements a named event emitter pattern.
157
+ * @export
158
+ * @abstract
159
+ * @class NamedEventEmitterBase
160
+ * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
161
+ * @template [T=any] The type of the underlying data for the listeners.
162
+ * @template {boolean} [R=false] The `boolean` type of the async flag for the listeners.
163
+ * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=any] The adapter type for the listeners.
164
+ */
165
+ class NamedEventEmitterBase {
166
+ /**
167
+ * @description The adapter class used to manage listeners.
168
+ * @type {new (...listeners: E[keyof E][]) => A}
169
+ */
170
+ #adapter;
171
+ /**
172
+ * @description Indicates whether the emitter listeners operate asynchronously.
173
+ * @type {R}
174
+ */
175
+ #async;
176
+ /**
177
+ * @description The map of events to their listeners.
178
+ * @type {Map<keyof E, Listeners<Parameters<E[keyof E]>, E[keyof E], T, R, A>>}
179
+ */
180
+ #events = new Map();
181
+ /**
182
+ * @description The set of names of paused events.
183
+ * @type {Set<keyof E>}
184
+ */
185
+ #pausedEvents = new Set();
186
+ /**
187
+ * Creates an instance of `NamedEventEmitterBase`.
188
+ * @constructor
189
+ * @param {{async?: R, value?: T}} param0
190
+ * @param {R} param0.async Whether the emitter listeners operate asynchronously.
191
+ * @param {T} param0.value The value underlying data for the listeners for capture its type only.
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.
194
+ */
195
+ constructor({ async, value }, adapter, events) {
196
+ this.#adapter = adapter;
197
+ this.#async = async ?? false;
198
+ for (const [event, listeners] of Object.entries(events ?? {})) {
199
+ this.#events.set(event, new Listeners(this.#async, this.#adapter));
200
+ for (const listener of listeners) {
201
+ this.#events.get(event).add(listener);
202
+ }
203
+ }
204
+ }
205
+ /**
206
+ * @description Removes all listeners for a specific event type.
207
+ * @public
208
+ * @template {keyof E} Event The event key.
209
+ * @param {Event} event The event name of key.
210
+ * @returns {this} The current instance.
211
+ */
212
+ clear(event) {
213
+ return this.#events.get(event)?.clear(), this;
214
+ }
215
+ /**
216
+ * @description Removes all listeners for all event types.
217
+ * @public
218
+ * @returns {this} The current instance.
219
+ */
220
+ clearAll() {
221
+ return this.#events.clear(), this;
222
+ }
223
+ /**
224
+ * @description Gets the number of listeners for a specific event.
225
+ * @public
226
+ * @template {keyof E} Event The event key.
227
+ * @param {Event} event The event name of key.
228
+ * @returns {number} The number of listeners for the event.
229
+ */
230
+ count(event) {
231
+ return this.#events.get(event)?.size ?? 0;
232
+ }
233
+ /**
234
+ * @description Emits an event, calling all listeners for that event type.
235
+ * @public
236
+ * @template {keyof E} Event The event key.
237
+ * @param {Event} event The event name of key.
238
+ * @param {...Parameters<E[Event]>} args The arguments for the event listeners.
239
+ */
240
+ emit(event, ...args) {
241
+ return !this.isPaused(event) && this.#events.get(event)?.forEach(listener => listener(...args)),
242
+ this;
243
+ }
244
+ /**
245
+ * @description Gets all event names with registered listeners.
246
+ * @public
247
+ * @returns {(keyof E)[]} Array of event names.
248
+ */
249
+ eventNames() {
250
+ return Array.from(this.#events.keys());
251
+ }
252
+ /**
253
+ * @description Checks if the event is paused.
254
+ * @public
255
+ * @param {keyof E} event The event name of key.
256
+ * @returns {boolean} Whether the event is paused.
257
+ */
258
+ isPaused(event) {
259
+ return this.#pausedEvents.has(event);
260
+ }
261
+ /**
262
+ * @description Gets the listeners for a specific event type.
263
+ * @public
264
+ * @template {keyof E} Event The event key.
265
+ * @param {Event} event The event name of key.
266
+ * @returns {(ListenersFor<Event, E, T, R> | undefined)} The listeners for the event.
267
+ */
268
+ listeners(event) {
269
+ return this.#events.get(event);
270
+ }
271
+ /**
272
+ * @description Emits an event asynchronously, calling all listeners for that event type.
273
+ * @public
274
+ * @async
275
+ * @template {keyof E} Event The event key.
276
+ * @param {Event} event The event name of key.
277
+ * @param {...Parameters<E[Event]>} args The arguments for the event listeners.
278
+ * @returns {this} The current instance.
279
+ */
280
+ async emitAsync(event, ...args) {
281
+ const listeners = this.listeners(event);
282
+ if (!listeners || this.isPaused(event)) {
283
+ return this;
284
+ }
285
+ const snapshot = await listeners.snapshot();
286
+ await Promise.all(snapshot.map(listener => Promise.resolve(listener(...args))));
287
+ return this;
288
+ }
289
+ /**
290
+ * @description Adds a listener for a specific event type.
291
+ * @public
292
+ * @template {keyof E} Event The event key.
293
+ * @param {Event} event The event name of key.
294
+ * @param {E[Event]} listener The listener function.
295
+ * @returns {this} The current instance.
296
+ */
297
+ on(event, listener) {
298
+ return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapter)),
299
+ this.#events.get(event).add(listener),
300
+ this;
301
+ }
302
+ /**
303
+ * @description Adds a listener for a specific event type.
304
+ * @public
305
+ * @template {keyof E} Event The event key.
306
+ * @param {Event} event The event name of key.
307
+ * @param {E[Event]} listener The listener function.
308
+ * @returns {this} The current instance.
309
+ */
310
+ once(event, listener) {
311
+ return !this.#events.has(event) && this.#events.set(event, new Listeners(this.#async, this.#adapter)),
312
+ this.#events.get(event).once(listener),
313
+ this;
314
+ }
315
+ /**
316
+ * @description Removes a listener for a specific event type.
317
+ * @public
318
+ * @template {keyof E} Event The event key.
319
+ * @param {Event} event The event name of key.
320
+ * @param {E[Event]} listener The listener function.
321
+ * @returns {this} The current instance.
322
+ */
323
+ off(event, listener) {
324
+ return this.#events.get(event)?.delete(listener), this;
325
+ }
326
+ /**
327
+ * @description Pauses the event, preventing it from being emitted.
328
+ * @public
329
+ * @template {keyof E} Event The event key.
330
+ * @param {Event} event The event name of key.
331
+ * @returns {this} The current instance.
332
+ */
333
+ pause(event) {
334
+ return this.#pausedEvents.add(event), this;
335
+ }
336
+ /**
337
+ * @description Resumes the event if it was paused.
338
+ * @public
339
+ * @template {keyof E} Event The event key.
340
+ * @param {Event} event The event name of key.
341
+ * @returns {this} The current instance.
342
+ */
343
+ resume(event) {
344
+ return this.#pausedEvents.delete(event), this;
345
+ }
346
+ }
347
+
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
+ // Abstract.
367
+ /**
368
+ * @description A concrete class that implements an event emitter pattern.
369
+ * @export
370
+ * @class EventEmitter
371
+ * @template {ListenerFunction<any[]>} E The object type of events.
372
+ * @template [T=any] The type of the listeners underlying data.
373
+ * @template {boolean} [R=false] The async flag for the listeners.
374
+ * @template {ListenersAdapter<Parameters<E>, E, any, R >} [A=R extends false ? ListenersSetAdapter<E> : any] The adapter type for the listeners.
375
+ * @extends {EventEmitterBase<E, any, R, A>}Base class abstracting common event emitter functionality.
376
+ */
377
+ class EventEmitter extends EventEmitterBase {
378
+ /**
379
+ * Creates an instance of `EventEmitter`.
380
+ * @constructor
381
+ * @param {R} async
382
+ * @param {?(E | E[])} [events]
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.
390
+ * @param {R} param0.async Whether the emitter listeners operate asynchronously.
391
+ * @param {T} param0.value The underlying data for the listeners for capture its type only.
392
+ * @param {?(E | E[])} [events] The initial listeners.
393
+ */
394
+ constructor({ adapter, async, value }, events) {
395
+ super({ async, value }, adapter ?? ListenersSetAdapter, events);
396
+ }
397
+ }
398
+
399
+ // Abstract.
400
+ /**
401
+ * @description A concrete class that implements a named event emitter pattern.
402
+ * @export
403
+ * @class NamedEventEmitter
404
+ * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
405
+ * @template {boolean} [R=false] The `boolean` type of the async flag for the listeners.
406
+ * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, R>} [A=R extends false ? ListenersSetAdapter<E[keyof E]> : any]
407
+ * @extends {NamedEventEmitterBase<E, any, R, A>}
408
+ */
409
+ class NamedEventEmitter extends NamedEventEmitterBase {
410
+ /**
411
+ * Creates an instance of `NamedEventEmitter`.
412
+ * @constructor
413
+ * @param {R} async Whether the emitter listeners operate asynchronously.
414
+ * @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial listeners.
415
+ * @param {new (...listeners: E[keyof E][]) => A} [adapter=ListenersSetAdapter as any] The adapter class to manage listeners.
416
+ */
417
+ constructor({ adapter, async, value }, events) {
418
+ super({ async }, adapter ?? ListenersSetAdapter, events);
419
+ }
420
+ }
421
+
422
+ // Abstract.
423
+
424
+ /*
425
+ * Public API Surface of event-emitter
426
+ */
427
+
428
+ /**
429
+ * Generated bundle index. Do not edit.
430
+ */
431
+
432
+ export { EventEmitter, EventEmitterBase, NamedEventEmitter, NamedEventEmitterBase };
433
+ //# sourceMappingURL=typescript-package-event-emitter.mjs.map
@@ -0,0 +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,M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;AACF,QAAA,OAAO,IAAI;IACb;IAEO,QAAQ,GAAA;QACb,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC;IAC/B;AACD;;ACzBD;AAMA;;;;;;;;;AASG;AACG,MAAO,YAKX,SAAQ,gBAA4B,CAAA;AACpC;;;;;;AAMG;AAEH;;;;;;;;AAQG;IACH,WAAA,CACE,EAAC,OAAO,EAAE,KAAK,EAAE,KAAK,EAAiE,EACvF,MAAgB,EAAA;AAEhB,QAAA,KAAK,CAAC,EAAC,KAAK,EAAE,KAAK,EAAC,EAAE,OAAO,IAAI,mBAA0B,EAAE,MAAM,CAAC;IACtE;AACD;;AC7CD;AAMA;;;;;;;;AAQG;AACG,MAAO,iBAKX,SAAQ,qBAAiC,CAAA;AACzC;;;;;;AAMG;IACH,WAAA,CACE,EAAC,OAAO,EAAE,KAAK,EAAE,KAAK,EAA0E,EAChG,MAA4C,EAAA;QAE5C,KAAK,CAAC,EAAC,KAAK,EAAC,EAAE,OAAO,IAAI,mBAA0B,EAAE,MAAM,CAAC;IAC/D;AACD;;AClCD;;ACAA;;AAEG;;ACFH;;AAEG;;;;"}
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@typescript-package/event-emitter",
3
+ "version": "0.1.0",
4
+ "author": "wwwdev.io <dev@wwwdev.io>",
5
+ "description": "A lightweight TypeScript package for event emitter.",
6
+ "license": "MIT",
7
+ "publishConfig": {
8
+ "access": "public",
9
+ "registry": "https://registry.npmjs.org"
10
+ },
11
+ "peerDependencies": {
12
+ "@typescript-package/collection-adapter": "^0.2.0",
13
+ "@typescript-package/listeners": "^0.0.1"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/typescript-package/event-emitter.git"
18
+ },
19
+ "bugs": {
20
+ "url": "https://github.com/typescript-package/event-emitter/issues"
21
+ },
22
+ "keywords": [
23
+ "@typescript-package",
24
+ "@typescript-package/event-emitter",
25
+ "EventEmitter",
26
+ "Listeners"
27
+ ],
28
+ "funding": [
29
+ {
30
+ "type": "stripe",
31
+ "url": "https://donate.stripe.com/dR614hfDZcJE3wAcMM"
32
+ },
33
+ {
34
+ "type": "individual",
35
+ "url": "https://checkout.revolut.com/pay/048b10a3-0e10-42c8-a917-e3e9cb4c8e29"
36
+ }
37
+ ],
38
+ "sideEffects": false,
39
+ "module": "fesm2022/typescript-package-event-emitter.mjs",
40
+ "typings": "types/typescript-package-event-emitter.d.ts",
41
+ "exports": {
42
+ "./package.json": {
43
+ "default": "./package.json"
44
+ },
45
+ ".": {
46
+ "types": "./types/typescript-package-event-emitter.d.ts",
47
+ "default": "./fesm2022/typescript-package-event-emitter.mjs"
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,312 @@
1
+ import { Listeners } from '@typescript-package/listeners';
2
+ import { ListenerFunction, ListenersAdapter } from '@typedly/listeners';
3
+ import { SetAdapter } from '@typescript-package/collection-adapter';
4
+
5
+ /**
6
+ * @description The base abstraction class for an event emitter pattern.
7
+ * @export
8
+ * @abstract
9
+ * @class EventEmitterBase
10
+ * @template {ListenerFunction<any[]>} E The listener function type.
11
+ * @template [T=any] The type of the listeners underlying data.
12
+ * @template {boolean} [R=false] The async flag for the listeners.
13
+ * @template {ListenersAdapter<Parameters<E>, E, T, R>} [A=any] The adapter type for the listeners.
14
+ */
15
+ declare abstract class EventEmitterBase<E extends ListenerFunction<any[]>, T = any, R extends boolean = false, A extends ListenersAdapter<Parameters<E>, E, T, R> = any> {
16
+ #private;
17
+ /**
18
+ * Creates an instance of `EventEmitterBase`.
19
+ * @constructor
20
+ * @param {{async?: R, value?: T}} param0 The object with configuration options.
21
+ * @param {R} param0.async Whether the emitter listeners operate asynchronously.
22
+ * @param {T} param0.value The underlying data for the listeners for capture its type only.
23
+ * @param {new (...listeners: E[]) => A} adapter The adapter class to manage listeners.
24
+ * @param {?(E | E[])} [events] The initial listeners.
25
+ */
26
+ constructor({ async, value }: {
27
+ async?: R;
28
+ value?: T;
29
+ }, adapter: new (...listeners: E[]) => A, events?: E | E[]);
30
+ /**
31
+ * @description Removes all listeners for a specific event type.
32
+ * @public
33
+ * @returns {this} The current instance.
34
+ */
35
+ clear(): this;
36
+ /**
37
+ * @description Gets the number of listeners for a specific event.
38
+ * @public
39
+ * @returns {number} The number of listeners for the event.
40
+ */
41
+ count(): number;
42
+ /**
43
+ * @description Emits an event, calling all listeners for that event type.
44
+ * @public
45
+ * @param {...Parameters<E>} args The arguments for the event listeners.
46
+ */
47
+ emit(...args: Parameters<E>): this;
48
+ /**
49
+ * @description Checks if the event is paused.
50
+ * @public
51
+ * @returns {boolean} Whether the event is paused.
52
+ */
53
+ 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
+ /**
61
+ * @description Emits an event asynchronously, calling all listeners for that event type.
62
+ * @public
63
+ * @async
64
+ * @param {...Parameters<E>} args The arguments for the event listeners.
65
+ * @returns {this} The current instance.
66
+ */
67
+ emitAsync(...args: Parameters<E>): Promise<this>;
68
+ /**
69
+ * @description Adds a listener for event.
70
+ * @public
71
+ * @param {E} listener The listener function.
72
+ * @returns {this} The current instance.
73
+ */
74
+ on(listener: E): this;
75
+ /**
76
+ * @description Adds a one-time listener for event.
77
+ * @public
78
+ * @param {E} listener The listener function.
79
+ * @returns {this} The current instance.
80
+ */
81
+ once(listener: E): this;
82
+ /**
83
+ * @description Removes a listener for event.
84
+ * @public
85
+ * @param {E} listener The listener function.
86
+ * @returns {this} The current instance.
87
+ */
88
+ off(listener: E): this;
89
+ /**
90
+ * @description Pauses the event, preventing it from being emitted.
91
+ * @public
92
+ * @returns {this} The current instance.
93
+ */
94
+ pause(): this;
95
+ /**
96
+ * @description Resumes the event if it was paused.
97
+ * @public
98
+ * @returns {this} The current instance.
99
+ */
100
+ resume(): this;
101
+ }
102
+
103
+ /**
104
+ * @description
105
+ * @export
106
+ * @template {keyof E} Event
107
+ * @template {Record<string, ListenerFunction<any[]>>} E
108
+ * @template [T=any]
109
+ * @template {boolean} [R=false]
110
+ */
111
+ type ListenersFor<Event extends keyof E, E extends Record<string, ListenerFunction<any[]>>, T = any, R extends boolean = false> = Listeners<Parameters<E[Event]>, E[Event], T, R, ListenersAdapter<Parameters<E[Event]>, E[Event], T, R>>;
112
+
113
+ /**
114
+ * @description A base abstraction class that implements a named event emitter pattern.
115
+ * @export
116
+ * @abstract
117
+ * @class NamedEventEmitterBase
118
+ * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
119
+ * @template [T=any] The type of the underlying data for the listeners.
120
+ * @template {boolean} [R=false] The `boolean` type of the async flag for the listeners.
121
+ * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R>} [A=any] The adapter type for the listeners.
122
+ */
123
+ declare abstract class NamedEventEmitterBase<E extends Record<string, ListenerFunction<any[]>>, T = any, R extends boolean = false, A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = any> {
124
+ #private;
125
+ /**
126
+ * Creates an instance of `NamedEventEmitterBase`.
127
+ * @constructor
128
+ * @param {{async?: R, value?: T}} param0
129
+ * @param {R} param0.async Whether the emitter listeners operate asynchronously.
130
+ * @param {T} param0.value The value underlying data for the listeners for capture its type only.
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.
133
+ */
134
+ constructor({ async, value }: {
135
+ async?: R;
136
+ value?: T;
137
+ }, adapter: new (...listeners: E[keyof E][]) => A, events?: Partial<{
138
+ [K in keyof E]: E[K][];
139
+ }>);
140
+ /**
141
+ * @description Removes all listeners for a specific event type.
142
+ * @public
143
+ * @template {keyof E} Event The event key.
144
+ * @param {Event} event The event name of key.
145
+ * @returns {this} The current instance.
146
+ */
147
+ clear<Event extends keyof E>(event: Event): this;
148
+ /**
149
+ * @description Removes all listeners for all event types.
150
+ * @public
151
+ * @returns {this} The current instance.
152
+ */
153
+ clearAll(): this;
154
+ /**
155
+ * @description Gets the number of listeners for a specific event.
156
+ * @public
157
+ * @template {keyof E} Event The event key.
158
+ * @param {Event} event The event name of key.
159
+ * @returns {number} The number of listeners for the event.
160
+ */
161
+ count<Event extends keyof E>(event: Event): number;
162
+ /**
163
+ * @description Emits an event, calling all listeners for that event type.
164
+ * @public
165
+ * @template {keyof E} Event The event key.
166
+ * @param {Event} event The event name of key.
167
+ * @param {...Parameters<E[Event]>} args The arguments for the event listeners.
168
+ */
169
+ emit<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): this;
170
+ /**
171
+ * @description Gets all event names with registered listeners.
172
+ * @public
173
+ * @returns {(keyof E)[]} Array of event names.
174
+ */
175
+ eventNames(): (keyof E)[];
176
+ /**
177
+ * @description Checks if the event is paused.
178
+ * @public
179
+ * @param {keyof E} event The event name of key.
180
+ * @returns {boolean} Whether the event is paused.
181
+ */
182
+ isPaused(event: keyof E): boolean;
183
+ /**
184
+ * @description Gets the listeners for a specific event type.
185
+ * @public
186
+ * @template {keyof E} Event The event key.
187
+ * @param {Event} event The event name of key.
188
+ * @returns {(ListenersFor<Event, E, T, R> | undefined)} The listeners for the event.
189
+ */
190
+ listeners<Event extends keyof E>(event: Event): ListenersFor<Event, E, T, R> | undefined;
191
+ /**
192
+ * @description Emits an event asynchronously, calling all listeners for that event type.
193
+ * @public
194
+ * @async
195
+ * @template {keyof E} Event The event key.
196
+ * @param {Event} event The event name of key.
197
+ * @param {...Parameters<E[Event]>} args The arguments for the event listeners.
198
+ * @returns {this} The current instance.
199
+ */
200
+ emitAsync<Event extends keyof E>(event: Event, ...args: Parameters<E[Event]>): Promise<this>;
201
+ /**
202
+ * @description Adds a listener for a specific event type.
203
+ * @public
204
+ * @template {keyof E} Event The event key.
205
+ * @param {Event} event The event name of key.
206
+ * @param {E[Event]} listener The listener function.
207
+ * @returns {this} The current instance.
208
+ */
209
+ on<Event extends keyof E>(event: Event, listener: E[Event]): this;
210
+ /**
211
+ * @description Adds a listener for a specific event type.
212
+ * @public
213
+ * @template {keyof E} Event The event key.
214
+ * @param {Event} event The event name of key.
215
+ * @param {E[Event]} listener The listener function.
216
+ * @returns {this} The current instance.
217
+ */
218
+ once<Event extends keyof E>(event: Event, listener: E[Event]): this;
219
+ /**
220
+ * @description Removes a listener for a specific event type.
221
+ * @public
222
+ * @template {keyof E} Event The event key.
223
+ * @param {Event} event The event name of key.
224
+ * @param {E[Event]} listener The listener function.
225
+ * @returns {this} The current instance.
226
+ */
227
+ off<Event extends keyof E>(event: Event, listener: E[Event]): this;
228
+ /**
229
+ * @description Pauses the event, preventing it from being emitted.
230
+ * @public
231
+ * @template {keyof E} Event The event key.
232
+ * @param {Event} event The event name of key.
233
+ * @returns {this} The current instance.
234
+ */
235
+ pause<Event extends keyof E>(event: Event): this;
236
+ /**
237
+ * @description Resumes the event if it was paused.
238
+ * @public
239
+ * @template {keyof E} Event The event key.
240
+ * @param {Event} event The event name of key.
241
+ * @returns {this} The current instance.
242
+ */
243
+ resume<Event extends keyof E>(event: Event): this;
244
+ }
245
+
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
+ /**
253
+ * @description A concrete class that implements an event emitter pattern.
254
+ * @export
255
+ * @class EventEmitter
256
+ * @template {ListenerFunction<any[]>} E The object type of events.
257
+ * @template [T=any] The type of the listeners underlying data.
258
+ * @template {boolean} [R=false] The async flag for the listeners.
259
+ * @template {ListenersAdapter<Parameters<E>, E, any, R >} [A=R extends false ? ListenersSetAdapter<E> : any] The adapter type for the listeners.
260
+ * @extends {EventEmitterBase<E, any, R, A>}Base class abstracting common event emitter functionality.
261
+ */
262
+ declare class EventEmitter<E extends ListenerFunction<any[]>, T = any, R extends boolean = false, A extends ListenersAdapter<Parameters<E>, E, T, R> = R extends false ? ListenersSetAdapter<E> : any> extends EventEmitterBase<E, T, R, A> {
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
+ */
270
+ /**
271
+ * Creates an instance of `EventEmitter`.
272
+ * @constructor
273
+ * @param {{adapter?: new (...listeners: E[]) => A, async?: R, value?: T}} param0
274
+ * @param {new (...listeners: {}) => A} param0.adapter The adapter class to manage listeners.
275
+ * @param {R} param0.async Whether the emitter listeners operate asynchronously.
276
+ * @param {T} param0.value The underlying data for the listeners for capture its type only.
277
+ * @param {?(E | E[])} [events] The initial listeners.
278
+ */
279
+ constructor({ adapter, async, value }: {
280
+ adapter?: new (...listeners: E[]) => A;
281
+ async?: R;
282
+ value?: T;
283
+ }, events?: E | E[]);
284
+ }
285
+
286
+ /**
287
+ * @description A concrete class that implements a named event emitter pattern.
288
+ * @export
289
+ * @class NamedEventEmitter
290
+ * @template {Record<string, ListenerFunction<any[]>>} E Object mapping event names to their listener function types.
291
+ * @template {boolean} [R=false] The `boolean` type of the async flag for the listeners.
292
+ * @template {ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], any, R>} [A=R extends false ? ListenersSetAdapter<E[keyof E]> : any]
293
+ * @extends {NamedEventEmitterBase<E, any, R, A>}
294
+ */
295
+ declare class NamedEventEmitter<E extends Record<string, ListenerFunction<any[]>>, T = any, R extends boolean = false, A extends ListenersAdapter<Parameters<E[keyof E]>, E[keyof E], T, R> = R extends false ? ListenersSetAdapter<E[keyof E]> : any> extends NamedEventEmitterBase<E, T, R, A> {
296
+ /**
297
+ * Creates an instance of `NamedEventEmitter`.
298
+ * @constructor
299
+ * @param {R} async Whether the emitter listeners operate asynchronously.
300
+ * @param {?Partial<{ [K in keyof E]: E[K][] }>} [events] The initial listeners.
301
+ * @param {new (...listeners: E[keyof E][]) => A} [adapter=ListenersSetAdapter as any] The adapter class to manage listeners.
302
+ */
303
+ constructor({ adapter, async, value }: {
304
+ adapter?: new (...listeners: E[keyof E][]) => A;
305
+ async?: R;
306
+ value?: T;
307
+ }, events?: Partial<{
308
+ [K in keyof E]: E[K][];
309
+ }>);
310
+ }
311
+
312
+ export { EventEmitter, EventEmitterBase, NamedEventEmitter, NamedEventEmitterBase };