chiisai-event-emitter 1.0.1 → 2.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 +15 -5
- package/lib/EventEmitter.d.ts +8 -5
- package/lib/EventEmitter.js +18 -9
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -22,17 +22,27 @@ npm install chiisai-event-emitter
|
|
|
22
22
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
|
+
### Subscribe to an Event
|
|
26
|
+
|
|
25
27
|
```ts
|
|
26
28
|
import { EventEmitter } from 'chiisai-event-emitter';
|
|
27
29
|
|
|
28
30
|
const eventEmitter = new EventEmitter();
|
|
29
31
|
|
|
30
|
-
eventEmitter.subscribe('
|
|
31
|
-
|
|
32
|
+
eventEmitter.subscribe('event', () => console.log('event-handler called!'));
|
|
33
|
+
|
|
34
|
+
eventEmitter.emit('event')
|
|
35
|
+
// event-handler called!
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Unsubscibe
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const unsubscibe = eventEmitter.subscribe('event', () => console.log('event-handler called!'));
|
|
42
|
+
unsubscribe();
|
|
32
43
|
|
|
33
|
-
eventEmitter.emit('
|
|
34
|
-
//
|
|
35
|
-
// another test-event handler called!
|
|
44
|
+
eventEmitter.emit('event')
|
|
45
|
+
// (nothing happened)
|
|
36
46
|
```
|
|
37
47
|
|
|
38
48
|
[build-img]:https://github.com/bencelaszlo/chiisai-event-emitter/actions/workflows/release.yml/badge.svg
|
package/lib/EventEmitter.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
export declare type EventCallback<T = void> = (...args: Array<any>) => T;
|
|
2
|
-
export declare
|
|
2
|
+
export declare class Subscriptions extends Map<string, Array<{
|
|
3
3
|
id: symbol;
|
|
4
4
|
callback: EventCallback;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
}>> {
|
|
6
|
+
get(key: string): {
|
|
7
|
+
id: symbol;
|
|
8
|
+
callback: EventCallback;
|
|
9
|
+
}[];
|
|
10
|
+
}
|
|
11
|
+
export declare type Unsubscribe = () => void;
|
|
9
12
|
export declare class EventEmitter {
|
|
10
13
|
subscriptions: Subscriptions;
|
|
11
14
|
constructor();
|
package/lib/EventEmitter.js
CHANGED
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EventEmitter = void 0;
|
|
3
|
+
exports.EventEmitter = exports.Subscriptions = void 0;
|
|
4
|
+
class Subscriptions extends Map {
|
|
5
|
+
get(key) {
|
|
6
|
+
return super.get(key) || [];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.Subscriptions = Subscriptions;
|
|
4
10
|
class EventEmitter {
|
|
5
11
|
constructor() {
|
|
6
|
-
this.subscriptions = new
|
|
12
|
+
this.subscriptions = new Subscriptions();
|
|
7
13
|
}
|
|
8
14
|
/**
|
|
9
15
|
* @param {string} eventName
|
|
@@ -12,19 +18,22 @@ class EventEmitter {
|
|
|
12
18
|
*/
|
|
13
19
|
subscribe(eventName, callback) {
|
|
14
20
|
const id = Symbol(callback.toString());
|
|
15
|
-
this.subscriptions.set(eventName,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
this.subscriptions.set(eventName, [
|
|
22
|
+
...this.subscriptions.get(eventName),
|
|
23
|
+
{ id, callback },
|
|
24
|
+
]);
|
|
25
|
+
return () => this.subscriptions.set(eventName, this.subscriptions
|
|
26
|
+
.get(eventName)
|
|
27
|
+
.filter(({ id: subscriptionId }) => subscriptionId !== id));
|
|
21
28
|
}
|
|
22
29
|
/**
|
|
23
30
|
* @param {string} eventName
|
|
24
31
|
* @param {Array<any>} args
|
|
25
32
|
*/
|
|
26
33
|
emit(eventName, ...args) {
|
|
27
|
-
|
|
34
|
+
this.subscriptions
|
|
35
|
+
.get(eventName)
|
|
36
|
+
.forEach(({ callback }) => callback(...args));
|
|
28
37
|
}
|
|
29
38
|
}
|
|
30
39
|
exports.EventEmitter = EventEmitter;
|