JSPM

  • Created
  • Published
  • Downloads 125484
  • Score
    100M100P100Q158974F
  • License MIT

Type safe replacement for node's EventEmitter

Package Exports

  • evt
  • evt/dist/tools/typeSafety
  • evt/dist/tools/typeSafety/assert

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 (evt) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

💧Type safe replacement for Node's EventEmitter💧

Home - Documentation


'evt' is intended to be a replacement for Node's 'events' and a lighter alternative to 'rxjs'.
It enable and encourage functional programming and makes heavy use of typescript's type inference features to provide type safety while keeping things concise and elegant 🍸.

Browserify friendly:

  • No polyfills needed ✅
  • Transpiled down to ES3 ✅
  • Light-weight, no third party dependencies ✅

TLDR;

import { Evt } from "evt";

const evtText = new Evt<string>();
const evtTime = new Evt<number>();

evtText.attach(text => console.log(text));
evtTime.attachOnce(time => console.log(time));

evtText.post("hi!");
evtTime.post(123);
evtTime.post(1234);

OR

import { Evt, to } from "evt";

const evt = new Evt<
    [ "text",  string ] | 
    [ "time",  number ]
>();

evt.$attach(to("text"), text => console.log(text));
evt.$attachOnce(to("time"), time => console.log(time));

evt.post(["text", "hi!"]);
evt.post(["time", 123]);
evt.post(["time", 1234]);

Try it

Run hello world

Motivation

There are a lot of things that can't easily be done with EventEmitter:

  • Enforcing type safety.

  • Removing a particular listener when the callback is an anonymous function.

  • Adding a one-time listener for the next event that meets a condition.

  • Waiting (via a Promise) for one thing or another to happen.

    Example: waiting at most one second for the next message, stop waiting if the socket disconnects.

RxJS have its issues as well:

  • When chaining operators the type is often lost along the way as TypeScript struggle to keep track of the mutation / filtering being applied to the event flow.
  • Introduce (too) many abstractions/operators, combining them right can be challenging, even for seemingly straights forward control flows.

EVT is an attempt to solve all these issues while trying to remain as accessible as EventEmitter.


Get started