
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
@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.
The npm package @zenstack/zen-bus receives a total of 5 weekly downloads. As such, @zenstack/zen-bus popularity was classified as not popular.
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.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.