Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

native-promise-pool

Package Overview
Dependencies
Maintainers
2
Versions
95
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

native-promise-pool - npm Package Compare versions

Comparing version 2.3.0 to 3.0.0-next.1587290706.8352c90844b025b38b9519ddf029ff56bf4eec00

edition-esnext/index.js

191

edition-browsers/index.js

@@ -1,147 +0,44 @@

'use strict';
/**
* A function that is fired when the pool is ready for it to execute.
* It can return a promise if it is asynchronous, in which case the pool will only start another task once the promise resolves.
* @callback Task
* @returns {Promise|any}
*/
/**
* Create a PromisePool
*/
function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _construct(Parent, args, Class) { if (isNativeReflectConstruct()) { _construct = Reflect.construct; } else { _construct = function _construct(Parent, args, Class) { var a = [null]; a.push.apply(a, args); var Constructor = Function.bind.apply(Parent, a); var instance = new Constructor(); if (Class) _setPrototypeOf(instance, Class.prototype); return instance; }; } return _construct.apply(null, arguments); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
var PromisePool =
/*#__PURE__*/
function () {
_createClass(PromisePool, null, [{
key: "create",
/**
* Create a new instance of the class with the specified arguments.
* @param {...any} args
* @returns {PromisePool}
*/
value: function create() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _construct(this, args);
}
/**
* Instantiate the PromisePool with the desired concurrency.
* @param {Object} opts
* @param {number} opts.concurrency - How many tasks to run at once.
* @param {PromiseConstructor} [opts.PromiseClass=Promise] - The Promise class to use. It must support `Promise.resolve().finally(() => {})`. If you are using Node v10 or above, you don't have to modify this, as the default `Promise` class already supports `.finally`. An alternative to passing in a custom `PromiseClass`, is to polyfill the builtin `Promise` class.
*/
}]);
function PromisePool(_ref) {
var concurrency = _ref.concurrency,
_ref$PromiseClass = _ref.PromiseClass,
PromiseClass = _ref$PromiseClass === void 0 ? Promise : _ref$PromiseClass;
_classCallCheck(this, PromisePool);
/**
* How many tasks to run at once.
* @type {number}
* @private
*/
this.concurrency = concurrency;
/**
* The Promise class to use.
* It must support `Promise.resolve().finally(() => {})`.
* @type {PromiseConstructor}
* @private
*/
this.PromiseClass = PromiseClass;
/**
* How many tasks are currently running.
* @type {number}
* @private
*/
this.running = 0;
/**
* How many tasks have been started.
* @type {number}
* @private
*/
this.started = 0;
/**
* For upcoming tasks, they have a function (a promise resolver) inserted into the queue which starts the task when called.
* @type {Array<Function>}
* @private
*/
this.queue = [];
}
/**
* Add a task to the pool.
* @param {Task} task - The task to be executed when the pool permits.
* @return {Promise} Returns a promise that resolves once the task has resolved or rejected. You should `.catch` it in case your task fails.
*/
_createClass(PromisePool, [{
key: "open",
value: function open(task) {
var _this = this;
// Grab the class to use to create our promise,
// so that the consumer can ensure `.finally` exists.
var Promise = this.PromiseClass; // Create our promise and push its resolver to the queue.
// This has the effect that we can queue its execution for later, instead of right now.
var p = new Promise(function (resolve) {
return _this.queue.push(resolve);
}) // Once the resolver has fired, update the counts accordingly.
.finally(function () {
_this.started--;
_this.running++;
}) // Fire our our task and store the result.
.then(task) // Update our counts accordingly, and start the next queue item if there are any.
.finally(function () {
_this.running--;
if (_this.queue.length) {
_this.started++;
_this.queue.shift()();
}
}); // If our pool is under capacity, then start the first item in the queue.
if (this.running + this.started < this.concurrency && this.queue.length) {
this.started++;
this.queue.shift()();
} // Return the the promise that wraps the task,
// such that it resolves once the task has compelted and our wrappers have completed.
// This allows the user to do `Promise.all(Array<Task>.map((task) => pool.open(task)))`,
// so that they can queue something for when all their pooled tasks are completed.
// It also allows the user to do the mandatory `.catch` handling for task failures.
return p;
}
}]);
return PromisePool;
}();
module.exports = PromisePool;
/** Promise Pool */
export default class PromisePool {
/** Instantiate the PromisePool with the desired concurrency. */
constructor(concurrency) {
this.concurrency = concurrency
this.running = 0
this.started = 0
this.queue = []
}
/** Add a task to the pool. */
open(task) {
// Create our promise and push its resolver to the queue.
// This has the effect that we can queue its execution for later, instead of right now.
const p = new Promise((resolve) => this.queue.push(resolve))
// Once the resolver has fired, update the counts accordingly.
.finally(() => {
this.started--
this.running++
})
// Fire our our task and store the result.
.then(task)
// Update our counts accordingly, and start the next queue item if there are any.
.finally(() => {
this.running--
if (this.queue.length) {
this.started++
const resolver = this.queue.shift()
resolver()
}
})
// If our pool is under capacity, then start the first item in the queue.
if (this.running + this.started < this.concurrency && this.queue.length) {
this.started++
const resolver = this.queue.shift()
resolver()
}
// Return the the promise that wraps the task,
// such that it resolves once the task has compelted and our wrappers have completed.
// This allows the user to do `Promise.all(Array<Task>.map((task) => pool.open(task)))`,
// so that they can queue something for when all their pooled tasks are completed.
// It also allows the user to do the mandatory `.catch` handling for task failures.
return p
}
}
# History
## v3.0.0 2020 April 19
- Rewrote in TypeScript
- `new PromisePool({concurrency})` has changed to `new PromisePool(concurrency)`
- Support for specifying the promise class has been removed now that Node v10 is now the minimum supported LTS version
- Updated dependencies, [base files](https://github.com/bevry/base), and [editions](https://editions.bevry.me) using [boundation](https://github.com/bevry/boundation)
## v2.3.0 2019 December 10

@@ -4,0 +11,0 @@

{
"name": "native-promise-pool",
"version": "2.3.0",
"version": "3.0.0-next.1587290706.8352c90844b025b38b9519ddf029ff56bf4eec00",
"description": "Create a pool of a specified concurrency that accepts functions that return promises",

@@ -9,8 +9,14 @@ "homepage": "https://github.com/bevry/native-promise-pool",

"async",
"browser",
"concurrency",
"export-default",
"flow",
"flow control",
"module",
"pool",
"promise",
"promises",
"pool",
"concurrency",
"flow",
"flow control"
"typed",
"types",
"typescript"
],

@@ -54,4 +60,4 @@ "badges": {

"contributors": [
"Benjamin Lupton (http://balupton.com)",
"dependabot-preview[bot] (http://github.com/apps/dependabot-preview)"
"Benjamin Lupton <b@lupton.cc>",
"Benjamin Lupton (http://balupton.com)"
],

@@ -66,65 +72,78 @@ "bugs": {

"engines": {
"node": ">=8"
"node": ">=10"
},
"editions": [
{
"description": "esnext source code with require for modules",
"description": "TypeScript source code with Import for modules",
"directory": "source",
"entry": "index.ts",
"tags": [
"typescript",
"import"
],
"engines": false
},
{
"description": "TypeScript compiled against ESNext for web browsers with Import for modules",
"directory": "edition-browsers",
"entry": "index.js",
"tags": [
"javascript",
"esnext",
"require"
"import"
],
"engines": {
"node": "8 || 10 || 12",
"browsers": false
"node": false,
"browsers": "defaults"
}
},
{
"description": "esnext compiled for browsers with require for modules",
"directory": "edition-browsers",
"description": "TypeScript compiled against ESNext for Node.js with Require for modules",
"directory": "edition-esnext",
"entry": "index.js",
"tags": [
"javascript",
"esnext",
"require"
],
"engines": {
"node": false,
"browsers": "defaults"
"node": "10 || 12",
"browsers": false
}
}
],
"types": "source/index.ts",
"type": "commonjs",
"main": "source/index.js",
"main": "edition-esnext/index.js",
"browser": "edition-browsers/index.js",
"module": "edition-browsers/index.js",
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/plugin-proposal-object-rest-spread": "^7.7.4",
"@babel/preset-env": "^7.7.6",
"assert-helpers": "^5.8.0",
"bluebird": "^3.7.2",
"chalk": "^3.0.0",
"eslint": "^6.7.2",
"eslint-config-bevry": "^2.3.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-prettier": "^3.1.1",
"jsdoc": "^3.6.3",
"@bevry/update-contributors": "^1.0.1",
"@typescript-eslint/eslint-plugin": "^2.28.0",
"@typescript-eslint/parser": "^2.28.0",
"assert-helpers": "^6.0.0",
"chalk": "^4.0.0",
"eslint": "^6.8.0",
"eslint-config-bevry": "^3.0.0",
"eslint-config-prettier": "^6.10.1",
"eslint-plugin-prettier": "^3.1.3",
"kava": "^4.4.0",
"logger-clearable": "^2.3.0",
"minami": "^1.2.3",
"prettier": "^1.19.1",
"projectz": "^1.15.0",
"prettier": "^2.0.4",
"projectz": "^1.19.0",
"surge": "^0.21.3",
"valid-directory": "^1.5.0"
"typedoc": "^0.17.4",
"typescript": "^3.8.3",
"valid-directory": "^1.6.0",
"valid-module": "^1.0.0"
},
"scripts": {
"our:clean": "rm -Rf ./docs ./edition* ./es2015 ./es5 ./out ./.next",
"our:compile": "npm run our:compile:edition-browsers",
"our:compile:edition-browsers": "env BABEL_ENV=edition-browsers babel --out-dir ./edition-browsers ./source",
"our:compile": "npm run our:compile:edition-browsers && npm run our:compile:edition-esnext",
"our:compile:edition-browsers": "tsc --module ESNext --target ESNext --outDir ./edition-browsers --project tsconfig.json && test -d edition-browsers/source && ( mv edition-browsers/source edition-temp && rm -Rf edition-browsers && mv edition-temp edition-browsers ) || true",
"our:compile:edition-esnext": "tsc --module commonjs --target ESNext --outDir ./edition-esnext --project tsconfig.json && test -d edition-esnext/source && ( mv edition-esnext/source edition-temp && rm -Rf edition-esnext && mv edition-temp edition-esnext ) || true",
"our:deploy": "echo no need for this project",
"our:meta": "npm run our:meta:docs && npm run our:meta:projectz",
"our:meta:docs": "npm run our:meta:docs:jsdoc",
"our:meta:docs:jsdoc": "rm -Rf ./docs && jsdoc --recurse --pedantic --access all --destination ./docs --package ./package.json --readme ./README.md --template ./node_modules/minami ./source && mv ./docs/$npm_package_name/$npm_package_version/* ./docs/ && rm -Rf ./docs/$npm_package_name/$npm_package_version",
"our:meta": "npm run our:meta:contributors && npm run our:meta:docs && npm run our:meta:projectz",
"our:meta:contributors": "update-contributors",
"our:meta:docs": "npm run our:meta:docs:typedoc",
"our:meta:docs:typedoc": "rm -Rf ./docs && typedoc --mode file --exclude '**/+(*test*|node_modules)' --excludeExternals --name \"$npm_package_name\" --readme ./README.md --out ./docs ./source",
"our:meta:projectz": "projectz compile",

@@ -140,7 +159,9 @@ "our:release": "npm run our:release:prepare && npm run our:release:check-changelog && npm run our:release:check-dirty && npm run our:release:tag && npm run our:release:push",

"our:test": "npm run our:verify && npm test",
"our:verify": "npm run our:verify:directory && npm run our:verify:eslint && npm run our:verify:prettier",
"our:verify:directory": "npx valid-directory",
"our:verify": "npm run our:verify:directory && npm run our:verify:eslint && npm run our:verify:module && npm run our:verify:prettier && npm run our:verify:typescript",
"our:verify:directory": "valid-directory",
"our:verify:eslint": "eslint --fix --ignore-pattern '**/*.d.ts' --ignore-pattern '**/vendor/' --ignore-pattern '**/node_modules/' --ext .mjs,.js,.jsx,.ts,.tsx ./source",
"our:verify:prettier": "prettier --write ./source/**",
"test": "node ./source/test.js"
"our:verify:module": "valid-module",
"our:verify:prettier": "prettier --write .",
"our:verify:typescript": "tsc --noEmit --project tsconfig.json",
"test": "node ./edition-esnext/test.js"
},

@@ -155,22 +176,3 @@ "eslintConfig": {

"singleQuote": true
},
"babel": {
"env": {
"edition-browsers": {
"sourceType": "script",
"presets": [
[
"@babel/preset-env",
{
"targets": "defaults",
"modules": false
}
]
],
"plugins": [
"@babel/proposal-object-rest-spread"
]
}
}
}
}

@@ -36,47 +36,18 @@ <!-- TITLE/ -->

<!-- INSTALL/ -->
## Usage
<h2>Install</h2>
[Complete API Documentation.](http://master..bevry.surge.sh/docs/globals.html)
<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>npm</h3></a>
<ul>
<li>Install: <code>npm install --save native-promise-pool</code></li>
<li>Require: <code>require('native-promise-pool')</code></li>
</ul>
To use, just make a promise pool with the specified concurrency, then wrap the method in a `pool.open(() => /* the code that you want pooled */)`. The callback is necessary, as otherwise the code that you want pooled will execute before it is pooled. By pooling it, it executes once the pool entry is called.
<a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a>
``` html
<script type="module">
import * as pkg from '//dev.jspm.io/native-promise-pool'
</script>
```javascript
import PromisePool from 'native-promise-pool'
const pool = new PromisePool(2) // 5 tasks at once
await Promise.all([
pool.open(() => /* the thing that takes a while */)
pool.open(() => /* the thing that takes a while */)
pool.open(() => /* the thing that takes a while, will run after the first two have completed */)
])
```
<h3><a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3>
<p>This package is published with the following editions:</p>
<ul><li><code>native-promise-pool</code> aliases <code>native-promise-pool/source/index.js</code></li>
<li><code>native-promise-pool/source/index.js</code> is esnext source code with require for modules</li>
<li><code>native-promise-pool/edition-browsers/index.js</code> is esnext compiled for browsers with require for modules</li></ul>
<h3><a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a></h3>
This project provides its type information via inline <a href="http://usejsdoc.org" title="JSDoc is an API documentation generator for JavaScript, similar to Javadoc or phpDocumentor">JSDoc Comments</a>. To make use of this in <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a>, set your <code>maxNodeModuleJsDepth</code> compiler option to `5` or thereabouts. You can accomlish this via your `tsconfig.json` file like so:
``` json
{
"compilerOptions": {
"maxNodeModuleJsDepth": 5
}
}
```
<!-- /INSTALL -->
## Usage
[API Documentation.](http://master.native-promise-pool.bevry.surge.sh/docs/)
### Table Example

@@ -104,21 +75,56 @@

On older environments, you can provide compatibility via two methods.
On older environments, you must use a shim, like so:
By passing `PromiseClass`:
```javascript
require('promise.prototype.finally').shim()
const pool = require('native-promise-pool').create({ concurrency: 2 })
```
``` javascript
const pool = require('native-promise-pool').create({
concurrency: 2,
PromiseClass: require('bluebird')
})
<!-- INSTALL/ -->
<h2>Install</h2>
<a href="https://npmjs.com" title="npm is a package manager for javascript"><h3>npm</h3></a>
<ul>
<li>Install: <code>npm install --save native-promise-pool</code></li>
<li>Import: <code>import pkg from ('native-promise-pool')</code></li>
<li>Require: <code>const pkg = require('native-promise-pool').default</code></li>
</ul>
<a href="https://www.pika.dev/cdn" title="100% Native ES Modules CDN"><h3>pika</h3></a>
``` html
<script type="module">
import pkg from '//cdn.pika.dev/native-promise-pool/^3.0.0'
</script>
```
Or by adding direct support to the builtin `Promise` class:
<a href="https://unpkg.com" title="unpkg is a fast, global content delivery network for everything on npm"><h3>unpkg</h3></a>
``` javascript
require('promise.prototype.finally').shim()
const pool = require('native-promise-pool').create({concurrency: 2})
``` html
<script type="module">
import pkg from '//unpkg.com/native-promise-pool@^3.0.0'
</script>
```
<a href="https://jspm.io" title="Native ES Modules CDN"><h3>jspm</h3></a>
``` html
<script type="module">
import pkg from '//dev.jspm.io/native-promise-pool@3.0.0'
</script>
```
<h3><a href="https://editions.bevry.me" title="Editions are the best way to produce and consume packages you care about.">Editions</a></h3>
<p>This package is published with the following editions:</p>
<ul><li><code>native-promise-pool/source/index.ts</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> source code with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li>
<li><code>native-promise-pool/edition-browsers/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> for web browsers with <a href="https://babeljs.io/docs/learn-es2015/#modules" title="ECMAScript Modules">Import</a> for modules</li>
<li><code>native-promise-pool</code> aliases <code>native-promise-pool/edition-esnext/index.js</code></li>
<li><code>native-promise-pool/edition-esnext/index.js</code> is <a href="https://www.typescriptlang.org/" title="TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. ">TypeScript</a> compiled against <a href="https://en.wikipedia.org/wiki/ECMAScript#ES.Next" title="ECMAScript Next">ESNext</a> for <a href="https://nodejs.org" title="Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine">Node.js</a> with <a href="https://nodejs.org/dist/latest-v5.x/docs/api/modules.html" title="Node/CJS Modules">Require</a> for modules</li></ul>
<!-- /INSTALL -->
<!-- HISTORY/ -->

@@ -170,4 +176,5 @@

<ul><li><a href="http://balupton.com">Benjamin Lupton</a></li>
<li><a href="http://github.com/apps/dependabot-preview">dependabot-preview[bot]</a></li></ul>
<ul><li>Benjamin Lupton</li>
<li><a href="http://balupton.com">Benjamin Lupton</a> — <a href="https://github.com/bevry/native-promise-pool/commits?author=balupton" title="View the GitHub contributions of Benjamin Lupton on repository bevry/native-promise-pool">view contributions</a></li>
<li><a href="http://github.com/apps/dependabot-preview">dependabot-preview[bot]</a> — <a href="https://github.com/bevry/native-promise-pool/commits?author=dependabot-preview[bot]" title="View the GitHub contributions of dependabot-preview[bot] on repository bevry/native-promise-pool">view contributions</a></li></ul>

@@ -174,0 +181,0 @@ <a href="https://github.com/bevry/native-promise-pool/blob/master/CONTRIBUTING.md#files">Discover how you can contribute by heading on over to the <code>CONTRIBUTING.md</code> file.</a>

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc