Skip to content

An Event Bus written in <50 LOC vanilla JS. You can use it with standard WebComponents.

License

Notifications You must be signed in to change notification settings

vanillaspa/event-bus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Getting started

To install the event-bus into your project, simply run

npm install @vanillaspa/event-bus

How-To

Now integrate the event-bus into your app:

<script type="module">
  import * as eventbus from '@vanillaspa/event-bus';
  window.eventbus = eventbus;
</script>

This is only a recommendation. You can use it however you like. With the eventbus object attached to the window object, you have access to the event-bus in your WebComponents.

Then simply use eventbus.addEventListener(type, listener) and eventbus.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!

eventbus.addEventListener("click", () => {console.log("click")}, window);
eventbus.dispatchEvent(new Event("click"), window);

EventBus API architecture

EventBus schema

classDiagram
    EventTarget <|-- EventBus
    EventTarget: +addEventListener()
    EventTarget: +removeEventListener()
    EventTarget: +dispatchEvent()
    EventBus: -WeakMap listeners
Loading

That's it. Check it out! KISS

Contribute

Please give your warm feedback.

About

An Event Bus written in <50 LOC vanilla JS. You can use it with standard WebComponents.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors