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

response-pool

v0.3.3

Published

Response Pool is a very small library, based on js-csp, that can be used to pause subsequent function calls and pass a value from the first call in the concurrent group to the callback functions of subsequent calls as soon as it becomes available.

Downloads

8

Readme

Response Pool

Response Pool is a very small library (< 40 LoC), based on js-csp, that can be used to pause subsequently redundant function calls and pass a value from the first call to the callback functions of the redundant calls as soon as it becomes available.

For example: Functions within a Web API library may depend on having an access token available. If the library instance has not yet been authenticated, and then four such functions are executed from within the same block, each function will check the token state and will attempt to concurrently log-in.

This library provides an easy way of preventing this behavior in JavaScript, and in the example case, prevents four concurrent login attempts when only one with the same parameters is desired.

Response Pool was created for a specific purpose and meets the goals that it was written for, though feel free to submit a pull request or an issue.

Example Implementation

expensiveCall blocks subsequent calls while running and then passes the result from the first function call into the callback functions of the subsequent calls.

import ResponsePool from 'response-pool';
const rPool = new ResponsePool();                 // Create a new pool for a specific function or set of parameters.

function expensiveCall(respCallback) {
  if (rPool.pending) {                            // Check if a response value might be published.
    rPool.subVal(respCallback);                   // Wait for the value, then pass it to respCallback.
    return;                                       // Done.
  }
  try {
    rPool.setPending();                           // Effectively disable expensiveNetworkRequest.

    handler(expensiveNetworkRequest(), val => {
      try {
        rPool.pubVal(val, respCallback);          // Publish response value to subscribers.
        rPool.done();                             // Enable expensiveNetworkRequest again.
      }
      catch (ex) {
        throw ex;
      }
    });
  }
  catch (ex) {
    rPool.reset();                                // Publish null value to subscribers and enable expensiveNetworkRequest again.
    throw ex;
  }
}
  • Each implementation should determine whether or not a response value is pending, and then if so, pass in a callback function to the (Subscribe to Response Value) subVal method.

  • The setPending method is called from within a try...catch block. It starts the process of subscribing subsequently redundant calls to the pool, which was set up in the previous block.

  • The catch block should include a reset method call. This sends back a null value to, and releases, the blocking calls.

  • From within the callback that passes the response value into scope, the pubVal method is used, by passing in the response value and a callback function.

  • The done method is called after the response value is published to the pool. It removes the pending request state that was set by the setPending method.

License

The source code is available under the MIT License.