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

switchboard

v1.3.0

Published

Event listener for the browser and node.js for composite events (i.e., will not trigger until several specified events occur).

Downloads

7

Readme

// switchboard.js

A composite event listener. Waits for several events to occur before firing the handler.

Switchboard returns the arguments of each respective emit to the specified handler. These arguments can be named if desired.

Installing

node.js:

npm install switchboard

bower:

bower install switchboardjs
<script src="components/switchboardjs/index.js"></script>

Why do I need this?

Sometimes you have to wait for 2 or more things to happen before a certain part of your program can move forward. Let's say you're rendering a bunch of templates, for example. You want to do it asynchronously, but it's kind of a pain to figure out when everything's done without something kludgey (like a static counter or a jungle of if statements). switchboard.js can hide the kludge for you.

Familiar syntax, too. Just uses the classic EventEmitter 'on/once' pattern. Except instead of a single event name, you specify an array.

How to use

You've got two choices, cowboy.

1. Extremely simple way

var Switchboard = require('switchboard').Switchboard,
    myEmitter   = new Switchboard();

var events = ['firstEvent', 'secondEvent', 'thirdEvent'];

myEmitter.on(events, function(args) {
  // after calling `myEmitter.emit(e)` for all three events, args is something like:
  // { 'firstEvent':  [ ... the args ... ],
  //   'secondEvent': [ ... the args ... ],
  //   ...
  // }
  // etc.
});

2. With a little bit more code, you get your results returned in a dictionary!

var Switchboard = require('switchboard').Switchboard,
    myEmitter   = new Switchboard();

var events = ['firstEvent', 'secondEvent', 'thirdEvent'];

myEmitter.registerEventArguments({
  firstEvent: ['err', 'resultA', 'resultB']
});

// multiple calls to registerEventArguments are a-okay
// (hint: good for loops or for registering callbacks from
// within other callbacks)
myEmitter.registerEventArguments({
  secondEvent: ['resultC', 'resultD', 'resultE'],
  thirdEvent:  ['err', 'myVar', 'someJunk']
});

myEmitter.once(events, function(args) {
  console.log(args)
});

Now that you've defined your events, you can go ahead and emit things...

myEmitter.emit('firstEvent', null, 'aaaa', 'bbbbb')
myEmitter.emit('thirdEvent', {err: 'someErr'}, 'mvvvarrr', 'junk~!')
myEmitter.emit('secondEvent', 'CCCC', 222222, 'EEEE')

Which will produce the following output:

{ firstEvent: 
  { '0': null,
    '1': 'aaaa',
    '2': 'bbbbb',
    err: null,
    resultA: 'aaaa',
    resultB: 'bbbbb' },
secondEvent: 
  { '0': 'CCCC',
    '1': 222222,
    '2': 'EEEE',
    resultC: 'CCCC',
    resultD: 222222,
    resultE: 'EEEE' },
thirdEvent: 
  { '0': { err: 'someErr' },
    '1': 'mvvvarrr',
    '2': 'junk~!',
    err: { err: 'someErr' },
    myVar: 'mvvvarrr',
    someJunk: 'junk~!' } }

As you can see, each event's arguments are enumerated as well as being named. The point: you can skip the call to registerEventArguments() if you want.