@servie/events
Tiny type-safe event emitter
Last updated 5 years ago by blakeembrey .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @servie/events 
SYNC missed versions from official npm registry.

Events

NPM version NPM downloads Build status Build coverage Bundle size

Tiny type-safe event emitter.

Installation

npm install @servie/events --save

Usage

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();

TypeScript

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

License

MIT

Current Tags

  • 3.0.0                                ...           latest (5 years ago)

4 Versions

  • 3.0.0                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (0)
None
Dev Dependencies (6)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |