🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

l8r

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

l8r

queue multiple functions and run later

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

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

Keywords

later

FAQs

Package last updated on 07 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts