Skip to content

Simple event bus with asynchronous calling support based on vanilla js

License

Notifications You must be signed in to change notification settings

NickMaev/NEventBus

Repository files navigation

npm version npm downloads

Description

NEventBus is a Vanilla JS event bus with async call support.

Usage

Install

npm install neventbus

Prepare and usage

Import:

import {NEventBus} from "neventbus";

or use the scripts tag (bundle):

<script src="neventbus.min.js"></script>

Then: (TypeScript)

NEventBus.subscribe("event1", this, (args) => {
	alert(args);
}); // "this" <-- Object which will be subscribed.

(JavaScript, using scripts tag)

NEventBus.NEventBus.subscribe("event1", this, function(args) {
	alert(args);
}); // In JS bundle NEventBus.* required, because of UMD library!

Fire event: (TypeScript)

NEventBus.fire("event1", args);

(JS from bundle)

NEventBus.NEventBus.fire("event1", args);

Unsubscribe: (TypeScript)

NEventBus.unsubscribe("event1", this); // "this" <-- Object which was subscribed.

(JS from bundle)

NEventBus.NEventBus.unsubscribe("event1", this);

Async call support

Define the event:

NEventBus.subscribe("asyncEvent1", this, 
	async (arg) => {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                    console.log("Executed: " + arg);
                    resolve();
                },
                2000);
        });
    }); // "this" <-- Object which will be subscribed.

Fire async event:

await NEventBus.fireAsync("asyncEvent1", args);

Releases

No releases published

Packages

No packages published