1.0.12 • Published 2 years ago

minemit v1.0.12

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

minemit

A minimal event emitter

license npm

Installation and basic usage

Install via npm

$ npm i minemit

Either import minemit completely or just the desired method

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.

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.

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.

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...

Functions

add ⇒ function

Adds a listener to a given event.

Returns: function - Returns the newly added listener

ParamTypeDescription
eventstringThe name of the event
listenerfunctionThe listener that you want to add

addOnce ⇒ void

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

ParamTypeDescription
eventstringThe name of the event
listenerfunctionThe listener that you want to add

prepend ⇒ function

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

Returns: function - Returns the newly added listener

ParamTypeDescription
eventstringThe name of the event
listenerfunctionThe listener that you want to add

prependOnce ⇒ void

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

ParamTypeDescription
eventstringThe name of the event
listenerfunctionThe listener that you want to add

remove ⇒ void

Removes a listener from a given event.

ParamTypeDescription
eventstringThe name of the event
listenerfunctionThe listener that you want to add

clear ⇒ void

Clears the listeners stack of the given event.

ParamTypeDescription
eventstringThe name of the event

emit ⇒ void

Fires all the listeners of the given event.

ParamTypeDescription
eventstringThe name of the event
paramsarguments...The optional arguments that will be passed to listeners

emitAsync ⇒ Promise

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

Returns: Promise - The listeners stack

ParamTypeDescription
eventstringThe name of the event
paramsarguments...The name of the event

list ⇒ Array

Returns the listeners stack of the given event.

Returns: Array - The listeners stack

ParamTypeDescription
eventstringThe name of the event
1.0.7

2 years ago

1.0.6

2 years ago

1.0.12

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago