Security News
Research
Supply Chain Attack on Rspack npm Packages Injects Cryptojacking Malware
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
@piggly/event-bus
Advanced tools
An ESM/CommonJS library following Oriented-Object Programming pattern to manager an Event Bus.
An ESM/CommonJS library following Oriented-Object Programming pattern to manager an Event Bus.
This library was a requirement for some internal projects on our company. But it may work with another projects designed following Oriented-Object Programming pattern.
This library is ready for ES module or CommonJs module. You must add it by using Node.Js:
npm i --save @piggly/event-bus
First you must import EventBus
singleton. This is the main entry point for event bus management.
By default, EventBus
will start with LocalEventDriver
loaded and get/set dispatcher to it. But you can create your own event driver implementing EventDriverInterface
with your custom dispatcher extending EventDispatcher
.
By default, the publish()
, subscribe()
, unsubscrive()
and unsubscriveAll()
methods will you the LocalEventDriver
. But, you can change this behavior saying what driver must be loaded. E.g: publish(event, { driver: 'another' })
. The another
driver must be registered.
import EventBus from '@piggly/event-bus';
// Register a new driver
EventBus.instance.register(new AnotherEventDriver());
// Will subscribe on local driver
EventBus.instance.subscribe('EVENT_NAME', new SomeEventHandler());
// Will subscribe on another driver
EventBus.instance.subscribe('EVENT_NAME', new SomeEventHandler(), { driver: 'another' });
import EventBus from '@piggly/event-bus';
// You can subscribe handlers to events
EventBus.instance.subscribe('EVENT_NAME', new SomeEventHandler());
// If needed you can unsubscribe, handler must be the same class that was subscribed
EventBus.instance.unsubscrive('EVENT_NAME', new SomeEventHandler());
// When event is ready, just publish it and event bus does what is need
EventBus.instance.publish(new EventPayload('EVENT_NAME', {}));
You must create classes for handlers, it allows dispatcher avoid to add the same constructor classes into handler array of an event:
import { EventHandler, EventPayload } from '@piggly/event-bus';
// !! USE EVENTPAYLOAD
// Event data
export type StubEventData = {
value: number;
};
// Event handler
export class StubEventHandler extends EventHandler<StubEventData> {
public handle(event: EventPayload<StubEventData>): void {
console.log(event);
}
}
// !! USE CUSTOMEVENTPAYLOAD
export class StubEventPayload extends EventPayload<StubEventData> {
constructor(data: { value: number }) {
super('STUB_EVENT', data);
}
}
// Event handler
export class StubEventHandler extends EventHandler<StubEventData> {
public handle(event: StubEventPayload): void {
console.log(event);
}
}
See the CHANGELOG file for information about all code changes.
This library uses the Jest. We carry out tests of all the main features of this application.
npm run test:once
See the CONTRIBUTING file for information before submitting your contribution.
MIT License (MIT). See LICENSE.
1.0.0 at 2023-05-29
FAQs
An ESM/CommonJS library following Oriented-Object Programming pattern to manager an Event Bus.
The npm package @piggly/event-bus receives a total of 6 weekly downloads. As such, @piggly/event-bus popularity was classified as not popular.
We found that @piggly/event-bus demonstrated a healthy version release cadence and project activity because the last version was released less than 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
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.
Security News
Sonar’s acquisition of Tidelift highlights a growing industry shift toward sustainable open source funding, addressing maintainer burnout and critical software dependencies.