npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ga-pubsub

v1.0.49

Published

It establishes data communication between different components or systems using event-driven architecture

Downloads

28

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