Gathering detailed insights and metrics for tiny-typed-emitter2
Gathering detailed insights and metrics for tiny-typed-emitter2
npm install tiny-typed-emitter2
Typescript
Module System
Node Version
NPM Version
64.5
Supply Chain
96.4
Quality
75
Maintenance
100
Vulnerability
100
License
JavaScript (100%)
Total Downloads
7,493
Last Day
20
Last Week
73
Last Month
279
Last Year
4,515
27 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
tiny-typed-emitter2@1.0.0
Unpacked Size
6.22 kB
Size
2.66 kB
File Count
5
NPM Version
8.5.5
Node Version
16.15.0
Cumulative downloads
Total Downloads
Last day
17.6%
20
Compared to previous day
Last week
7.4%
73
Compared to previous week
Last month
-45.2%
279
Compared to previous month
Last year
115.7%
4,515
Compared to previous year
1
2
This is a fork of tiny-typed-emitter
for EventEmitter2
.
Have your events and their listeners type-checked with no overhead.
Simply add the dependency using npm:
1$ npm i tiny-typed-emitter2
or using yarn:
1$ yarn add tiny-typed-emitter2
1import { TypedEmitter } from 'tiny-typed-emitter2';
1interface MyClassEvents { 2 'added': (el: string, wasNew: boolean) => void; 3 'deleted': (deletedCount: number) => void; 4}
EventEmitter
:
1class MyClass extends TypedEmitter<MyClassEvents> { 2 constructor() { 3 super(); 4 } 5}
1const emitter = new TypedEmitter<MyClassEvent>();
To use with generic events interface:
1interface MyClassEvents<T> { 2 'added': (el: T, wasNew: boolean) => void; 3} 4 5class MyClass<T> extends TypedEmitter<MyClassEvents<T>> { 6 7}
The type of eventNames()
is a superset of the actual event names to make
subclasses of a TypedEmitter
that introduce different events type
compatible. For example the following is possible:
1class Animal<E extends ListenerSignature<E>=ListenerSignature<unknown>> extends TypedEmitter<{spawn: () => void} & E> { 2 constructor() { 3 super(); 4 } 5} 6 7class Frog<E extends ListenerSignature<E>> extends Animal<{jump: () => void} & E> { 8} 9 10class Bird<E extends ListenerSignature<E>> extends Animal<{fly: () => void} & E> { 11} 12 13const animals: Animal[] = [new Frog(), new Bird()];
This library is compatible with @nestjs/event-emitter
which uses EventEmitter2
under the hood. To use follow the instructions from:
https://docs.nestjs.com/techniques/events
With the following changes:
EventEmitter2
. For example:constructor(private eventEmitter: EventEmitter2) {}
becomes:
constructor(private eventEmitter: TypedEmitter<MyEvents>) {}
@OnEvent
method decoratorInstead of doing this:
@OnEvent('some.event')
myHandler(event: MyCustomEventType) {}
You'll need to do this:
constructor(events: TypedEmitter<MyEvents>) {
events.on('some.event', (event) => {
// correct event type will automatically be available
});
}
Library adds no overhead. All it does is it simply reexports renamed EventEmitter2
with customized typings.
You can check lib/index.js to see the exported code.
No vulnerabilities found.
No security vulnerabilities found.