chiisai-event-emitter 0.0.0-development → 1.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Chiisai Event Emitter
2
2
 
3
- A minimal event emitter library for Node.JS.
3
+ A minimal and performant event emitter library for Node.JS.
4
4
 
5
5
  ---
6
6
 
@@ -14,8 +14,6 @@ A minimal event emitter library for Node.JS.
14
14
  [![Commitizen Friendly][commitizen-img]][commitizen-url]
15
15
  [![Semantic Release][semantic-release-img]][semantic-release-url]
16
16
 
17
- > My awesome module
18
-
19
17
  ## Install
20
18
 
21
19
  ```bash
@@ -25,43 +23,28 @@ npm install chiisai-event-emitter
25
23
  ## Usage
26
24
 
27
25
  ```ts
28
- import { myPackage } from 'chiisai-event-emitter';
29
-
30
- myPackage('hello');
31
- //=> 'hello from my package'
32
- ```
33
-
34
- ## API
35
-
36
- ### myPackage(input, options?)
37
-
38
- #### input
26
+ import { EventEmitter } from 'chiisai-event-emitter';
39
27
 
40
- Type: `string`
28
+ const eventEmitter = new EventEmitter();
41
29
 
42
- Lorem ipsum.
30
+ eventEmitter.subscribe('test-event', () => console.log('test-event handler called!'));
31
+ eventEmitter.subscribe('test-event', () => console.log('another test-event handler called!'))
43
32
 
44
- #### options
45
-
46
- Type: `object`
47
-
48
- ##### postfix
49
-
50
- Type: `string`
51
- Default: `rainbows`
52
-
53
- Lorem ipsum.
33
+ eventEmitter.emit('test-event')
34
+ // test-event handler called!
35
+ // another test-event handler called!
36
+ ```
54
37
 
55
- [build-img]:https://github.com/ryansonshine/chiisai-event-emitter/actions/workflows/release.yml/badge.svg
56
- [build-url]:https://github.com/ryansonshine/chiisai-event-emitter/actions/workflows/release.yml
38
+ [build-img]:https://github.com/bencelaszlo/chiisai-event-emitter/actions/workflows/release.yml/badge.svg
39
+ [build-url]:https://github.com/bencelaszlo/chiisai-event-emitter/actions/workflows/release.yml
57
40
  [downloads-img]:https://img.shields.io/npm/dt/chiisai-event-emitter
58
41
  [downloads-url]:https://www.npmtrends.com/chiisai-event-emitter
59
42
  [npm-img]:https://img.shields.io/npm/v/chiisai-event-emitter
60
43
  [npm-url]:https://www.npmjs.com/package/chiisai-event-emitter
61
- [issues-img]:https://img.shields.io/github/issues/ryansonshine/chiisai-event-emitter
62
- [issues-url]:https://github.com/ryansonshine/chiisai-event-emitter/issues
63
- [codecov-img]:https://codecov.io/gh/ryansonshine/chiisai-event-emitter/branch/main/graph/badge.svg
64
- [codecov-url]:https://codecov.io/gh/ryansonshine/chiisai-event-emitter
44
+ [issues-img]:https://img.shields.io/github/issues/bencelaszlo/chiisai-event-emitter
45
+ [issues-url]:https://github.com/bencelaszlo/chiisai-event-emitter/issues
46
+ [codecov-img]:https://codecov.io/gh/bencelaszlo/chiisai-event-emitter/branch/main/graph/badge.svg
47
+ [codecov-url]:https://codecov.io/gh/bencelaszlo/chiisai-event-emitter
65
48
  [semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
66
49
  [semantic-release-url]:https://github.com/semantic-release/semantic-release
67
50
  [commitizen-img]:https://img.shields.io/badge/commitizen-friendly-brightgreen.svg
@@ -0,0 +1,23 @@
1
+ export declare type EventCallback<T = void> = (...args: Array<any>) => T;
2
+ export declare type Subscriptions = Map<string, Array<{
3
+ id: symbol;
4
+ callback: EventCallback;
5
+ }>>;
6
+ export declare type Unsubscribe = {
7
+ unsubscribe: () => void;
8
+ };
9
+ export declare class EventEmitter {
10
+ subscriptions: Subscriptions;
11
+ constructor();
12
+ /**
13
+ * @param {string} eventName
14
+ * @param {EventCallback} callback
15
+ * @return {Unsubscribe}
16
+ */
17
+ subscribe(eventName: string, callback: EventCallback): Unsubscribe;
18
+ /**
19
+ * @param {string} eventName
20
+ * @param {Array<any>} args
21
+ */
22
+ emit(eventName: string, ...args: Array<any>): void;
23
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EventEmitter = void 0;
4
+ class EventEmitter {
5
+ constructor() {
6
+ this.subscriptions = new Map();
7
+ }
8
+ /**
9
+ * @param {string} eventName
10
+ * @param {EventCallback} callback
11
+ * @return {Unsubscribe}
12
+ */
13
+ subscribe(eventName, callback) {
14
+ const id = Symbol(callback.toString());
15
+ this.subscriptions.set(eventName, this.subscriptions.has(eventName)
16
+ ? [...(this.subscriptions.get(eventName) || []), { id, callback }]
17
+ : [{ id, callback }]);
18
+ return {
19
+ unsubscribe: () => this.subscriptions.set(eventName, (this.subscriptions.get(eventName) || []).filter(({ id: subscriptionId }) => subscriptionId !== id)),
20
+ };
21
+ }
22
+ /**
23
+ * @param {string} eventName
24
+ * @param {Array<any>} args
25
+ */
26
+ emit(eventName, ...args) {
27
+ (this.subscriptions.get(eventName) || []).forEach(({ callback }) => callback(...args));
28
+ }
29
+ }
30
+ exports.EventEmitter = EventEmitter;
package/lib/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './EventEmitter';
package/lib/index.js ADDED
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./EventEmitter"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chiisai-event-emitter",
3
- "version": "0.0.0-development",
3
+ "version": "1.0.1",
4
4
  "description": "A minimal event emitter.",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
@@ -115,4 +115,4 @@
115
115
  "@semantic-release/github"
116
116
  ]
117
117
  }
118
- }
118
+ }