Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@piggly/event-bus

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@piggly/event-bus

An ESM/CommonJS library following Oriented-Object Programming pattern to manager an Event Bus.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by500%
Maintainers
1
Weekly downloads
 
Created
Source

Event Bus Library

Typescript NPM Software License

An ESM/CommonJS library following Oriented-Object Programming pattern to manager an Event Bus.

Features

  • Publish, subscribe and unsubscribe to events;
  • Change default event driver;
  • Singleton & Oriented-Object Programming patterns;
  • Better code organization.

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.

Class Diagram for Event Bus Library

Installation

This library is ready for ES module or CommonJs module. You must add it by using Node.Js:

npm i --save @piggly/event-bus

Usage

First you must import EventBus singleton. This is the main entry point for event bus management.

Registering Drivers

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' });

Operations

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', {}));

Handlers & Payloads

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);
	}
}

Changelog

See the CHANGELOG file for information about all code changes.

Testing the code

This library uses the Jest. We carry out tests of all the main features of this application.

npm run test:once

Contributions

See the CONTRIBUTING file for information before submitting your contribution.

Credits

License

MIT License (MIT). See LICENSE.

Keywords

FAQs

Package last updated on 29 May 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc