@wxn0brp/event-emitter 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 wxn0brP
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,49 @@
1
+ # @wxn0brp/event-emitter
2
+
3
+ A simple event emitter.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @wxn0brp/event-emitter
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import { EventEmitter } from '@wxn0brp/event-emitter';
15
+
16
+ const emitter = new EventEmitter();
17
+
18
+ emitter.on('hello', (name) => {
19
+ console.log(`Hello, ${name}!`);
20
+ });
21
+
22
+ emitter.emit('hello', 'world');
23
+ ```
24
+
25
+ ## API
26
+
27
+ ### `on(event: string, listener: Function): void`
28
+
29
+ Registers an event listener.
30
+
31
+ ### `once(event: string, listener: Function): void`
32
+
33
+ Registers a one-time event listener.
34
+
35
+ ### `off(event: string, listener: Function): void`
36
+
37
+ Removes an event listener.
38
+
39
+ ### `emit(event: string, ...args: any[]): void`
40
+
41
+ Emits an event.
42
+
43
+ ### `listenerCount(event: string): number`
44
+
45
+ Returns the number of listeners for the given event.
46
+
47
+ ## License
48
+
49
+ This project is licensed under the MIT License.
@@ -0,0 +1,34 @@
1
+ export declare class EventEmitter {
2
+ _events: {
3
+ [event: string]: Function[];
4
+ };
5
+ /**
6
+ * Registers an event listener
7
+ * @param {string} event - event name
8
+ * @param {Function} listener - function to be called when event occurs
9
+ */
10
+ on(event: string, listener: Function): void;
11
+ /**
12
+ * Registers a one-time event listener
13
+ * @param {string} event - event name
14
+ * @param {Function} listener - function to be called once
15
+ */
16
+ once(event: string, listener: Function): void;
17
+ /**
18
+ * Removes an event listener.
19
+ * @param {string} event - event name
20
+ * @param {Function} listener - listener to remove
21
+ */
22
+ off(event: string, listener: Function): void;
23
+ /**
24
+ * Emits an event
25
+ * @param {string} event - event name
26
+ * @param {...any} args - arguments to be passed to listeners
27
+ */
28
+ emit(event: string, ...args: any[]): void;
29
+ /**
30
+ * Returns the number of listeners for the given event
31
+ * @param {string} event - event name
32
+ */
33
+ listenerCount(event: string): number;
34
+ }
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ export class EventEmitter {
2
+ _events = {};
3
+ /**
4
+ * Registers an event listener
5
+ * @param {string} event - event name
6
+ * @param {Function} listener - function to be called when event occurs
7
+ */
8
+ on(event, listener) {
9
+ if (!this._events[event])
10
+ this._events[event] = [];
11
+ this._events[event].push(listener);
12
+ }
13
+ /**
14
+ * Registers a one-time event listener
15
+ * @param {string} event - event name
16
+ * @param {Function} listener - function to be called once
17
+ */
18
+ once(event, listener) {
19
+ const onceListener = (...args) => {
20
+ this.off(event, onceListener);
21
+ listener(...args);
22
+ };
23
+ this.on(event, onceListener);
24
+ }
25
+ /**
26
+ * Removes an event listener.
27
+ * @param {string} event - event name
28
+ * @param {Function} listener - listener to remove
29
+ */
30
+ off(event, listener) {
31
+ if (!this._events[event])
32
+ return;
33
+ this._events[event] = this._events[event].filter(l => l !== listener);
34
+ }
35
+ /**
36
+ * Emits an event
37
+ * @param {string} event - event name
38
+ * @param {...any} args - arguments to be passed to listeners
39
+ */
40
+ emit(event, ...args) {
41
+ const listeners = this._events[event];
42
+ if (listeners && listeners.length > 0) {
43
+ listeners.forEach(listener => {
44
+ listener(...args);
45
+ });
46
+ }
47
+ }
48
+ /**
49
+ * Returns the number of listeners for the given event
50
+ * @param {string} event - event name
51
+ */
52
+ listenerCount(event) {
53
+ return this._events[event]?.length || 0;
54
+ }
55
+ }
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@wxn0brp/event-emitter",
3
+ "version": "0.0.1",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "author": "wxn0brP",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "description": "A simple event emitter",
10
+ "repository": {
11
+ "type": "git",
12
+ "url": "https://github.com/wxn0brP/EventEmitter.git"
13
+ },
14
+ "homepage": "https://github.com/wxn0brP/EventEmitter",
15
+ "devDependencies": {
16
+ "typescript": "^5.7.3"
17
+ },
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "import": "./dist/index.js",
25
+ "default": "./dist/index.js"
26
+ },
27
+ "./*": {
28
+ "types": "./dist/*.d.ts",
29
+ "import": "./dist/*.js",
30
+ "default": "./dist/*.js"
31
+ }
32
+ }
33
+ }