neventbus
v1.0.0
Published
Simple EventBus with async support on Vanilla JS
Downloads
2
Readme
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);