ga-pubsub
TypeScript icon, indicating that this package has built-in type declarations

1.0.49 • Public • Published

About

A lightweight and flexible PubSub (Publish-Subscribe) event manager for JavaScript environments. This package enables seamless communication between different parts of your application by allowing components to subscribe to specific events and react accordingly when those events are published.

Installation

To install the PubSub Eventing Manager, use the following npm command:

npm install ga-pubsub

Support

This package follows event-driven architecture, utilizes the UMD pattern, and is compatible with both CommonJS and ES6 Modules.

Getting Started

To initialize the event manager, use the provided function getEventingManagerInstance and pass the argument 'pubsub'. This ensures a singleton instance of the Eventing Manager is created and shared across your application.

CommonJS:

const { getEventingManagerInstance } = require('ga-pubsub');

// Initialize the event manager
const eventManager = getEventingManagerInstance('pubsub');

ES6:

import { getEventingManagerInstance } from 'ga-pubsub';

// Initialize the event manager
const eventManager = getEventingManagerInstance('pubsub');

Typescript:

import { getEventingManagerInstance } from 'ga-pubsub';

// Initialize the event manager
private eventManager = getEventingManagerInstance('pubsub');

Usage

Subscribing to events

Subscribing to events by providing an event name and a callback function. The callback function will be invoked whenever the specified event is published.

CommonJS:

const callback = (data) => {
  // Handle the event data
  console.log('Event received:', data);
};

// Subscribe to an event
eventManager.subscribe('exampleEvent', callback);

ES6:

const callback = (data) => {
  // Handle the event data
  console.log('Event received:', data);
};

// Subscribe to an event
eventManager.subscribe('exampleEvent', callback);

Typescript:

private callback = (data: any) => {
  // Handle the event data
  console.log('Event received:', data);
};

// Subscribe to an event
this.eventManager.subscribe('exampleEvent', this.callback);

Publishing Events

Publish events with associated data. Subscribed functions will be called with the provided data when the event is published.

CommonJS:

const eventData = { 'key': 'value' };

// Publish an event
eventManager.publish('exampleEvent', eventData);

ES6:

// Publish an event
const eventData = { 'key': 'value' };

this.eventManager.publish('exampleEvent', eventData);

Typescript:

// Publish an event
const eventData = { 'key': 'value' };

this.eventManager.publish('exampleEvent', eventData);

Unsubscribing from an event

Unsubscribe from specific events or remove all subscriptions.

CommonJS:

const subscriber = eventManager.subscribe('exampleEvent', callback);

// Unsubscribe from a specific event
await eventManager.unsubscribe(subscriber.eventName, subscriber.id);

// Unsubscribe from all events
eventManager.unsubscribeAll();

ES6:

const subscriber = eventManager.subscribe('exampleEvent', callback);

// Unsubscribe from a specific event
await this.eventManager.unsubscribe(subscriber.eventName, subscriber.id);

// Unsubscribe from all events
this.eventManager.unsubscribeAll();

Typescript:

const subscriber = eventManager.subscribe('exampleEvent', callback);

// Unsubscribe from a specific event
await eventManager.unsubscribe(subscriber.eventName, subscriber.id);

// Unsubscribe from all events
eventManager.unsubscribeAll();

Subscribe to an event at ANYTIME

CommonJS:

const eventName = 'testEvent';
const eventData = 'Test data';
    
function callback(data) {
    console.log('Event received:', data);
}

// Publish event
eventManager.publish(eventName, eventData);

const subscriber = eventManager.subscribe(eventName, callback);

ES6:

const eventName = 'testEvent';
const eventData = 'Test data';
    
function callback(data) {
    console.log('Event received:', data);
}

// Publish event
eventManager.publish(eventName, eventData);

const subscriber = eventManager.subscribe(eventName, callback);

Typescript:

const eventName = 'testEvent';
const eventData = 'Test data';
    
private callback = (data: any) => {
    console.log('Event received:', data);
}

// Publish event
this.eventManager.publish(eventName, eventData);

const subscriber = this.eventManager.subscribe(eventName, this.callback);

Sample Code to Get Started

CommonJS:

const { getEventingManagerInstance } = require('ga-pubsub');

// Initialize the event manager
let eventManager = getEventingManagerInstance('pubsub');
let subscriberList = [];

const callback_1 = (data) => {
  // Handle the event data
  console.log('Event received:', data);
};

// Subscribe to an event
addSubscriber('exampleEvent', callback_1);

// Publish an event
const eventData = { 'key': 'value' };
eventManager.publish('exampleEvent', eventData);

const callback_2 = (data) => {
  // Handle the event data
  console.log('Event received:', data);
};

// Subscribe to an event at anytime
addSubscriber('exampleEvent', callback_2);

function addSubscriber(eventName, callback) {
  const subscriber = eventManager.subscribe(eventName, callback);
  this.subscriberList.push(subscriber);
}

// Unsubscribe after completion 
this.subscriberList.forEach(subscriber => {
  eventManager.unsubscribe(subscriber.eventName, subscriber.id);
})

ES6:

import { getEventingManagerInstance } from 'ga-pubsub';

// Initialize the event manager
const eventManager = getEventingManagerInstance('pubsub');
let subscriberList = [];

const callback_1 = (data) => {
  // Handle the event data
  console.log('Event received:', data);
};

// Subscribe to an event
addSubscriber('exampleEvent', callback_1);

// Publish an event
const eventData = { 'key': 'value' };
eventManager.publish('exampleEvent', eventData);

const callback_2 = (data) => {
  // Handle the event data
  console.log('Event received:', data);
};

// Subscribe to an event at any time
addSubscriber('exampleEvent', callback_2);

function addSubscriber(eventName, callback) {
  const subscriber = eventManager.subscribe(eventName, callback);
  subscriberList.push(subscriber);
}

// Unsubscribe after completion 
subscriberList.forEach(subscriber => {
  eventManager.unsubscribe(subscriber.eventName, subscriber.id);
});

Typescript:

private eventManager = getEventingManagerInstance('pubsub');
private subscriberList: any[] = [];

ngOnInit() {
  this.subscribeToEvent('exampleEvent', this.handleEvent1);

  const eventData = { 'key': 'value' };
  this.eventManager.publish('exampleEvent', eventData);

  this.subscribeToEvent('exampleEvent', this.handleEvent2);
}

ngOnDestroy() {
  this.unsubscribeAll();
}

private handleEvent1(data:any) {
  console.log('Event received:', data);
}

private handleEvent2(data:any) {
  console.log('Event received:', data);
}

private subscribeToEvent(eventName:string, callback:any) {
  const subscriber = this.eventManager.subscribe(eventName, callback);
  this.subscriberList.push(subscriber);
}

private unsubscribeAll() {
  this.subscriberList.forEach((subscriber:any) => {
    this.eventManager.unsubscribe(subscriber.eventName, subscriber.id);
  });
}

License

This package is licensed under the MIT License - see the LICENSE.md file for details.

Author

Ajithraj G and his Official site

Package Sidebar

Install

npm i ga-pubsub

Weekly Downloads

1

Version

1.0.49

License

ISC

Unpacked Size

16.9 kB

Total Files

5

Last publish

Collaborators

  • ajithraj-g