An Event Bus written in less then 100 LOC vanilla JS. You can use it with standard WebComponents.
npm install @yhwh-script/event-buxTo install the eventbus into your project, simply run
``bash`
npm install @yhwh-script/event-bux
For instance:
`javascript`
After importing @yhwh-script/event-bux, it is recommended to add it to the window object.
Feel free to import at your own or stick to @yhwh-script examples.
Then use bus.addEventListener(type, listener) and bus.dispatchEvent(event) in your WebComponents. You are not bound to event bubbling or capturing, which are the standard event propagation mechanisms, but now you can send events even among any objects!
`javascript`
bus.addEventListener("click", () => {console.log("click")}, window);
bus.dispatchEvent(new Event("click"), window);
`mermaid``
classDiagram
EventTarget <|-- EventBus
EventTarget: +addEventListener()
EventTarget: +removeEventListener()
EventTarget: +dispatchEvent()
EventBus: -WeakMap listeners
That's it. Check it out! KISS
Welcome, please give your warm feedback.