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

event-target-bus

v1.1.0

Published

Centralizes `EventTarget` subscriptions for creating shared subscription stores.

Readme

event-target-bus

A type-safe simple event redistribution bus implementation for EventTarget API.

Installation

pnpm add event-target-bus
yarn add event-target-bus
npm install event-target-bus

Why?

When working with EventTarget API, typically a Web/DOM API, you often need to add and remove event listeners. A small number of listeners on event targets (say a few to a few dozen) has negligible memory and dispatch cost in modern browsers. The cost to add/remove a listener and to call a few listeners when an event fires is very small in normal apps.

However, if you have a large number of listeners (say hundreds or more) on the same event target and event name, the cost can become significant.

This can happen in complex apps or when you have many components that need to listen to the same events. And it is especially true if you are creating a re-usable library/component (say a React/Vue hook/component, a custom web component, etc.) that will be used across the entire app.

In such cases, event-target-bus can be beneficial to have a single listener on the event target that redistributes the event to multiple subscribers. This way, you can reduce the overhead of adding/removing listeners and improve performance when dispatching events.

Usage

Basic Usage

import { createEventTargetBus } from 'event-target-bus';

const onlineBus = createEventTargetBus(
  window,
  'online' /** event name is typed and you get autocomplete and type checking */
);

const handleOnline = (event: Event) => { console.log('The browser is online!'); };

// subscribe to the event
const unsub = onlineBus.on(handleOnline);

// later, when you want to unsubscribe
ubsub();
// or
onlineBus.off(handleOnline);

const mqlBus = createEventTargetBus(window.matchMedia('(prefers-color-scheme: dark)'), 'change');

mqlBus.on((event /** event object is also typed and you will get proper typing `MediaQueryListEvent` here */) => {
  if (event.matches) {
    console.log('Dark mode is preferred!');
  } else {
    console.log('Light mode is preferred!');
  }
});

mqlBus.on((evt) => {});

// with multiple `on` calls, you will only result in one event listener actually attached to the EventTarget

Usage with React useSyncExternalStore

// React official docs `useSyncExternalStore` "Subscribing to a browser API" example, but
// implemented with `event-target-bus` for better performance when there are many subscribers
// https://react.dev/reference/react/useSyncExternalStore#subscribing-to-a-browser-api

import { useSyncExternalStore } from 'react';
import { createSyncExternalStoreSubscribe } from 'event-target-bus/react';

// Define this at module scope so every hook instance shares the same subscribe function
// and event target buses. event target (`window` in this case) is only read on the first
// client subscription, so evaluating this module during SSR is safe.
const subscribe = createSyncExternalStoreSubscribe(
  () => window,
  ['online', 'offline']
);

function getSnapshot() {
  return navigator.onLine;
}

function useOnline() {
  return useSyncExternalStore(subscribe, getSnapshot, () => false);
}

The helper only adapts event subscriptions. Snapshot caching and the server snapshot value remain the responsibility of the hook using it.

For dynamic targets, use the keyed variant. It returns the same subscribe function for the same key and lazily creates one target and bus per key:

import { useSyncExternalStore } from 'react';
import { createKeyedSyncExternalStoreSubscribe } from 'event-target-bus/react';

const getMediaQuerySubscribe = createKeyedSyncExternalStoreSubscribe(
  (query: string) => window.matchMedia(query),
  'change'
);

function useMediaQuery(query: string, serverValue: boolean) {
  return useSyncExternalStore(
    getMediaQuerySubscribe(query),
    () => window.matchMedia(query).matches,
    () => serverValue
  );
}

The keyed factory retains subscribe functions by key for its lifetime. Define it at module scope and use it with a bounded set of keys, such as application-defined media queries.


event-target-bus © Sukka, Released under the MIT License. Authored and maintained by Sukka with help from contributors (list).

Personal Website · Blog · GitHub @SukkaW · Telegram Channel @SukkaChannel · Mastodon @[email protected] · Twitter @isukkaw · BlueSky @skk.moe