publer

A tiny, type-safe pubsub library

Usage no npm install needed!

<script type="module">
  import publer from 'https://cdn.skypack.dev/publer';
</script>

README

GitHub Workflow Status Codecov GitHub issues GitHub npm npm bundle size

Publer

publer is a tiny, type-safe pubsub library written in typescript.

Installation

npm install publer

Usage

import { publer } from "publer";

interface LoginPayload {
  token: string;
  remember?: boolean;
}

interface Events {
  login: LoginPayload;
  logout: void; // no payload
}

const [pub, sub] = publer<Events>();

// returns a cleanup function to remove listener
const unsub = sub("login", ({ token, remember }) => {
  // payload is inferred
});

// fails without payload, must be provided
pub("login", { token: "foobar" });

// fails with payload
pub("logout");