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

@metamask/safe-event-emitter

Package Overview
Dependencies
Maintainers
11
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/safe-event-emitter

An EventEmitter that isolates the emitter from errors in handlers

  • 3.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
532K
decreased by-1.77%
Maintainers
11
Weekly downloads
 
Created

What is @metamask/safe-event-emitter?

@metamask/safe-event-emitter is a lightweight event emitter library designed to be a safer alternative to the Node.js EventEmitter. It provides a simple API for emitting and listening to events, with additional safety checks to prevent common issues such as memory leaks and unhandled errors.

What are @metamask/safe-event-emitter's main functionalities?

Basic Event Emission

This feature allows you to emit and listen to events. The code sample demonstrates how to create an instance of SafeEventEmitter, set up an event listener, and emit an event.

const SafeEventEmitter = require('@metamask/safe-event-emitter');
const emitter = new SafeEventEmitter();
emitter.on('event', (message) => {
  console.log(message);
});
emitter.emit('event', 'Hello, world!');

Error Handling

This feature provides built-in error handling. The code sample shows how to listen for 'error' events and handle them appropriately.

const SafeEventEmitter = require('@metamask/safe-event-emitter');
const emitter = new SafeEventEmitter();
emitter.on('error', (err) => {
  console.error('An error occurred:', err);
});
emitter.emit('error', new Error('Something went wrong!'));

Removing Listeners

This feature allows you to remove event listeners. The code sample demonstrates how to add a listener, remove it, and then emit an event that will not be logged because the listener has been removed.

const SafeEventEmitter = require('@metamask/safe-event-emitter');
const emitter = new SafeEventEmitter();
function listener(message) {
  console.log(message);
}
emitter.on('event', listener);
emitter.removeListener('event', listener);
emitter.emit('event', 'This will not be logged.');

Other packages similar to @metamask/safe-event-emitter

FAQs

Package last updated on 18 Oct 2024

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