chiisai-event-emitter 1.0.0 → 1.0.2
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 +7 -7
- package/lib/EventEmitter.d.ts +23 -0
- package/lib/EventEmitter.js +30 -0
- package/lib/index.d.ts +1 -23
- package/lib/index.js +15 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -23,7 +23,7 @@ npm install chiisai-event-emitter
|
|
|
23
23
|
## Usage
|
|
24
24
|
|
|
25
25
|
```ts
|
|
26
|
-
import
|
|
26
|
+
import { EventEmitter } from 'chiisai-event-emitter';
|
|
27
27
|
|
|
28
28
|
const eventEmitter = new EventEmitter();
|
|
29
29
|
|
|
@@ -35,16 +35,16 @@ eventEmitter.emit('test-event')
|
|
|
35
35
|
// another test-event handler called!
|
|
36
36
|
```
|
|
37
37
|
|
|
38
|
-
[build-img]:https://github.com/
|
|
39
|
-
[build-url]:https://github.com/
|
|
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
|
|
40
40
|
[downloads-img]:https://img.shields.io/npm/dt/chiisai-event-emitter
|
|
41
41
|
[downloads-url]:https://www.npmtrends.com/chiisai-event-emitter
|
|
42
42
|
[npm-img]:https://img.shields.io/npm/v/chiisai-event-emitter
|
|
43
43
|
[npm-url]:https://www.npmjs.com/package/chiisai-event-emitter
|
|
44
|
-
[issues-img]:https://img.shields.io/github/issues/
|
|
45
|
-
[issues-url]:https://github.com/
|
|
46
|
-
[codecov-img]:https://codecov.io/gh/
|
|
47
|
-
[codecov-url]:https://codecov.io/gh/
|
|
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
|
|
48
48
|
[semantic-release-img]:https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
|
49
49
|
[semantic-release-url]:https://github.com/semantic-release/semantic-release
|
|
50
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
CHANGED
|
@@ -1,23 +1 @@
|
|
|
1
|
-
export
|
|
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
|
-
}
|
|
1
|
+
export * from './EventEmitter';
|
package/lib/index.js
CHANGED
|
@@ -1,29 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
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
|
-
};
|
|
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]; } };
|
|
20
7
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
exports
|
|
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);
|