glob-bus
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

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();

Package Sidebar

Install

npm i glob-bus

Weekly Downloads

1

Version

2.0.0

License

MIT

Unpacked Size

5.46 kB

Total Files

6

Last publish

Collaborators

  • fold_left