NEventBus is a Vanilla JS event bus with async call support.
npm install neventbus
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);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);