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

pubsubmanager

v0.1.0

Published

A simple and lightweight module for publishing and subscribing to custome events.

Downloads

3

Readme

PubSubManager

A library for subscribing to custom events and publishing them.

Install

npm install pubsubmanager

This library is basically used in scenarios where custom and mutiple eventing is needed.

It follows a publisher-subscribers model. An event if fired, multiple subscribers who're listening to that event receive that event.

Basic Scenario

For example, consider a scenario where on a particular action you need to perform multiple actions irrespective of eachother.

var setProfile = function(sender, args) {
	//custom code
};

var setUserActive = function(sender, args) {
	//Custom code	
};

PubSubManager.subscribe('user.login', setProfile);
PubSubManager.subscribe('user.login', setUserActive);

PubSubManager.fire('user.login', this, {id: 'XCVDF1DD233', name : 'John'});

Publish

To publish an event you need to provide three arguments viz. eventname, sender and optional arguments

PubSubManager.fire(
	"user.login", // event-name
	this, // a refernece to sender, this can be string or an object
	{ name:'John' } // an object of arguments 
)

Subscribe

To subscribe an event you need to provide three arguments viz. eventname and callback

var subId = PubSubManager.subscibe(
	"user.login", // event-name
	callback 	  //callback
)

Unsubscribe

Whenever you subscribe to an event , you're returned with an subscriptionId. You can save this for unsubscribing to the event in future

PubSubManager.unsubscribe(subId);

ClearAndSubscribe

This is an enhaced version of subscribe, which makes sure that before you subscribe to that event, all other subscriptions for that event are cleared.

PubSubManager.clearAndSubscribe("eventName", callback);

ClearSubscriptions

This method allows you to unsubscribe all other subscriptions for this event.

PubSubManager.clearSubscriptions("eventName");

Other Scenarios

For example, consider you're writing a multipage app, and intend to reuse components/javascript files in these pages. There're cases where you may include not all but only some of these files. Thus, unintentionally giving way for exceptions, for undefined functions, which're defined in other script which was not included. This is the scenario where PubSubManager has its real purpose.

a.js

var showMessage = function(sender, args) {
	//Your code
};

//Subscribe to message show event
PubSubManager.subscribe('message.show', showMessage);

b.js

var hideMessage = function(sender, args) {
	//Your code
};

//Subscribe to message hide event
PubSubManager.subscribe('message.hide', hideMessage);

c.js

//Fire message show event
PubSubManager.fire('message.show', this, { msg: 'This is a message' });

//Fire message hide event
PubSubManager.fire('message.hide', this, {});

In above example, consider if you include a.js and c.js, it won't throw an exception, as there is no subscriber for message.hide function.

License

MIT License