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

@jgarber/radioradio

v2.0.1

Published

A very basic JavaScript PubSub library.

Downloads

23

Readme

RadioRadio

A very small, lightweight, dependency-free JavaScript PubSub library.

npm Downloads Build

Key Features

  • Supports namespaced events (e.g. foo.bar)
  • Dependency-free
  • JavaScript module (ESM), CommonJS, and browser global (window.RadioRadio) support

Getting RadioRadio

You've got a couple options for adding RadioRadio to your project:

  • Download a release from GitHub and do it yourself (old school).
  • Install using npm: npm install @jgarber/radioradio --save
  • Install using Yarn: yarn add @jgarber/radioradio

Usage

Subscribing

RadioRadio.subscribe(topic, subscriber);
  • Accepts: topic (String) and subscriber (Function)
  • Returns: String or false

The topic argument

Topics must be strings of any length (e.g. foo) made up of letters, numbers, and underscores. Topics may also be organized into namespaces using a . as a separator (e.g. foo.bar).

Topics within a namespace may themselves act as namespaces. For example, a subscribed topic foo.bar.biz exists in the foo and foo.bar namespaces. Note that this structure will affect publication (see Publishing below).

Wildcard topics within a namespace (e.g. foo.*, foo.bar.*) are also allowed. See Publishing below for more on when these topics are published.

The subscriber argument

A function to execute when topic is published. Subscribers accept a single argument (data) passed on from the publish method.

Publishing

RadioRadio.publish(topic, data);
  • Accepts: topic (String) and data (Object)
  • Returns: Array or false

The topic argument

The topic to which you wish to publish. When using namespaced topics (e.g. foo.bar), you may publish to a single topic:

RadioRadio.publish('foo.bar', data);

…or, to a topic and any topics within its namespace:

RadioRadio.publish('foo', data);

Publishing to the namespace foo will publish to foo and all topics namespaced to foo (e.g. foo.bar, foo.biz, foo.baz). As mentioned above in Subscribing, topics may be deeply nested (e.g. foo.bar.biz) which will affect publishing to namespaces:

RadioRadio.publish('foo', data);     // publishes to `foo`, `foo.bar`, `foo.bar.biz`
RadioRadio.publish('foo.bar', data); // publishes to `foo.bar`, `foo.bar.biz`

Wildcard topics within a namespace (e.g. foo.*) will be published alongside adjacent (e.g. foo.bar, foo.biz) published topics:

RadioRadio.subscribe('foo.bar', subscriber);
RadioRadio.subscribe('foo.*', wildcardSubscriber);

RadioRadio.publish('foo.bar', data); // publishes to `foo.bar` and `foo.*`

Note that topics are published in the order in which they were originally subscribed.

The data argument

The data to pass along to subscribers. Most usefully an Object, data could be anything so long as the topic's subscriber functions are prepared to handle it.

Unsubscribing

RadioRadio.unsubscribe(topic);
  • Accepts: topic (String)
  • Returns: true

The topic argument

The topic from which you wish to unsubscribe. Calling this method removes topic from the stored list of subscribed topics. Subsequent calls to publish to that topic will fail silently, returning false.

Example

For a full-featured RadioRadio demonstration, check out the demo page and the example file.

Browser Support

RadioRadio works in modern browsers. The library makes use of several new(ish) JavaScript methods and, in an effort to remain as lightweight and dependency-free as possible, leaves it up to you to choose whether or not to polyfill features for older browsers.

Acknowledgments

RadioRadio is inspired by Morgan Roderick's PubSubJS.

RadioRadio is written and maintained by Jason Garber and, yes, it's named after an Elvis Costello & The Attractions song. It's also another in a growing line of small, curiously-named JavaScript utilities:

License

RadioRadio is freely available under The MIT License. Use it, learn from it, fork it, improve it, change it, tailor it to your needs.