npm package: @nabilk/minemit
Description: A minimal event emitter
Author: Nabil Kharrich
Latest version: 1.0.11

# minemit

A minimal event emitter

![license](https://img.shields.io/github/license/NabilKharrich/minemit)
![npm](https://img.shields.io/npm/v/minemit)

## Installation and basic usage

Install via <code>npm</code>

```bash
$ npm i minemit
```

Either import minemit completely or just the desired method

```javascript
import minemit, { add, emit } from "minemit";
```

Once imported, there is no need to create any instances. Minemit allows you to register multiple listeners for a given event and emit it.

```javascript
function myCallback(params) {
    // code...
}

add("myEvent", myCallback);

// ...

emit("myEvent");
```

You can also pass down arguments to the event you are emitting. Those will be accessible to all registered listeners.

```javascript
function myCallback({ prop }) {
    console.log(prop); // foo

    // code...
}

add("myEvent", myCallback);
emit("myEvent", { prop: "foo" });
```

Minemit also allows you to emit an event asynchronously with the emitAsync method. Basically all the listeners registered to that event will be treated as promises, giving you the possibility to wait for the whole listeners stack to complete. Here is an example.

```javascript
function myCallback() {
    // code...
}

async function myAsyncFunction() {
    // code...
}

add("myEvent", myCallback);
add("myEvent", myAsyncFunction);

await emitAsync("myEvent");

// code will be executed after the listeners stack has finished...
```

<br>

## Functions

<dl>
<dt><a href="#add">add</a> ⇒ <code>function</code></dt>
<dd><p>Adds a listener to a given event.</p>
</dd>
<dt><a href="#addOnce">addOnce</a> ⇒ <code>void</code></dt>
<dd><p>Adds a listener that gets fired once to a given event.</p>
</dd>
<dt><a href="#prepend">prepend</a> ⇒ <code>function</code></dt>
<dd><p>Adds a listener to a given event. The listener is added to the first position of the stack.</p>
</dd>
<dt><a href="#prependOnce">prependOnce</a> ⇒ <code>void</code></dt>
<dd><p>Adds a listener to a given event. The listener is added to the first position of the stack and that gets fired once.</p>
</dd>
<dt><a href="#remove">remove</a> ⇒ <code>void</code></dt>
<dd><p>Removes a listener from a given event.</p>
</dd>
<dt><a href="#clear">clear</a> ⇒ <code>void</code></dt>
<dd><p>Clears the listeners stack of the given event.</p>
</dd>
<dt><a href="#emit">emit</a> ⇒ <code>void</code></dt>
<dd><p>Fires all the listeners of the given event.</p>
</dd>
<dt><a href="#emitAsync">emitAsync</a> ⇒ <code>Promise</code></dt>
<dd><p>Fires all the listeners of the given event. The listeners get all treated as promises in order to get fired asyncronously.</p>
</dd>
<dt><a href="#list">list</a> ⇒ <code>Array</code></dt>
<dd><p>Returns the listeners stack of the given event.</p>
</dd>
</dl>

<br><br>

<a name="add"></a>

## add ⇒ <code>function</code>

Adds a listener to a given event.

**Returns**: <code>function</code> - Returns the newly added listener

| Param    | Type                  | Description                       |
| -------- | --------------------- | --------------------------------- |
| event    | <code>string</code>   | The name of the event             |
| listener | <code>function</code> | The listener that you want to add |

<br>

<a name="addOnce"></a>

## addOnce ⇒ <code>void</code>

Adds a listener that gets fired once to a given event.

| Param    | Type                  | Description                       |
| -------- | --------------------- | --------------------------------- |
| event    | <code>string</code>   | The name of the event             |
| listener | <code>function</code> | The listener that you want to add |

<br>

<a name="prepend"></a>

## prepend ⇒ <code>function</code>

Adds a listener to a given event. The listener is added to the first position of the stack.

**Returns**: <code>function</code> - Returns the newly added listener

| Param    | Type                  | Description                       |
| -------- | --------------------- | --------------------------------- |
| event    | <code>string</code>   | The name of the event             |
| listener | <code>function</code> | The listener that you want to add |

<br>

<a name="prependOnce"></a>

## prependOnce ⇒ <code>void</code>

Adds a listener to a given event. The listener is added to the first position of the stack and that gets fired once.

| Param    | Type                  | Description                       |
| -------- | --------------------- | --------------------------------- |
| event    | <code>string</code>   | The name of the event             |
| listener | <code>function</code> | The listener that you want to add |

<br>

<a name="remove"></a>

## remove ⇒ <code>void</code>

Removes a listener from a given event.

| Param    | Type                  | Description                       |
| -------- | --------------------- | --------------------------------- |
| event    | <code>string</code>   | The name of the event             |
| listener | <code>function</code> | The listener that you want to add |

<br>

<a name="clear"></a>

## clear ⇒ <code>void</code>

Clears the listeners stack of the given event.

| Param | Type                | Description           |
| ----- | ------------------- | --------------------- |
| event | <code>string</code> | The name of the event |

<br>

<a name="emit"></a>

## emit ⇒ <code>void</code>

Fires all the listeners of the given event.

| Param  | Type                      | Description                                             |
| ------ | ------------------------- | ------------------------------------------------------- |
| event  | <code>string</code>       | The name of the event                                   |
| params | <code>arguments...</code> | The optional arguments that will be passed to listeners |

<br>

<a name="emitAsync"></a>

## emitAsync ⇒ <code>Promise</code>

Fires all the listeners of the given event. The listeners get all treated as promises in order to get fired asyncronously.

**Returns**: <code>Promise</code> - The listeners stack

| Param  | Type                      | Description           |
| ------ | ------------------------- | --------------------- |
| event  | <code>string</code>       | The name of the event |
| params | <code>arguments...</code> | The name of the event |

<br>

<a name="list"></a>

## list ⇒ <code>Array</code>

Returns the listeners stack of the given event.

**Returns**: <code>Array</code> - The listeners stack

| Param | Type                | Description           |
| ----- | ------------------- | --------------------- |
| event | <code>string</code> | The name of the event |