This package has been deprecated

Author message:

Package has been renamed to @prezly/events

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

1.0.1 • Public • Published

@prezly/typed-events

Version License Build Prettier

Event emitter with DOM-like EventTarget API & strong TypeScript typings.


Alternatives

There's this great mitt package which implements EventEmitter interface from node.js but it does not have strong typings. There's a PR pending with this feature, but there's been no activity for 7 months. Perhaps in the future we might not want to maintain this @prezly/typed-events package. But on the other hand, we should think twice before using a package where last PR got merged over half year ago.

Example

Demo: https://codesandbox.io/s/prezlytyped-events-demo-2k7pd

import Events from '@prezly/typed-events';

const ref = { current: 0 };

const events = new Events<{
    add: number;
    substract: number;
    log: never; // "never" means that event has no payload
}>();

const add = (value: number) => {
    ref.current += value;
};

const substract = (value: number) => {
    ref.current -= value;
};

const log = () => {
    console.log(ref.current);
};

// Attach event handlers
events.addEventListener('add', add);
events.addEventListener('substract', substract);
const removeLogListener = events.addEventListener('log', log);

// Dispatch events
events.dispatchEvent('add', 2);
events.dispatchEvent('substract', 5);
events.dispatchEvent('log');

// Remove event handlers
events.removeEventListener('add', add);
events.removeEventListener('substract', substract);
removeLogListener(); // this is another way to unsubscribe event listener

Brought to you by Prezly.

Readme

Keywords

none

Package Sidebar

Install

npm i @prezly/typed-events

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

12.6 kB

Total Files

20

Last publish

Collaborators

  • mohammadxali
  • francis-prezly
  • pestaa
  • kudlik
  • digitalbase
  • e1npm
  • satoav
  • makanda