npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@p-vbordei/typed-event-bus

v0.2.1

Published

A tiny, dependency-free, type-safe event bus that works in Node, browsers, and Deno.

Readme

typed-event-bus

ci

npm downloads bundle

A tiny, dependency-free, type-safe event bus. Works anywhere modern JS runs — Node, browsers, Deno, Bun, edge runtimes.

import { EventBus } from "@p-vbordei/typed-event-bus";

type Events = {
  "task.created": { id: string };
  "task.done":    { id: string; ms: number };
};

const bus = new EventBus<Events>();

const off = bus.on("task.created", ({ id }) => console.log("created", id));
bus.once("task.done", ({ id, ms }) => console.log("done", id, ms));

await bus.emit("task.created", { id: "abc" });
await bus.emit("task.done",    { id: "abc", ms: 142 });

off();

Install

npm install @p-vbordei/typed-event-bus

Works with Node 20+, browsers, Bun, Deno. ESM + CJS.

Why

Node's EventEmitter is untyped, ships only in Node, and is bigger than this whole package. typed-event-bus is ~120 lines, zero deps, runs everywhere, and gives you real TypeScript autocomplete for both event names and payload shapes.

Recipes

Decouple modules via events

// events.ts
import { EventBus } from "@p-vbordei/typed-event-bus";

type AppEvents = {
  "user.signup":  { userId: string; plan: "free" | "pro" };
  "user.cancel":  { userId: string };
  "billing.fail": { userId: string; reason: string };
};

export const events = new EventBus<AppEvents>();
// in auth module
events.emit("user.signup", { userId, plan: "free" });

// in email module (knows nothing about auth)
events.on("user.signup", async ({ userId, plan }) => {
  await sendWelcomeEmail(userId, plan);
});

// in metrics module
events.onAny((event, payload) => {
  metrics.inc(`event.${event.replace(".", "_")}`);
});

React-like "await next" pattern

import { EventBus } from "@p-vbordei/typed-event-bus";

type Events = { "auth.complete": { token: string } };
const bus = new EventBus<Events>();

// Anywhere in the app:
const { token } = await bus.waitFor("auth.complete", 30_000);  // 30s timeout

Bridge to native EventTarget / WebSocket

import { EventBus } from "@p-vbordei/typed-event-bus";

type Messages = { open: void; close: { code: number }; message: { data: string } };
const bus = new EventBus<Messages>();

ws.addEventListener("open",    () => bus.emit("open", undefined));
ws.addEventListener("close",   (e) => bus.emit("close", { code: e.code }));
ws.addEventListener("message", (e) => bus.emit("message", { data: e.data }));

// Consumer side gets typed events:
bus.on("message", ({ data }) => handle(JSON.parse(data)));

Cleanup on component unmount

import { useEffect } from "react";
import { events } from "./events";

function MyComponent() {
  useEffect(() => {
    const off = events.on("user.signup", ({ userId }) => {
      console.log("new user:", userId);
    });
    return off;  // off() runs on unmount
  }, []);
  return <div>...</div>;
}

Error isolation

import { EventBus } from "@p-vbordei/typed-event-bus";

const bus = new EventBus<Events>({
  onError: (err, event) => {
    logger.error(`listener for ${event} threw:`, err);
    // don't re-throw — other listeners should still run
  },
});

bus.on("task.created", () => { throw new Error("boom"); });
bus.on("task.created", () => { /* still runs even though prev threw */ });

API

new EventBus<E>(opts?: { onError?: (err, event) => void })

bus.on(event, listener) → unsubscribe()
bus.once(event, listener) → unsubscribe()
bus.onAny((event, payload) => ...) → unsubscribe()
bus.off(event, listener) → void
bus.emit(event, payload) → Promise<void>     // resolves after all async listeners settle
bus.waitFor(event, timeoutMs?) → Promise<payload>
bus.listenerCount(event) → number
bus.clear(event?) → void                      // clears one event, or everything

Listener errors

By default, listener errors throw. Pass onError to the constructor to collect them instead — sibling listeners keep running either way:

const bus = new EventBus<Events>({
  onError: (err, event) => console.error(`[${event}]`, err),
});

Both sync throw and async Promise rejections are routed through onError.

Async listeners

Listeners may return Promise<void>. emit() returns a promise that resolves after all async listeners settle. Useful when you need to await side-effects (e.g. in tests).

When to use this

  • ✅ Decoupling modules within a single process
  • ✅ React/Vue/Svelte event bridges
  • ✅ WebSocket / EventSource adapters
  • ✅ Anywhere you want pub/sub without dependencies
  • ❌ Cross-process pub/sub — use Redis, NATS, or another broker
  • ❌ Persistent event sourcing — this is in-memory

License

Apache-2.0 © Vlad Bordei