Member-only story
Introducing Omnibus: new Event Bus library written in TypeScript
Event bus is an integral part of JavaScript. Each DOM element subscribes to it and events are the key to the web’s interactivity. But the main concept of it can be used in many other scenarios.
Omnibus is a library that helps you leverage the capabilities of event buses (or more widely speaking publisher/subscriber pattern) without relying on the DOM. It’s lightweight and fully written in TypeScript. It can provide safe type-checking for all your events — even if they emit different types of data.
Let’s check the basic example below:
The code above sets up a new Omnibus based on the EventDefinitions
interface. Thanks to that the .on
and .trigger
method parameters are typed properly. Note that we can use completely different types for each of the events and those will be properly typed.
You can optionally name your event arguments (like in our case of the end
event), and you will be prompted with a helpful name suggestion: