neventbus
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

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);

Readme

Keywords

none

Package Sidebar

Install

npm i neventbus

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

37.1 kB

Total Files

28

Last publish

Collaborators

  • trixon