JSPM

@servie/events

3.0.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 184382
  • Score
    100M100P100Q181423F
  • License MIT

Tiny type-safe event emitter

Package Exports

  • @servie/events

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@servie/events) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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