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

airwaves

v0.3.0

Published

Airwaves is a lightweight pub/sub library that can be used in any JavaScript environment. It has no dependencies.

Downloads

15

Readme

Airwaves

Airwaves is a lightweight pub/sub library that can be used in any JavaScript environment. It has no dependencies.

To communicate over the airwaves, first create a channel:

bbc = new airwaves.Channel

Channels provide methods for broadcasting transmissions and for managing subscriptions to these broadcasts. Each channel operates on a dedicated frequency, so there's no risk of anyone receiving unwanted transmissions.

bbc.subscribe "news", ({name}) -> alert "now playing: #{name}"
bbc.broadcast "news",
  name: "World Business Report"
  desc: "The latest business and finance news from around the world."

This adds a subscriber to "news" broadcasts on the bbc channel. Shortly thereafter, the subscriber learns of the World Business Report broadcast.


channel.broadcast(name, args...)

Broadcast args to all those subscribed to name on this channel.

channel.broadcast "delete"
channel.broadcast "rename", "johnsmith", "JohnSmith"
channel.broadcast "resize", width: 1360, height: 859

channel.subscribe(name, fn)

Subscribe fn to broadcasts of name on this channel. fn receives the broadcast's values as arguments.

channel.subscribe "hashchange", (hash) -> location.hash = hash

channel.unsubscribe(name[, fn])

Unsubscribe fn from broadcasts of name on this channel. If fn is omitted, all subscriptions to broadcasts of name on this channel are cancelled.

# Unsubscribe save handler from "textchange" broadcasts:
channel.unsubscribe "textchange", save

# Unsubscribe all handlers from "resize" broadcasts:
channel.unsubscribe "resize"

channel.intercept(name, fn)

Subscribe fn to broadcasts of name on this channel in such a way that it may modify and/or cancel such broadcasts.

Subscribers can receive and respond to broadcasts, but cannot affect them in any way. Interceptors, on the other hand:

  • receive broadcasts ahead of subscribers
  • can modify broadcasts
  • can cancel broadcasts

An interceptor receives the broadcast as its first argument, followed by zero or more arguments representing the broadcast's content:

channel.intercept "print", (broadcast, filename, orientation) ->
  # ...
channel.broadcast "print", "~/Downloads/contract.pdf", "portrait"

An interceptor cancels any broadcast it intercepts unless it invokes the function passed to it as its first argument. The values it passes to this function are received by subscribers (and any remaining interceptors). The no-op interceptor is as follows:

channel.intercept name, (broadcast, args...) ->
  broadcast args...

An interceptor modifies a broadcast by invoking the broadcast function with values different from those it received. This enables an interceptor to, for example, sanitize broadcasts on the fly:

channel.intercept "comment", (broadcast, user, message) ->
  broadcast user, message.replace /wtf/gi, "what on earth"

channel.subscribe "comment", (user, message) ->
  console.log "#{user}: #{message}"

channel.broadcast "comment", "Mr. Badger", "I don't know WTF is going on"
channel.broadcast "comment", "Ken Shabby", "WTF are you talking about?"

# Mr. Badger: I don't know what on earth is going on
# Ken Shabby: what on earth are you talking about?

Interceptors can also be used to suppress invalid broadcasts. For example:

channel.intercept "move", (broadcast, move) ->
  if player is active_player then broadcast move
  else alert "It's not your turn!"

Event names

Airwaves supports comma-separated event names, so the following snippets are equivalent:

channel.subscribe "numerator-change", calculate
channel.subscribe "denominator-change", calculate
channel.subscribe "numerator-change, denominator-change", calculate

More accurately, event names are delimited by [,\s]+ (thus cannot contain commas or whitespace).

Testing

make setup
make test