Native node.js event emitter with generics for typescript.
$ npm install --save @imba/event-emittter
or with yarn
$ yarn add @imba/event-emitter
Instead of extending the EventEmitter
from node.js, create property for each event. It's similar in usage to what
angular is doing with rxjs.
import {EventEmitter} from '@imba/event-emitter';
class MyClassWithEvents
{
public onStart: EventEmitter<string> = new EventEmitter<string>();
public onFinish: EventEmitter<string> = new EventEmitter<string>();
public run(): void
{
this.onStart.emit('started');
this.onFinish.emit('finished');
}
}
const instance = new MyClassWithEvents;
instance.onStart.subscribe((msg) => {
console.log(msg); // output: started
});
instance.onFinish.subscribe((msg) => {
console.log(msg); // output: finished
});
instance.run();