@prismamedia/ts-typed-event-emitter
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Typescript Typed Event Emitter

A "typed" version of the native EventEmitter

Usage

import TypedEventEmitter from '@prismamedia/ts-typed-event-emitter';

// We declare the different types and the data they carry
type Events = {
  pre: [{ at: Date }],
  post: [{ took: number }],
}

// We "type" the data carried by the events like this :
const ee = new TypedEventEmitter<Events>();

// We can listen on events like this :
ee.on('pre', ({ at }) => console.log({ first: at }));
ee.on('pre', ({ at }) => console.log({ second: at }));
ee.on('post', ({ took }) => console.log({ took }));

// [...]

ee.emit('pre', { at: 2000 });
// -> { first: 2000 }
// -> { second: 2000 }

ee.emit('post', { took: 100 });
// -> { took: 100 }

A convenient "wait" methods is available :

// Wait for an event to be triggered
const eventData = await ee.wait('pre');

const eventDataOfTheFirstTriggeredEvent = await ee.wait('pre', 'post');

Readme

Keywords

none

Package Sidebar

Install

npm i @prismamedia/ts-typed-event-emitter

Weekly Downloads

0

Version

1.0.2

License

MIT

Unpacked Size

17 kB

Total Files

17

Last publish

Collaborators

  • prismamedia
  • yvann