glob-bus
v2.0.0
Published
A hierarchical pub/sub event manager with namespace wildcard support
Downloads
5
Maintainers
Readme
glob-bus
249 byte pub/sub event bus with namespaced wildcard support.
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();