chiisai-event-emitter 2.0.0 → 2.1.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
@@ -24,6 +24,8 @@ npm install chiisai-event-emitter
24
24
 
25
25
  ### Subscribe to an Event
26
26
 
27
+ Use `EventEmitter.subscribe(eventName, callback)` to subscribe to an event.
28
+
27
29
  ```ts
28
30
  import { EventEmitter } from 'chiisai-event-emitter';
29
31
 
@@ -37,6 +39,8 @@ eventEmitter.emit('event')
37
39
 
38
40
  ### Unsubscibe
39
41
 
42
+ `EventEmitter.subscribe(eventName, callback)` method returns a function which can be called to unsubscribe the callback from the event.
43
+
40
44
  ```ts
41
45
  const unsubscibe = eventEmitter.subscribe('event', () => console.log('event-handler called!'));
42
46
  unsubscribe();
@@ -45,6 +49,20 @@ eventEmitter.emit('event')
45
49
  // (nothing happened)
46
50
  ```
47
51
 
52
+ ### Delete an Event
53
+
54
+ `EventEmitter.clear(eventName)` method removes an event and all callbacks subscibed to this event.
55
+
56
+ ```ts
57
+ eventEmitter.subscribe('event', () => console.log('event-handler called!'));
58
+ eventEmitter.subscribe('event', () => console.log('another event-handler called!'));
59
+ eventEmitter.clear('event')
60
+
61
+ eventEmitter.emit('event')
62
+ // (nothing happened)
63
+
64
+ ```
65
+
48
66
  [build-img]:https://github.com/bencelaszlo/chiisai-event-emitter/actions/workflows/release.yml/badge.svg
49
67
  [build-url]:https://github.com/bencelaszlo/chiisai-event-emitter/actions/workflows/release.yml
50
68
  [downloads-img]:https://img.shields.io/npm/dt/chiisai-event-emitter
@@ -1,4 +1,4 @@
1
- export declare type EventCallback<T = void> = (...args: Array<any>) => T;
1
+ export type EventCallback<T = void> = (...args: Array<any>) => T;
2
2
  export declare class Subscriptions extends Map<string, Array<{
3
3
  id: symbol;
4
4
  callback: EventCallback;
@@ -8,7 +8,7 @@ export declare class Subscriptions extends Map<string, Array<{
8
8
  callback: EventCallback;
9
9
  }[];
10
10
  }
11
- export declare type Unsubscribe = () => void;
11
+ export type Unsubscribe = () => void;
12
12
  export declare class EventEmitter {
13
13
  subscriptions: Subscriptions;
14
14
  constructor();
@@ -21,6 +21,13 @@ export declare class EventEmitter {
21
21
  /**
22
22
  * @param {string} eventName
23
23
  * @param {Array<any>} args
24
+ * @returns void
24
25
  */
25
26
  emit(eventName: string, ...args: Array<any>): void;
27
+ /**
28
+ *
29
+ * @param eventName eventName to be deleted
30
+ * @returns void
31
+ */
32
+ clear(eventName: string): void;
26
33
  }
@@ -29,11 +29,20 @@ class EventEmitter {
29
29
  /**
30
30
  * @param {string} eventName
31
31
  * @param {Array<any>} args
32
+ * @returns void
32
33
  */
33
34
  emit(eventName, ...args) {
34
35
  this.subscriptions
35
36
  .get(eventName)
36
37
  .forEach(({ callback }) => callback(...args));
37
38
  }
39
+ /**
40
+ *
41
+ * @param eventName eventName to be deleted
42
+ * @returns void
43
+ */
44
+ clear(eventName) {
45
+ this.subscriptions = new Subscriptions([...this.subscriptions].filter(([key]) => key !== eventName));
46
+ }
38
47
  }
39
48
  exports.EventEmitter = EventEmitter;
@@ -0,0 +1 @@
1
+ var e=this&&this.__createBinding||(Object.create?function(e,t,r,i){void 0===i&&(i=r);var n=Object.getOwnPropertyDescriptor(t,r);n&&!("get"in n?!t.__esModule:n.writable||n.configurable)||(n={enumerable:!0,get:function(){return t[r]}}),Object.defineProperty(e,i,n)}:function(e,t,r,i){void 0===i&&(i=r),e[i]=t[r]}),t=this&&this.__exportStar||function(t,r){for(var i in t)"default"===i||Object.prototype.hasOwnProperty.call(r,i)||e(r,t,i)};Object.defineProperty(exports,"__esModule",{value:!0}),t(require("./EventEmitter"),exports);
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "chiisai-event-emitter",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A minimal event emitter.",
5
5
  "main": "./lib/index.js",
6
6
  "files": [
7
7
  "lib/**/*"
8
8
  ],
9
9
  "scripts": {
10
- "build": "tsc --project tsconfig.build.json",
10
+ "tsc": "tsc --project tsconfig.build.json",
11
+ "minify": "terser ./lib/index.js -o ./lib/index.min.js --compress --mangle --module",
12
+ "build": "npm run tsc && npm run minify",
11
13
  "clean": "rm -rf ./lib/",
12
14
  "cm": "cz",
13
15
  "lint": "eslint ./src/ --fix",
@@ -58,9 +60,10 @@
58
60
  "lint-staged": "^13.2.1",
59
61
  "prettier": "^2.2.1",
60
62
  "semantic-release": "^21.0.1",
63
+ "terser": "^5.34.1",
61
64
  "ts-jest": "^27.0.5",
62
65
  "ts-node": "^10.2.1",
63
- "typescript": "^4.2.4"
66
+ "typescript": "^4.9.5"
64
67
  },
65
68
  "config": {
66
69
  "commitizen": {