1.1.4 • Published 5 years ago

yet-another-unique-name-ts-event-emitter v1.1.4

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

NPM Version

ts-event-emitter

Typed EventEmitter implemented with tsargs, based on nodejs EventEmitter.

Install & use

npm i yet-another-unique-name-ts-event-emitter
import { EventEmitter } from 'yet-another-unique-name-ts-event-emitter';

const events = new EventEmitter<{
    foo: (a: number, b: string) => void,
}>();

events.emit('foo', 123, 'hello world');

Feature

EventEmitter.emit's args is fully typed based on events map.

For foo event in example above, signature is: emit(eventName: string, a: number, b: string).

Api

EventEmitter<T> where T extends { [eventName]: Call signature }.

How it works?

Secret is ArgsN from tsargs:

emit<EventKey extends keyof EventMap>(
    event: EventKey,
    ...args: ArgsN<EventMap[EventKey]>
) {
    this.emitter.emit(event as string, ...args);
}

PS

Thats absolutely awesome that now on npm there is a lot of trash packages with similar name, but without needed functionality!

1.1.4

5 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago