glob-bus

A hierarchical pub/sub event manager with namespace wildcard support

Usage no npm install needed!

<script type="module">
  import globBus from 'https://cdn.skypack.dev/glob-bus';
</script>

README

glob-bus

249 byte pub/sub event bus with namespaced wildcard support.

NPM version NPM downloads Follow JamieMason on GitHub Follow fold_left on Twitter

Installation

npm install --save glob-bus

Example

In this example, all three listeners will be invoked.

import { globBus } from 'glob-bus';

type Event =
  | { type: 'basket.product.add'; id: number }
  | { type: 'basket.product.remove'; id: number };

const { on, send } = globBus<Event>();

on('*', (event: Event) => console.log(1, event));
on('basket.*', (event: Event) => console.log(2, event));
on('basket.product.*', (event: Event) => console.log(3, event));

send({ type: 'basket.product.add', id: 123 });

The on function returns a function to unregister the given listener:

import { globBus } from 'glob-bus';

const { on, send } = globBus();
const off = on('basket.product.*', fn);

off();