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

event-emitter-typesafe

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

event-emitter-typesafe

Simple event emitter working well with typescript

  • 0.1.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

event-emitter-typesafe

This package is meant to give you an easy to use way of defining an event emitter which is typesafe. Those definitions can be either applied by mixin or by extension of the EventEmitter class.

In either way those classes offer the three main methods add, remove and dispatch and several alias. The API documentation is available at: https://feirell.github.io/event-emitter-typesafe/.

usage

extending

The easiest way is to just extend the provided EventEmitter.

import {EventEmitter} from "event-emitter-typesafe";

// define all available events by their name and their structure
interface ExampleEvents {
    'example-a': { data: number },
    'example-b': { data: number },
    'example-c': { data: number },
    'example-d': { data: number }
}

// your class you want to extend to a event emitter
class Example extends EventEmitter<ExampleEvents> {
}

const e = new Example();

// add, remove and emit have their correct typings attached
e.once('example-c', () => console.log('example c was emitted'));
e.addEventListener('example-c', (ev) => console.log(ev.data));

// the first and the second listener will be called
e.emit('example-c', {data: 12});

// only the second listener will be called since the other one detached itself
// dispatch is just an alias for emit
e.dispatch('example-c', {data: 42});

e.on('example-a', () => { });

You can find this in examples\example-extending.ts

mixin

You can also use the second option which leverages TypeScript mixins which allow you to provide the functionality off the EventEmitter without extending it. This can be useful if you already are extending another class. Mixins results in pretty much the same type situation as you would have with extension.

import {EventEmitterInt, makeEventEmitter} from "event-emitter-typesafe";

class SomeOtherClass {}

// your class you want to extend to a event emitter but which also extends another class
class Example extends SomeOtherClass {}

// define all available events by their name and their structure
interface ExampleEvents {
    'example-a': { data: number },
    'example-b': { data: number },
    'example-c': { data: number },
    'example-d': { data: number }
}

// use a mixin to extend the type definition of the Example class
// typescript will add the event definitions to this class type definition
interface Example extends EventEmitterInt<ExampleEvents> {}

// actually add the implementation to the Example prototype
makeEventEmitter(Example);

// usage is transparent
const e = new Example();

// add, remove and emit have their correct typings attached
e.addEventListener('example-c', (ev) => console.log(ev.data));
e.dispatch('example-c', {data: 12});

You can find this in examples\example-mixin.ts

standalone

You could always just create an instance of the EventEmitter instead of extending it.

similar

The package @servie/events is quite similar but does not provide a mixin option and some of the alias.

Keywords

FAQs

Package last updated on 06 Jan 2021

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