
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
@zenstack/zen-bus
Advanced tools
A simple event bus for your general pub/sub needs.
yarn add @zenstack/zen-bus
or
npm install @zenstack/zen-bus
import { createEventBus } from "@zenstack/zen-bus";
const eventBus = createEventBus();
//you can also use a class if you are an OO person
import { EventBus } from "@zenstack/zen-bus";
const eventBus = new EventBus();
To subscribe a handler to an event type, you can use the .subscribe(:eventType, :handler) method.
const myTodoList = ['Clean Toilet'];
const addToList = (event) => myTodoList.push(event.title);
const logEvent = (event) => console.log(event);
eventBus.subscribe('Todo Added', addToList);
eventBus.subscribe('Todo Added', logEvent);
If you would like to subscribe a handler to any event type, you can use
the EventBus's ANY_EVENT_TYPE static property.
import { EventBus } from "@zenstack/zen-bus";
const logEvent = (event) => console.log(event);
// this will trigger `logEvent` when any event is emitted.
eventBus.subscribe(EventBus.ANY_EVENT_TYPE, addToList);
An event is an object that contains a string type attribute. You can asynchronously emit an event
by using the event bus's .emit(:event) method.
const todoAddedEvent = {
type: 'Todo Added',
title: 'Clean the kitchen'
};
eventBus.emit(todoAddedEvent);
By default, events are emitted asynchronously. You can also force the emission to be synchronous if you'd like.
const todoAddedEvent = {
type: 'Todo Added',
title: 'Clean the kitchen'
};
eventBus.emitSync(todoAddedEvent);
Note: Be careful of emitting events synchronously. It has a few disadvantages:
If at any point you would like to unsubscribe all handlers of a particular event type, you can
use the event bus's .flush(:eventType) method.
const myEvent1 = { type: 'myEvent1' };
const myEvent2 = { type: 'myEvent2'};
const myHandler1 = (event) => {/*..*/};
const myHandler2 = (event) => {/*..*/};
const myHandler3 = (event) => {/*..*/};
eventBus.subscribe('myEvent1', myHandler1);
eventBus.subscribe('myEvent1', myHandler2);
eventBus.subscribe('myEvent2', myHandler3);
// will trigger all event handlers
eventBus.emit(myEvent1);
eventBus.emit(myEvent2);
// only myHandler3 is still subscribed. only it will be triggered.
eventBus.flush('myEvent1');
eventBus.emit(myEvent1);
eventBus.emit(myEvent2);
If at any point you would like to unsubscribe all handlers, you can
use the event bus's .flushAll() method.
const myEvent1 = { type: 'myEvent1' };
const myEvent2 = { type: 'myEvent2'};
const myHandler1 = (event) => {/*..*/};
const myHandler2 = (event) => {/*..*/};
const myHandler3 = (event) => {/*..*/};
eventBus.subscribe('myEvent1', myHandler1);
eventBus.subscribe('myEvent1', myHandler2);
eventBus.subscribe('myEvent2', myHandler3);
// will trigger all event handlers
eventBus.emit(myEvent1);
eventBus.emit(myEvent2);
// no handler is susbcribed. Non will trigger.
eventBus.flushAll();
eventBus.emit(myEvent1);
eventBus.emit(myEvent2);
FAQs
A simple event bus for your general pub/sub needs.
We found that @zenstack/zen-bus demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.