@susisu/tyde
TypeScript icon, indicating that this package has built-in type declarations

0.4.0 • Public • Published

@susisu/tyde

CI

Typed event emitter

Installation

# npm
npm i @susisu/tyde
# yarn
yarn add @susisu/tyde
# pnpm
pnpm add @susisu/tyde

Usage

tyde provides API that is similar to EventEmitter and event-kit.

The simplest usage is subscribing events with .on() and emitting events with .emit().

import { Emitter } from "@susisu/tyde";

const emitter = new Emitter();

const subscription = emitter.on("change", value => {
  console.log(value);
});

emitter.emit("change", 42); // -> 42

Subscriptions can be unsubscribed by calling .unsubscribe().

subscription.unsubscribe();
emitter.emit("change", 0); // no output

The curried version of .on() is also available.

const onChange = emitter.on("change");
const subscription = onChange(value => { /* ... */ });

You can optionally restrict types of emitted values by giving a dictionary to Emitter's type parameter.

const emitter = new Emitter<{
  "change": number,
  "destroy": void,
}>();

emitter.emit("change", 42);     // ok
emitter.emit("destroy");        // ok
emitter.emit("foo", 42);        // type error: unknown event
emitter.emit("change", "test"); // type error: mismatched type

Difference from other event emitters

By design, tyde has some difference from other event emitter libraries:

  • No wildcards, because of the strongly typed interface.
  • Events are emitted asynchronously by default.
    • Usually event handlers should not care whether it is called synchronously or asynchronously.
    • There is also a synchronous version .emitSync(), but it is not generally recommended.
  • Invocation order of event handlers is not guaranteed in any way.
    • Specifying execution order in a less explicit way will lower the readability of code.
    • If you still want one, invoke functions sequentially in one event handler, or separate the event into multiple stages.

License

MIT License

Author

Susisu (GitHub, Twitter)

Readme

Keywords

none

Package Sidebar

Install

npm i @susisu/tyde

Weekly Downloads

51

Version

0.4.0

License

MIT

Unpacked Size

43.5 kB

Total Files

12

Last publish

Collaborators

  • susisu