Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/baileyherbert/events
A strongly typed event emitter for Node.js and browsers.
https://github.com/baileyherbert/events
eventemitter events typed typescript
Last synced: 3 months ago
JSON representation
A strongly typed event emitter for Node.js and browsers.
- Host: GitHub
- URL: https://github.com/baileyherbert/events
- Owner: baileyherbert
- License: mit
- Created: 2021-11-16T05:44:21.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-20T15:36:35.000Z (10 months ago)
- Last Synced: 2024-07-12T01:53:43.296Z (4 months ago)
- Topics: eventemitter, events, typed, typescript
- Language: TypeScript
- Homepage: https://npmjs.com/@baileyherbert/events
- Size: 104 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Events
This is an alternative event emitter that works on all platforms. It allows you to specify the event types, and keeps
the `emit` method protected.```
npm install @baileyherbert/events
```## Example
Extend the `EventEmitter` class and provide an enumeration of tuples for ``:
```ts
import { EventEmitter } from '@baileyherbert/events';export class Chat extends EventEmitter {
protected _onUserConnected(user: User) {
this.emit('connected', user.username);
}
}type Events = {
/**
* These JSDoc comments will show up in intellisense too!
*/
chat: [username: string, message: string];
connected: [username: string];
disconnected: [username: string];
};
```Then you can listen to those events like usual, but with autocompleted event names and typed arguments. 👍
```ts
const chat = new Chat();chat.on('connected', username => {});
chat.on('disconnected', username => {});
chat.on('chat', (username, message) => {});
```