eventsts
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

eventsts

A simple, typed event emitter. Use it like this:

import { EventSource } from 'eventsts'

interface MyEventType {
  name: string;
}

const emitter = new EventSource<MyEventType>();

// Watch for messages
emitter.on(message => {
  // message's type is MyEventType
  console.log("name", message.name);
});

// Emit a message
emitter.emit({name: 'something'});

Other things you can do:

emitter.once(message => {
  // called only once on the next event
})

emitter.untilTrue(message => {
  // called until this returns true
  if (message.name === 'bob') {
    // disable this handler
    return true;
  }
})

emitter.removeAllListeners();

function someListener(message:MyEventType) {
  console.log('some listener', message.name);
}
emitter.on(someListener);
emitter.removeListener(someListener);

Register an error handler (by default it will throw errors):

emitter.onError(err => {
  console.error(err);
})

Publishing

npm publish

Readme

Keywords

none

Package Sidebar

Install

npm i eventsts

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

16.9 kB

Total Files

8

Last publish

Collaborators

  • iffycan