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.
npm install switchboard
bower install switchboardjs
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.
You've got two choices, cowboy.
var Switchboard = SwitchboardmyEmitter = ;var events = 'firstEvent' 'secondEvent' 'thirdEvent';myEmitter;
var Switchboard = SwitchboardmyEmitter = ;var events = 'firstEvent' 'secondEvent' 'thirdEvent';myEmitter;// multiple calls to registerEventArguments are a-okay// (hint: good for loops or for registering callbacks from// within other callbacks)myEmitter;myEmitter;
Now that you've defined your events, you can go ahead and emit things...
Which will produce the following output:
firstEvent:'0': null'1': 'aaaa''2': 'bbbbb'err: nullresultA: 'aaaa'resultB: 'bbbbb'secondEvent:'0': 'CCCC''1': 222222'2': 'EEEE'resultC: 'CCCC'resultD: 222222resultE: '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.