Socket
Socket
Sign inDemoInstall

response-pool

Package Overview
Dependencies
5
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    response-pool

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.


Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

Response Pool

Response Pool is a very small library (< 50 LoC), based on js-csp, that can be used to pause subsequent function calls and pass a value from the first call to the callback functions of later 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.pubPending()) {                       // Check if a response value might be published.
    rPool.subResp(respCallback);                  // Wait for the value, then pass it to respCallback.
    return;                                       // Done.
  }
  try {
    rPool.addPending();                           // Effectively disable expensiveNetworkRequest.

    handler(expensiveNetworkRequest(), val => {
      rPool.pubResp(val, respCallback);           // Publish response value to subscribers.
      rPool.delPending();                         // Enable expensiveNetworkRequest again.
    });
  }
  catch (ex) {
    rPool.resetCh();                              // Send null value to subscribers and delete pending response.
    throw ex;
  }
}

License

The source code is available under the MIT License.

FAQs

Last updated on 23 May 2017

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc