npm package: @rsol/ebus
Description: EventBus flexible event bus emit/pubsub
Latest version: 1.0.0

# EventBus

> EventBus flexible event bus emit/pubsub weighs less than 1KB. Wildcard support. Based on [Node's EventEmitter](https://nodejs.org/api/events.html#events_class_eventemitter)

## Table of Contents

*   [Install](#install)
*   [Usage](#usage)
*   [API](#api)
*   [License](#license)

## Install

This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.

```bash
$ npm install --save @rsol/ebus
```

Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else:

```js
// using ES6 modules
import EventBus, { getEventBus, removeEventBus, hasEventBus } from '@rsol/ebus'

// using CommonJS modules
const EventBus = require('@rsol/ebus')

```

## Usage

```js
const global = EventBus.getInstance();

const bus = EventBus.getInstance('Bus', {
  wildcard: true,
  maxListeners: 5
});

let i = 1;
const addF = (add: number) => i += add;
const subF = (sub: number) => i -= sub;
const mulF = (mul: number) => i *= mul;
const divF = (div: number) => i /= div;

bus
  .on('wildcard.add', addF)
  .on('wildcard.sub', subF)
  .on('wildcard.mul', mulF)
  .on('wildcard.div', divF)
  .emit('wildcard.*', 10);
console.log(i); // 1

bus.emit('wildcard.add', 2);
console.log(i); // 3

const result = bus
  .removeListener('wildcard.div', divF)
  .emit('wildcard.*', 10);
console.log(i); // 30

console.log(result);
/**
 * Map(3) {
 'wildcard.add' => true,
 'wildcard.sub' => true,
 'wildcard.mul' => true
 }
 */

console.log(bus.listenerCount('wildcard.*')); //3 

bus.removeAllListeners('wildcard.\[div|mul\]')

console.log(wildcardEb.eventNames());//['wildcard.add', 'wildcard.sub']

```

> To find more examples see `test` folder. For more methods see [API](#api)

## API

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### Table of Contents

*   [EventBus](#eventbus)
    *   [getBus](#getbus)
    *   [getBusName](#getbusname)
    *   [close](#close)
    *   [on](#on)
        *   [Parameters](#parameters)
    *   [once](#once)
        *   [Parameters](#parameters-1)
    *   [off](#off)
        *   [Parameters](#parameters-2)
    *   [addListener](#addlistener)
        *   [Parameters](#parameters-3)
    *   [removeListener](#removelistener)
        *   [Parameters](#parameters-4)
    *   [removeAllListeners](#removealllisteners)
        *   [Parameters](#parameters-5)
    *   [setMaxListeners](#setmaxlisteners)
        *   [Parameters](#parameters-6)
    *   [getMaxListeners](#getmaxlisteners)
    *   [listeners](#listeners)
        *   [Parameters](#parameters-7)
    *   [emit](#emit)
        *   [Parameters](#parameters-8)
    *   [eventNames](#eventnames)
    *   [matchedEventNames](#matchedeventnames)
        *   [Parameters](#parameters-9)
    *   [listenerCount](#listenercount)
        *   [Parameters](#parameters-10)
    *   [getInstance](#getinstance)
        *   [Parameters](#parameters-11)
    *   [hasBus](#hasbus)
        *   [Parameters](#parameters-12)
*   [getEventBus](#geteventbus)
    *   [Parameters](#parameters-13)
*   [removeEventBus](#removeeventbus)
    *   [Parameters](#parameters-14)
*   [hasEventBus](#haseventbus)
    *   [Parameters](#parameters-15)

### EventBus

EventBus flexible event bus emit/pubsub

#### getBus

Returns **EventEmitter**&#x20;

#### getBusName

Returns **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;

#### close

Remove EventBus instance and corresponding EventEmitter

#### on

Alias for `addListener(eventName, listener)` [addListener](addListener)

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;
*   `listener` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**&#x20;

Returns **[EventBus](#eventbus)**&#x20;

#### once

Adds a **one-time**`listener` function for the event named `eventName`. The
next time `eventName` is triggered, this listener is removed and then invoked

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;
*   `listener` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**&#x20;

Returns **[EventBus](#eventbus)**&#x20;

#### off

Alias for `removeListener(eventName, listener)` [removeListener](removeListener)

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;
*   `listener` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**&#x20;

Returns **[EventBus](#eventbus)**&#x20;

#### addListener

Adds the `listener` function to the end of the listeners array for the
event named `eventName`. No checks are made to see if the `listener` has
already been added. Multiple calls passing the same combination of `eventName`
and `listener` will result in the `listener` being added, and called,
multiple times

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;
*   `listener` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**&#x20;

Returns **[EventBus](#eventbus)**&#x20;

#### removeListener

Removes the specified `listener` from the listener array for the event named`eventName`

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;
*   `listener` **[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)**&#x20;

Returns **[EventBus](#eventbus)**&#x20;

#### removeAllListeners

Removes all listeners, or those of the specified `eventName`

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;

Returns **void**&#x20;

#### setMaxListeners

By default, `EventEmitter`s will print a warning if more than `10` listeners are
added for a particular event. This is a useful default that helps to find
memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
modified for this specific `EventEmitter` instance

##### Parameters

*   `maxListeners` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;

Returns **[EventBus](#eventbus)**&#x20;

#### getMaxListeners

Returns the current max listener value for the `EventEmitter` which is either
set by `emitter.setMaxListeners(n)`

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;

#### listeners

Returns a copy of the array of listeners for the event named `eventName`

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)<[function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)>**&#x20;

#### emit

Synchronously calls each of the listeners registered for the event
named`eventName`, in the order they were registered, passing the
supplied arguments to each

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;
*   `args` **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<any>**&#x20;

Returns **[Map](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)<([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)), [boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)>**&#x20;

#### eventNames

Returns an array listing the events for which the emitter has registered
listeners. The values in the array are strings or `Symbol`s

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<EventName>**&#x20;

#### matchedEventNames

Returns a matched array listing the events for which the emitter has registered
listeners. The values in the array are strings or `Symbol`s

##### Parameters

*   `eventName` **EventName**&#x20;

Returns **[Array](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)\<EventName>**&#x20;

#### listenerCount

Returns the number of listeners listening for the event named `eventName`.
If `listener` is provided, it will return how many times the listener is found
in the list of the listeners of the event

##### Parameters

*   `eventName` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;

Returns **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)**&#x20;

#### getInstance

Get EventBus instance

##### Parameters

*   `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**  (optional, default `'GLOBAL'`)
*   `options` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)?**&#x20;

    *   `options.wildcard` **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Is use wildcard for emit, listenerCount and removeAllListeners methods (optional, default `false`)
    *   `options.maxListeners` **[number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Max number of listeners (optional, default `50`)

Returns **[EventBus](#eventbus)**&#x20;

#### hasBus

##### Parameters

*   `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**&#x20;

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**&#x20;

### getEventBus

Return specific EventEmitter by name

#### Parameters

*   `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**  (optional, default `'GLOBAL'`)

Returns **EventEmitter**&#x20;

### removeEventBus

Remove specific EventEmitter by name

#### Parameters

*   `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**  (optional, default `'GLOBAL'`)

Returns **void**&#x20;

### hasEventBus

Check if specific EventEmitter by name exists

#### Parameters

*   `name` **([string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String) | [Symbol](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol))**  (optional, default `'GLOBAL'`)

Returns **[boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**&#x20;

## License

[MIT License](https://opensource.org/licenses/MIT) © [Jason Miller](https://jasonformat.com/)