Socket
Book a DemoInstallSign in
Socket

@baileyherbert/events

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@baileyherbert/events

A strongly typed event emitter.

1.0.1
latest
Source
npmnpm
Version published
Weekly downloads
48
-34.25%
Maintainers
1
Weekly downloads
 
Created
Source

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 <T>:

import { EventEmitter } from '@baileyherbert/events';

export class Chat extends EventEmitter<Events> {
	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. 👍

const chat = new Chat();

chat.on('connected', username => {});
chat.on('disconnected', username => {});
chat.on('chat', (username, message) => {});

Keywords

typescript

FAQs

Package last updated on 02 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.