Socket
Socket
Sign inDemoInstall

strict-event-emitter

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strict-event-emitter

Type-safe implementation of EventEmitter for browser and Node.js


Version published
Weekly downloads
4.5M
decreased by-2.07%
Maintainers
1
Weekly downloads
 
Created

What is strict-event-emitter?

The 'strict-event-emitter' npm package is a TypeScript-first event emitter library that provides strong typing for event names and payloads. It ensures type safety and helps avoid common mistakes when working with events in TypeScript projects.

What are strict-event-emitter's main functionalities?

Basic Event Emission

This feature allows you to emit and listen to events with strong typing. The event name and payload are type-checked, ensuring that only valid events and payloads are used.

const { StrictEventEmitter } = require('strict-event-emitter');

interface Events {
  greet: string;
}

const emitter = new StrictEventEmitter<Events>();
emitter.on('greet', (message) => {
  console.log(message);
});
emitter.emit('greet', 'Hello, world!');

Typed Event Listeners

This feature allows you to define multiple events with different payload types. The event listeners are type-checked to ensure they handle the correct payload structure.

const { StrictEventEmitter } = require('strict-event-emitter');

interface Events {
  greet: string;
  farewell: { message: string; code: number };
}

const emitter = new StrictEventEmitter<Events>();
emitter.on('farewell', (data) => {
  console.log(data.message, data.code);
});
emitter.emit('farewell', { message: 'Goodbye!', code: 200 });

Removing Event Listeners

This feature allows you to remove specific event listeners, ensuring that they no longer respond to emitted events. This is useful for cleaning up resources and avoiding memory leaks.

const { StrictEventEmitter } = require('strict-event-emitter');

interface Events {
  greet: string;
}

const emitter = new StrictEventEmitter<Events>();
const greetListener = (message: string) => {
  console.log(message);
};
emitter.on('greet', greetListener);
emitter.off('greet', greetListener);
emitter.emit('greet', 'Hello, world!'); // No output, listener removed

Other packages similar to strict-event-emitter

FAQs

Package last updated on 21 Sep 2023

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