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

@bscotch/emitter

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

@bscotch/emitter

Extension of eventemitter2, adding typed events.

  • 0.2.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
62
decreased by-25.3%
Maintainers
1
Weekly downloads
 
Created
Source

EventEmitter2 (with types!)

Existing EventEmitter libraries typically do not provide much support for indicating what events and payloads are available on a given emitter. This library adds that support to the popular EventEmitter2 library.

Usage

Events can be described as a string (when just the event name is needed because there is no expected payload) or as an object with a payload:

import {createEmitter} from '@bscotch/emitter';

type MySimpleEvent = "my-simple-event";
interface MyComplexEvent {
    name: "my-complex-event";
    // The payload must be a *tuple* of the arguments that will
    // be passed to the event handler.
    payload: [arg0: string, arg1: { foo: number }];
}

// For best results, events should be collected in a tuple type
type MyEvents = [MySimpleEvent, MyComplexEvent];

const emitter = createEmitter<MyEvents>();

// Now you'll have typed emitter methods!
emitter.on('my-simple-event', () => {});
emitter.emit('my-simple-event');

emitter.on('my-complex-event', (arg0, arg1) => {}); // <- callback is typed!
emitter.emit('my-complex-event', 'foo', { foo: 1 }); // <- arguments are typed!

Keywords

FAQs

Package last updated on 25 Aug 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