0.1.3 • Published 3 years ago

event-emitter-typesafe v0.1.3

Weekly downloads
2
License
MIT
Repository
github
Last release
3 years ago

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.

0.1.3

3 years ago

0.1.2

3 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.1

4 years ago