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 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('test-event', () => console.log('test-event handler called!'));
31
- eventEmitter.subscribe('test-event', () => console.log('another test-event handler called!'))
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('test-event')
34
- // test-event handler called!
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
@@ -1,11 +1,14 @@
1
1
  export declare type EventCallback<T = void> = (...args: Array<any>) => T;
2
- export declare type Subscriptions = Map<string, Array<{
2
+ export declare class Subscriptions extends Map<string, Array<{
3
3
  id: symbol;
4
4
  callback: EventCallback;
5
- }>>;
6
- export declare type Unsubscribe = {
7
- unsubscribe: () => void;
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();
@@ -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 Map();
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, 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
+ 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
- (this.subscriptions.get(eventName) || []).forEach(({ callback }) => callback(...args));
34
+ this.subscriptions
35
+ .get(eventName)
36
+ .forEach(({ callback }) => callback(...args));
28
37
  }
29
38
  }
30
39
  exports.EventEmitter = EventEmitter;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chiisai-event-emitter",
3
- "version": "1.0.1",
3
+ "version": "2.0.0",
4
4
  "description": "A minimal event emitter.",
5
5
  "main": "./lib/index.js",
6
6
  "files": [