@yootil/events
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

events

A lightweight, typed event emitter implementation.

Installation

Install using npm/yarn

npm i @yootil/events

Usage

import { Events } from '@yootil/events';

const events = new Events('my-feature');

// or events.on();
events.subscribe('my-event', (data, event) => {
  // do something with the data
});

// Trigger the event
events.trigger('my-event', {
  foo: 'my super special data',
});

// Can be given types
const typedEvents = new Events<{
  'my-event': {
    foo: string,
  },
}>('typed-emitter');

typedEvents.subscribe('my-event', (data /* { foo: string } */, events /* 'my-event' */) => {
  // do something with the data, safely knowing that data.foo is a string
});

// This would raise a warning, as the data does not match the event type
typedEvents.trigger('my-event', 55)

If no type is given in the constructor type params new Event<YourType>(), then the type will be a generic string record (Record<string, any>).

Package Sidebar

Install

npm i @yootil/events

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

12.1 kB

Total Files

11

Last publish

Collaborators

  • mattbalmer