Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typed-emitter

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typed-emitter

Strictly typed event emitter interface for TypeScript 3.

  • 0.2.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
106K
decreased by-63.24%
Maintainers
1
Weekly downloads
 
Created

What is typed-emitter?

The 'typed-emitter' npm package provides a strongly-typed event emitter for TypeScript. It allows developers to define and use event emitters with type safety, ensuring that the events and their associated data are correctly typed.

What are typed-emitter's main functionalities?

Basic Event Emission

This feature allows you to define and emit basic events with type safety. The event 'event1' is defined to accept a string message, and the emitter ensures that only strings are passed when emitting this event.

const { TypedEmitter } = require('typed-emitter');

interface MyEvents {
  event1: (message: string) => void;
}

const emitter = new TypedEmitter<MyEvents>();
emitter.on('event1', (message) => {
  console.log(message);
});
emitter.emit('event1', 'Hello, World!');

Multiple Event Listeners

This feature demonstrates how you can attach multiple listeners to a single event. Both listeners will be called when 'event1' is emitted, and they will receive the same message.

const { TypedEmitter } = require('typed-emitter');

interface MyEvents {
  event1: (message: string) => void;
  event2: (count: number) => void;
}

const emitter = new TypedEmitter<MyEvents>();
emitter.on('event1', (message) => {
  console.log('Listener 1:', message);
});
emitter.on('event1', (message) => {
  console.log('Listener 2:', message);
});
emitter.emit('event1', 'Hello, World!');

Removing Event Listeners

This feature shows how to remove an event listener. The listener is first added to 'event1', then removed using the 'off' method. The second emission of 'event1' does not trigger the listener.

const { TypedEmitter } = require('typed-emitter');

interface MyEvents {
  event1: (message: string) => void;
}

const emitter = new TypedEmitter<MyEvents>();
const listener = (message: string) => {
  console.log(message);
};
emitter.on('event1', listener);
emitter.emit('event1', 'Hello, World!');
emitter.off('event1', listener);
emitter.emit('event1', 'This will not be logged');

Other packages similar to typed-emitter

Keywords

FAQs

Package last updated on 21 Oct 2019

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc