# js-event-bus

> Event bus for your Javascript applications

Latest version **1.1.1** (published 2021-08-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install js-event-bus
pnpm add js-event-bus
yarn add js-event-bus
bun add js-event-bus
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2021-08-18 |
| First published | 2018-07-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 105 |
| Author | Boris Cerati |
| Maintainers | bobo_dev |
| Keywords | event, bus, node, nodejs |

## Links

- npm: https://www.npmjs.com/package/js-event-bus
- Repository: https://github.com/bcerati/js-event-bus
- Homepage: https://github.com/bcerati/js-event-bus#readme
- Issues: https://github.com/bcerati/js-event-bus/issues
- npm.io page: https://npm.io/package/js-event-bus

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2021-08-18
- 1.1.0 — 2021-05-10
- 1.0.3 — 2020-04-23
- 1.0.2 — 2020-04-23
- 1.0.1 — 2020-02-28
- 1.0.0 — 2019-04-10
- 0.0.5 — 2019-03-02
- 0.0.4 — 2019-03-02
- 0.0.3 — 2018-07-20
- 0.0.2 — 2018-07-18
- 0.0.1 — 2018-07-17

## README

# js-event-bus
Simple Event Bus library built for any JavaScript application.

## Installation

### Using npm
```
npm i js-event-bus --save
```

### Using yarn
```
yarn add js-event-bus
```

## Usage
This library was built so you can use it in any JS application like Node.js apps, browser apps etc. The API is always the same.

### Importing in Node.js application
If you want to use it in your Node.js apps you can import the library like this:

```js
const eventBus = require('js-event-bus')();
```

### Importing in browser application
If you want to use it in your Browser apps you can import the library like this:

```html
<body>

  <div>Put your content here</div>

  <script src="/lib/js-event-bus/lib/js-event-bus.min.js"></script>
  <script>
    const eventBus = new EventBus();
  </script>
</body>
```

### Api of the library

#### Register to an event
```js
  eventBus.on('my-event', function () {
    console.log('Inside `my-event`');
  });
```
With this code, each time `my-event` is emited this function will be executed.

#### Register only one time to an event
```js
  eventBus.once('my-event', function () {
    console.log('Inside `my-event`. It\'ll be executed only one time!');
  });
```
With this code, when `my-event` is emited this function will be executed. The next triggers of this event won't execute the callback because it is a one time event.

#### Register several time to an event
```js
  eventBus.exactly(3, 'my-event', function () {
    console.log('Inside `my-event`. It\'ll be executed only 3 times!');
  });
```
With this code, when `my-event` is emited this function will be executed with a maximum of triggers of 3.

#### Register using wildcards
You can use wildcards to register listeners using a specific pattern.

```js
  eventBus.on('my-event.*', function () {
    console.log('Inside `my-event.*`');
  });
```
The callback will be executed with the events like `my-event.x`.

* `my-event.x` **will** trigger the callback ;
* `my-event.y` **will** trigger the callback ;
* `my-event` **will not** trigger the callback ;
* `my-event.x.y` **will not** trigger the callback ;

You can also use multiple wildcards to register listeners using a specific pattern.

```js
  eventBus.on('my-event.*.name.**', function () {
    console.log('my-event.*.name.**`');
  });
```
The callback will be executed with the events like `my-event.a.name.b.c`.

* `my-event.a.name.b.c` **will** trigger the callback ;
* `my-event.a.name.b` **will** trigger the callback ;
* `my-event.name.b` **will not** trigger the callback ;


#### Emit an event
You can emit an event by calling the `emit` function. The arguments are the following:

- the name of the event ;
- the context with which it will be fired ;
- ... all the arguments.

Here are some examples:

```js
  eventBus.emit('my-event');
  eventBus.emit('my-event', null, 'a', 'b'); // your callback sould be function (a, b) { ... }
  eventBus.emit('my-event', new SomeObject, 'a', 'b'); // your callback sould be function (a, b) { ... } and `this` will be set to the context of `SomeObject`
```

#### Detach an event
```js
  var callbackForMyEvent = function () {
    console.log('Inside `my-event`.');
  };

  eventBus.on('my-event', callbackForMyEvent);

  eventBus.emit('my-event');

  eventBus.detach('my-event', callbackForMyEvent);
```
This code will emit the event `my-event` and then detach the given callback for this event. So it'll not be executed anymore.

#### Detach an event for all the callbacks that have been set before
```js
  eventBus.detach('my-event');
```
This code will remove the event `my-event` from the event bus.

#### Detach all the events  created in the event bus
```js
  eventBus.detachAll();
```
This code will remove the event `my-event` from the event bus.

#### Remove an event
```js
  eventBus.on('my-event', function () {
    console.log('Inside `my-event`.');
  });

  eventBus.emit('my-event');

  eventBus.die('my-event');
```
This code will emit the event `my-event` and then detach all the callbacks for this event. So any of them won't be executed anymore.

Note that `off` is an alias of `die`.

# License
MIT

---
_Source: https://npm.io/package/js-event-bus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
