chiisai-event-emitter 0.0.0-development → 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 +9 -26
- package/lib/index.d.ts +23 -0
- package/lib/index.js +29 -0
- package/package.json +2 -2
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,32 +23,17 @@ npm install chiisai-event-emitter
|
|
|
25
23
|
## Usage
|
|
26
24
|
|
|
27
25
|
```ts
|
|
28
|
-
import
|
|
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
|
-
|
|
28
|
+
const eventEmitter = new EventEmitter();
|
|
41
29
|
|
|
42
|
-
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
38
|
[build-img]:https://github.com/ryansonshine/chiisai-event-emitter/actions/workflows/release.yml/badge.svg
|
|
56
39
|
[build-url]:https://github.com/ryansonshine/chiisai-event-emitter/actions/workflows/release.yml
|
package/lib/index.d.ts
ADDED
|
@@ -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 default 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
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
class EventEmitter {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.subscriptions = new Map();
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* @param {string} eventName
|
|
9
|
+
* @param {EventCallback} callback
|
|
10
|
+
* @return {Unsubscribe}
|
|
11
|
+
*/
|
|
12
|
+
subscribe(eventName, callback) {
|
|
13
|
+
const id = Symbol(callback.toString());
|
|
14
|
+
this.subscriptions.set(eventName, this.subscriptions.has(eventName)
|
|
15
|
+
? [...(this.subscriptions.get(eventName) || []), { id, callback }]
|
|
16
|
+
: [{ id, callback }]);
|
|
17
|
+
return {
|
|
18
|
+
unsubscribe: () => this.subscriptions.set(eventName, (this.subscriptions.get(eventName) || []).filter(({ id: subscriptionId }) => subscriptionId !== id)),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} eventName
|
|
23
|
+
* @param {Array<any>} args
|
|
24
|
+
*/
|
|
25
|
+
emit(eventName, ...args) {
|
|
26
|
+
(this.subscriptions.get(eventName) || []).forEach(({ callback }) => callback(...args));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.default = EventEmitter;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chiisai-event-emitter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
+
}
|