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

@evercoder/pubsub

v1.0.0

Published

`pubsub` is a small, fast JavaScript library that implements the [Publish/Subscribe](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) pattern for web applications.

Downloads

53

Readme

pubsub

pubsub is a small, fast JavaScript library that implements the Publish/Subscribe pattern for web applications.

Usage

npm install --save @evercoder/pubsub

API Reference

§ new pubsub(options) creates a new message bus.

Available options:

  • strict: (Boolean, default false) deprecates some ways of using pubsub that we found were promoting error-prone code patterns and restricts the message bus to a subset of its API. See Strict Mode.
import pubsub from '@evercoder/pubsub';

let bus = new pubsub();

§ bus.pub(event, payload, ...additional_args) — publish an event.

bus.pub('my-event', {
	myprop: myvalue
});

All pub() arguments after the event are forwarded as-is as arguments to the listeners.

Note: Although the library supports multiple arguments, we plan to deprecate this feature.

§ bus.sub(event(s), listener, thisArg, options) — subscribe a listener to an event.

bus.sub('my-event', function(payload) {
	console.log(payload.myrop);
	// ⇒ myvalue
});

Available options:

  • once (Boolean, default false) whether the listener should unsubscribe itself from the event after being called once.
  • 👎 recoup (Boolean, default false) whether the listener should be immediately executed if the event already happened before the subscription.

§ 👎 bus.recoup(event, listener, thisArg) is a shortcut for calling sub() with the recoup: true option.

Note: This option will be deprecated.

§ bus.once(event, listener, thisArg) is a shortcut for calling once() with the once: true option.

§ bus.unsub(event, listener) — unsubscribe a listener from an event.

Note: Currently, when listener is omitted, all listeners on that particular event are unsubscribed. We plan to deprecate this feature.

Strict mode

In strict mode, the following patterns log a warning:

pub()-ing an event with multiple arguments

Always use a single argument for the payload.

Handling multiple arguments means the library needs to manipulate the arguments object to properly forward them to the listeners. Renouncing this will allow us to make a faster library by leveraging browser code optimizations.

sub()-ing to multiple events via a space-separated string

Always use an array of events as the first argument to subscribe to many events at once.

A previous version of the library did not support arrays and used space-separated strings. When using statically analyzable event names, it made you use an awkward construct:

bus.sub([EVENT_A, EVENT_B].join(''), function() { ... });

sub(): using thisArg;

Instead, bind the listener manually with bind() or using an arrow function.

bus.sub(
	'myevent',
	function() {
		this.doSomething();
	}.bind(this)
);

// or

bus.sub('myevent', () => {
	this.doSomething();
});

Storing thisArg in the pubsub instance may create memory leaks.

Using recoup()

Find an alternative where you can use a simple sub().

Similar to storing thisArg for sub(), storing the arguments with which pub was called, so that listeners could listen to that event retroactively, can create memory leaks or subtle inconsistencies in state.

unsub() called without a listener

Always provide the listener you wish to unsubscribe.

The default behavior here (unsubscribing all listeners from the event) may cause hard-to-trace bugs if unsub() silently receives an undefined reference to a listener.