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

parallel-ware

v0.0.1

Published

Executes middleware in parallel

Downloads

25

Readme

parallel-ware

Execute middleware in parallel. For executing in series, see ware.

Example

The following example runs all the middleware in parallel.

var parallel = require('parallel-ware');

function add (val) {
  return function (calc, next) {
    calc.sum += val;
    console.log(calc.sum);
    next();
  };
}

// executes in parallel, no order guarenteed
var middleware = parallel()
  .use(add(1))
  .use(add(2))
  .use(add(3));

middleware.run({ sum: 0 }, function (err, result) {
  // all the middleware has run, calc.sum equals 6
});

// 2
// 5
// 6

Wait to Execute

You can also tell parallel-ware to wait to execute a middleware until a specific condition is met. The .when(wait, fn) method takes the wait function as an optional first parameter.

var parallel = require('parallel-ware');

function add (val) {
  return function (calc, next) {
    calc.sum += val;
    console.log(calc.sum);
    next();
  };
}

function equals (val) {
  return function (calc, next) {
    next(null, calc.sum === val);
  };
}

var middleware = parallel()
  .use(add(1))
  .when(equals(4), add(2)) // executes after sum === 4
  .when(equals(10), add(10)) // executes after sum === 10, which never happens
  .use(add(3));

middleware.run({ sum: 0 }, function (err, calc) {
  // all the middleware has run, calc.sum equals 6
});

// 3
// 4
// 6

Managing Concurrency

You can limit the concurrency with which the middleware will get executed.

var middleware = parallel()
  .concurrency(2)
  .use(constantContact())
  .use(linkedin())
  .use(facebook())
  .use(github());

Monitoring Progress

And you can listen for progress events (following the same structure as visionmedia/batch progress events).

var middleware = parallel()
  .use(constantContact())
  .use(linkedin())
  .use(facebook())
  .use(github());

middleware.on('progress', function (progress) {
  
});

middleware.run({ email: '[email protected]' });

Error Handling

Middleware errors don't stop the overall execution, but are rather passed back in batch.

var parallel = require('parallel-ware');

function add (val) {
  return function (calc, next) {
    calc.sum += val;
    next();
  };
}

function fail (animal) {
  return function (calc, next) {
    next(new Error(animal));
  };
}

// executes in parallel, no order guarenteed
var middleware = parallel()
  .use(add(1))
  .use(fail('porcupine'))
  .use(add(2))
  .use(fail('whale'))
  .use(add(3));

middleware.run({ sum: 0 }, function (err, calc) {
  if (err) {
    console.log(err);
    console.log(err.errors)
  }
  console.log(calc.sum);
});

// Error: 2 error(s) encountered.
// [Error: Whale, Error: Porcupine]
// 6

API

parallel()

Create a parallel middleware execution pipeline.

.use(fn)

Add a middleware fn to the execution pipeline.

.when(wait, fn)

Add a middleware fn to the execution pipeline to run after the wait says it can.

The wait function supports a synchronous signature:

function equals (val) {
  return function (calc) {
    return calc.sum === val;
  };
}

var middleware = parallel()
  .use(add(1))
  .when(equals(4), add(2)) // executes after sum === 4
  .when(equals(10), add(10)) // executes after sum === 10, which never happens
  .use(add(3));

middleware.run({ sum: 0 }, function (err, calc) {
  // all the middleware has run, calc.sum equals 6
});

And an asynchronous signature:

function equals (val) {
  return function (calc, next) {
    next(null, calc.sum === val);
  };
}

var middleware = parallel()
  .use(add(1))
  .when(equals(4), add(2)) // executes after sum === 4
  .when(equals(10), add(10)) // executes after sum === 10, which never happens
  .use(add(3));

middleware.run({ sum: 0 }, function (err, calc) {
  // all the middleware has run, calc.sum equals 6
});

.concurrency(max)

Limit the amount of concurrently executing middleware to max.

.on('progress', ..

Listen on progress events.

License

WWWWWW||WWWWWW
 W W W||W W W
      ||
    ( OO )__________
     /  |           \
    /o o|    MIT     \
    \___/||_||__||_|| *
         || ||  || ||
        _||_|| _||_||
       (__|__|(__|__|

Copyright (c) 2013 Segment.io <[email protected]>