queue multiple functions and run later
Niceties
- widely compatible and small codebase
Caveats
- there is no built in mechanism to receive return values; use callbacks or promises
- any unhandled exception will stop subsequent function calls
$ npm install --save l8r
'use strict';
// let's queue socket listeners before we have a connection
var L8r = require('l8r');
var httpServer = require('http').createServer().listen(3000);
var io = require('socket.io')(httpServer);
var ioClient = require('socket.io-client')('http://localhost:3000');
// ...
// queue client-side listeners
var clientListeners = new L8r();
(function () {
var self = {
smile: '=)'
};
clientListeners.add(function (socket) {
var smile = this.smile || self.smile;
socket.on('smile', function (gesture) {
var smiley = gesture || smile;
console.log(smiley);
if (smile === '=)') {
socket.emit('wink');
}
});
socket.on('smirk', function (gesture) {
if (gesture === ';D' && smile === ':-)') {
console.log(gesture);
httpServer.close();
ioClient.close();
}
});
});
})();
// ...
// queue server-side listeners
var serverListeners = new L8r();
(function () {
serverListeners.add(function (socket) {
socket.once('wink', function () {
socket.emit('smirk', ';D');
});
});
})();
// ...
// now that it's later, add listeners
serverListeners.run(ioClient);
// ...
io.on('connection', function (socket) {
// now we that we have the connected socket,
// we can add listeners
clientListeners.run(socket);
// if you want to pass context to all the functions,
// use "apply()" instead of "run()"
clientListeners.apply({
smile: ':-)'
}, [socket]);
ioClient.emit('smile');
// => =)
// => :-)
// => ;D
});
-
Required Type
Function
A function to be queued for calling later.
Run all the functions added to the queue, passing in any arguments
-
Type:
Any
Parameters to pass into each function
Run all the functions added to the queue, with context,
applying an array of parameters (if provided)
-
Required Type:
Object
-
Type:
Array
The queue of functions to be called later
An alias for `queue`
ISC © Buster Collings