Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

@servie/events

1.0.0 • Public • Published

Events

NPM version NPM downloads Build status Test coverage Bundle size

Tiny type-safe event emitter.

Installation

npm install @servie/events --save

Usage

import { Emitter, once } from "@servie/events";
 
// Define an events interface using keys & argument tuples.
interface Events {
  test: [boolean];
  other: [string];
}
 
// Initialize an `Emitter` using your `Events` interface.
const events = new Emitter<Events>();
 
// Add or remove listeners.
const listener = (...args) => console.log(args);
events.on("test", listener);
events.off("test", listener);
 
// Emit an event.
events.emit("test", true);
 
// Listen to _all_ events, e.g. debugging.
const eachListener = ({ type, args }) => console.log(type, args);
events.each(eachListener);
events.none(eachListener);
 
// "Once" utility.
const onceListener = (...args) => console.log(args);
once(events, "test", onceListener);

TypeScript

This project is written using TypeScript and publishes the definitions directly to NPM.

License

MIT

Install

npm i @servie/events

DownloadsWeekly Downloads

15,827

Version

1.0.0

License

MIT

Unpacked Size

20.9 kB

Total Files

9

Last publish

Collaborators

  • avatar