ga-pubsub
v1.0.49
Published
It establishes data communication between different components or systems using event-driven architecture
Downloads
28
Maintainers
Readme
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.
To install the PubSub Eventing Manager, use the following npm command:
npm install ga-pubsub
This package follows event-driven architecture, utilizes the UMD pattern, and is compatible with both CommonJS and ES6 Modules.
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');
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);
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);
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();
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);
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);
});
}
Ajithraj G and his Official site