Socket
Socket
Sign inDemoInstall

queue-promise

Package Overview
Dependencies
0
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.3.1 to 1.3.2

test/api.test.js

57

dist/index.js

@@ -12,4 +12,6 @@ "use strict";

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

@@ -111,3 +113,3 @@

this.options = _objectSpread({}, this.options, options);
this.options = _objectSpread({}, this.options, {}, options);
this.options.interval = parseInt(this.options.interval, 10);

@@ -130,3 +132,3 @@ this.options.concurrent = parseInt(this.options.concurrent, 10); // Backward compatibility:

start() {
if (!this.started) {
if (!this.started && !this.isEmpty) {
this.emit("start");

@@ -163,3 +165,5 @@ this.stopped = false;

finalize() {
if (--this.currentlyHandled === 0 && this.isEmpty) {
this.currentlyHandled -= 1;
if (this.currentlyHandled === 0 && this.isEmpty) {
this.emit("end");

@@ -183,30 +187,25 @@ this.stop(); // Finalize doesn't force queue to stop as `Queue.stop()` does. New tasks

dequeue() {
async dequeue() {
const promises = [];
this.tasks.forEach((promise, id) => {
// Maximum amount of parallel concurrencies:
if (this.currentlyHandled >= this.options.concurrent) {
return;
// Maximum amount of parallel tasks:
if (this.currentlyHandled < this.options.concurrent) {
this.currentlyHandled++;
this.tasks.delete(id);
promises.push(Promise.resolve(promise()).then(value => {
this.emit("resolve", value);
return value;
}).catch(error => {
this.emit("reject", error);
return error;
}).finally(() => {
this.emit("dequeue");
this.finalize();
}));
}
}); // Note: Promise.all will reject if any of the concurrent promises fails,
// regardless if they are finished yet!
this.currentlyHandled++;
this.tasks.delete(id);
promises.push(Promise.resolve(promise()));
}); // https://github.com/Bartozzz/queue-promise/issues/60
if (promises.length === 0) {
return;
}
return Promise.all(promises).then(values => {
for (let output of values) this.emit("resolve", output);
return values;
}).catch(error => {
this.emit("reject", error);
return error;
}).then(output => {
this.finalize();
return output;
});
const output = await Promise.all(promises);
return this.options.concurrent === 1 ? output[0] : output;
}

@@ -232,3 +231,3 @@ /**

} // Start the queue if the queue should resolve new tasks automatically and
// the queue hasn't been forced to stop:
// hasn't been forced to stop:

@@ -235,0 +234,0 @@

{
"name": "queue-promise",
"version": "1.3.1",
"version": "1.3.2",
"keywords": [

@@ -27,25 +27,25 @@ "queue",

"devDependencies": {
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/plugin-proposal-async-generator-functions": "^7.0.0",
"@babel/plugin-proposal-class-properties": "^7.0.0",
"@babel/plugin-proposal-object-rest-spread": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/plugin-proposal-async-generator-functions": "^7.7.0",
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
"@babel/preset-env": "^7.7.1",
"@babel/preset-flow": "^7.0.0",
"@babel/register": "7.0.0",
"babel-eslint": "^10.0.1",
"babel-plugin-add-module-exports": "^1.0.0",
"@babel/register": "^7.7.0",
"babel-eslint": "^10.0.3",
"babel-plugin-add-module-exports": "^1.0.2",
"chai": "^4.2.0",
"eslint": "^5.6.0",
"eslint-config-prettier": "^3.1.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-flowtype": "^3.0.0",
"eslint-plugin-import": "2.14.0",
"eslint-plugin-node": "^8.0.0",
"eslint-plugin-prettier": "^3.0.0",
"eslint-plugin-promise": "^4.0.0",
"eslint-plugin-standard": "^4.0.0",
"flow-bin": "^0.90.0",
"mocha": "^5.2.0",
"prettier": "^1.14.3"
"eslint": "^6.6.0",
"eslint-config-prettier": "^6.5.0",
"eslint-config-standard": "^14.1.0",
"eslint-plugin-flowtype": "^4.3.0",
"eslint-plugin-import": "^2.18.2",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"flow-bin": "^0.111.3",
"mocha": "^6.2.2",
"prettier": "^1.19.1"
},

@@ -52,0 +52,0 @@ "scripts": {

@@ -10,3 +10,3 @@ <div align="center">

`queue-promise` is a small, dependency-free library for promise-based queues. It will resolve enqueued functions concurrently at a given speed. When a task is being resolved or rejected, an event will be emitted.
`queue-promise` is a small, dependency-free library for promise-based queues. It will resolve enqueued tasks concurrently at a given speed. When a task is being resolved or rejected, an event will be emitted.

@@ -27,10 +27,14 @@ </div>

const queue = new Queue({
// How many tasks should be resolved at a time (defaults to `5`):
// How many tasks should be executed in parallel (defaults to `5`):
concurrent: 1,
// How often should new tasks be resolved (in ms – defaults to `500`):
// How often should new tasks be executed (in ms – defaults to `500`):
interval: 2000,
// If should resolve new tasks automatically when they are added (defaults to `true`):
// If should resolve new tasks automatically when added (defaults to `true`):
start: true
});
queue.on("start", () => /* … */);
queue.on("stop", () => /* … */);
queue.on("end", () => /* … */);
queue.on("resolve", data => console.log(data));

@@ -53,9 +57,9 @@ queue.on("reject", error => console.error(error));

| :----------- | :------ | :-------------------------------------------------------------------------- |
| `concurrent` | `5` | How many tasks can be handled at the same time |
| `interval` | `500` | How often should new tasks be handled (in ms) |
| `start` | `true` | Whether we should automatically resolve new tasks as soon as they are added |
| `concurrent` | `5` | How many tasks should be executed in parallel |
| `interval` | `500` | How often should new tasks be executed (in ms) |
| `start` | `true` | Whether it should automatically resolve new tasks as soon as they are added |
#### **public** `.enqueue(tasks)`/`.add(tasks)`
Puts a new task on the stack. A task should be an async function (ES2017) or return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Throws an error if the provided `task` is not a valid function.
Adds a new task to the queue. A task should be an [async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) (ES2017) or return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Throws an error if the provided `task` is not a valid function.

@@ -101,6 +105,6 @@ **Example:**

```javascript
queue.enqueue([…]);
queue.on("resolve", data => …);
queue.on("reject", error => …);
queue.on("start", () => …);
queue.on("stop", () => …);
queue.on("end", () => …);

@@ -107,0 +111,0 @@ ```

@@ -109,3 +109,3 @@ // @flow

start() {
if (!this.started) {
if (!this.started && !this.isEmpty) {
this.emit("start");

@@ -147,3 +147,5 @@

finalize() {
if (--this.currentlyHandled === 0 && this.isEmpty) {
this.currentlyHandled -= 1;
if (this.currentlyHandled === 0 && this.isEmpty) {
this.emit("end");

@@ -167,35 +169,34 @@ this.stop();

*/
dequeue() {
async dequeue() {
const promises = [];
this.tasks.forEach((promise, id) => {
// Maximum amount of parallel concurrencies:
if (this.currentlyHandled >= this.options.concurrent) {
return;
// Maximum amount of parallel tasks:
if (this.currentlyHandled < this.options.concurrent) {
this.currentlyHandled++;
this.tasks.delete(id);
promises.push(
Promise.resolve(promise())
.then(value => {
this.emit("resolve", value);
return value;
})
.catch(error => {
this.emit("reject", error);
return error;
})
.finally(() => {
this.emit("dequeue");
this.finalize();
})
);
}
this.currentlyHandled++;
this.tasks.delete(id);
promises.push(Promise.resolve(promise()));
});
// https://github.com/Bartozzz/queue-promise/issues/60
if (promises.length === 0) {
return;
}
// Note: Promise.all will reject if any of the concurrent promises fails,
// regardless if they are finished yet!
const output = await Promise.all(promises);
return Promise.all(promises)
.then(values => {
for (let output of values) this.emit("resolve", output);
return values;
})
.catch(error => {
this.emit("reject", error);
return error;
})
.then(output => {
this.finalize();
return output;
});
return this.options.concurrent === 1 ? output[0] : output;
}

@@ -222,3 +223,3 @@

// Start the queue if the queue should resolve new tasks automatically and
// the queue hasn't been forced to stop:
// hasn't been forced to stop:
if (this.options.start && !this.stopped) {

@@ -225,0 +226,0 @@ this.start();

Sorry, the diff of this file is not supported yet

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