l8r

1.0.0 • Public • Published

l8r NPM version Build Status Dependency Status

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

Installation

$ npm install --save l8r

Example

'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
});

API

add(fn)

  • #### fn

    Required Type Function

    A function to be queued for calling later.

run([arguments])

Run all the functions added to the queue, passing in any arguments
  • #### arguments

    Type: Any

    Parameters to pass into each function

apply(context, [parameters])

Run all the functions added to the queue, with context, 
applying an array of parameters (if provided)
  • #### context

    Required Type: Object

  • #### parameters

    Type: Array

queue

The queue of functions to be called later

q

An alias for `queue`

License

ISC © Buster Collings

Package Sidebar

Install

npm i l8r

Weekly Downloads

3

Version

1.0.0

License

ISC

Last publish

Collaborators

  • buster