@servie/events
Version:
Tiny type-safe event emitter
68 lines (49 loc) • 1.95 kB
Markdown
[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Build status][build-image]][build-url]
[![Build coverage][coverage-image]][coverage-url]
[![Bundle size][bundlephobia-image]][bundlephobia-url]
> Tiny type-safe event emitter.
```
npm install @servie/events --save
```
```ts
import { Emitter, once, ALL_EVENTS } 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);
const off = events.on("test", listener);
// Emit an event.
events.emit("test", true);
// Listen to _all_ events, i.e. for debugging.
const eachListener = ({ type, args }) => console.log(type, args);
const off = events.on(ALL_EVENTS, eachListener);
// "Once" utility.
const runOnce = (...args) => console.log(args);
once(events, "test", runOnce);
// Remove the listener.
off();
```
This project is written using [TypeScript](https://github.com/Microsoft/TypeScript) and publishes the definitions directly to NPM.
MIT
[]: https://img.shields.io/npm/v/@servie/events
[]: https://npmjs.org/package/@servie/events
[]: https://img.shields.io/npm/dm/@servie/events
[]: https://npmjs.org/package/@servie/events
[]: https://img.shields.io/github/workflow/status/serviejs/events/CI/main
[]: https://github.com/serviejs/events/actions/workflows/ci.yml?query=branch%3Amain
[]: https://img.shields.io/codecov/c/gh/serviejs/events
[]: https://codecov.io/gh/serviejs/events
[]: https://img.shields.io/bundlephobia/minzip/@servie/events.svg
[]: https://bundlephobia.com/result?p=@servie/events