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 0.0.1 to 0.1.0

.babelrc

59

package.json
{
"name" : "queue-promise",
"version" : "0.0.1",
"keywords" : [ "queue", "promise" ],
"description" : "A simple and small library for promise-based queues",
"author" : "Łaniewski Bartosz <laniewski.bartozzz@gmail.com> (http://laniewski.me/)",
"license" : "MIT",
"main" : "src/index.js",
"repository" : {
"type" : "git",
"url" : "https://github.com/Bartozzz/queue-promise.git"
"name": "queue-promise",
"version": "0.1.0",
"keywords": [
"queue",
"promise"
],
"description": "A simple and small library for promise-based queues",
"author": "Łaniewski Bartosz <laniewski.bartozzz@gmail.com> (http://laniewski.me/)",
"license": "MIT",
"main": "dist/index.js",
"repository": {
"type": "git",
"url": "https://github.com/Bartozzz/queue-promise.git"
},
"bugs" : {
"url" : "https://github.com/Bartozzz/queue-promise/issues"
"bugs": {
"url": "https://github.com/Bartozzz/queue-promise/issues"
},
"dependencies" : {
"event-emitter" : "latest",
"basic-collection" : "latest"
"devDependencies": {
"mocha": "^3.5.0",
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-plugin-add-module-exports": "^0.2.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-plugin-transform-class-properties": "^6.24.1"
},
"devDependencies" : {
"mocha" : "latest"
},
"scripts" : {
"test" : "mocha"
"scripts": {
"test": "mocha",
"clean": "rm -rf dist",
"build": "babel src -d dist",
"watch": "babel src -d dist -w",
"prepare": "npm run clean && npm run build"
}
}

@@ -1,5 +0,13 @@

# queue-promise
<div align="center">
<h1>queue-promise</h1>
> A simple and small library for promise-based queues.
[![Greenkeeper badge](https://badges.greenkeeper.io/Bartozzz/queue-promise.svg)](https://greenkeeper.io/)
[![Build Status](https://img.shields.io/travis/Bartozzz/queue-promise.svg)](https://travis-ci.org/Bartozzz/queue-promise/)
[![npm version](https://img.shields.io/npm/v/queue-promise.svg)](https://www.npmjs.com/package/queue-promise)
[![npm downloads](https://img.shields.io/npm/dt/queue-promise.svg)](https://www.npmjs.com/package/queue-promise)
<br>
A simple and small library for promise-based queues.
</div>
## Installation

@@ -11,21 +19,7 @@

This module uses several ECMAScript 2015 (ES6) features. You'll need the latest Node.js version in order to make it work correctly. You might need to run the script with the `--harmony` flag, for example:
```bash
$ node --harmony index.js
```
## Usage
```javascript
"use strict";
import Queue from "queue-promise";
const Queue = require( "./src/index.js" );
const debug = ( msg, err ) => {
return () => new Promise( ( resolve, reject ) => {
if ( err ) reject( `Error: ${msg}` );
resolve( `Success: ${msg}` );
} );
};
const q = new Queue( {

@@ -39,19 +33,9 @@ concurrency : 1,

q.add( debug( "Index 0" ) );
q.add( debug( "Index 1", true ) );
q.add( debug( "Index 2", true ) );
q.add( debug( "Index 3" ) );
q.add( asyncTaskA ); // resolved/rejected after 0s
q.add( asyncTaskB ); // resolved/rejected after 2s
q.add( asyncTaskC ); // resolved/rejected after 4s
q.add( asyncTaskD ); // resolved/rejected after 6s
q.start();
```
Generated output:
```
[ 'Success: Index 0' ] < after 2s
Error: Index 1 < after 4s
Error: Index 2 < after 6s
[ 'Success: Index 3' ] < after 8s
```
## API

@@ -63,6 +47,6 @@

| Option | Default | Description |
|:------------|:--------|:----------------------------------------------|
| concurrency | 10 | How many promises can be handled at the same time |
| interval | 250 | How often should new promises be handled (in ms) |
| Option | Default | Description |
|:------------|:--------|:--------------------------------------------------|
| concurrency | 5 | How many promises can be handled at the same time |
| interval | 500 | How often should new promises be handled (in ms) |

@@ -69,0 +53,0 @@ #### **public** .add( promise )

@@ -1,41 +0,65 @@

"use strict";
import EventEmitter from "events";
const EventEmitter = require( "event-emitter" );
const RequestCollection = require( "./collection/requestCollection" );
export default class Queue extends EventEmitter {
/**
* A collection to store unresolved promises in.
*
* @type {Map}
*/
collection = new Map;
/**
* Queue class for promises.
*
* @author Łaniewski Bartosz <laniewski.bartozzz@gmail.com> (//laniewski.me)
* @copyright Copyright (c) 2016 Łaniewski Bartosz
* @license MIT
*/
/**
* Used to generate unique id for each promise.
*
* @type {number}
*/
unique = 0;
class Queue extends RequestCollection {
/**
* Create a new `Queue` instance with optionally injected options.
* Amount of promises currently handled.
*
* @param object options
* @param int options.concurrency
* @param int options.interval
* @type {number}
*/
current = 0;
/**
* Whenever the queue has started.
*
* @type {boolean}
*/
started = false;
/**
* Queue interval.
*
* @type {Interval}
*/
interval = null;
/**
* Initializes a new Queue instance with provided options.
*
* @param {object} options
* @param {number} options.concurrency - how many promises can be handled at the same time
* @param {number} options.interval - how often should new promises be handled (in ms)
* @access public
*/
constructor( options ) {
constructor( options = {} ) {
super();
this.options = Object.assign( {
// Default options:
this.options = {
concurrency : 5,
interval : 500
}, options );
this.events = new EventEmitter;
this.current = 0;
this.started = false;
this.interval = null;
interval : 500,
...options
};
}
/**
* Starts the queue.
* Starts the queue if it has not been started yet.
*
* @emits start|tick|request|error
* @emits start
* @emits tick
* @emits request
* @emits error
* @access public

@@ -48,9 +72,10 @@ */

this.events.emit( "start" );
this.emit( "start" );
this.started = true;
this.interval = setInterval( () => {
this.events.emit( "tick" );
this.emit( "tick" );
this.each( ( promise, id ) => {
this.collection.forEach( ( promise, id ) => {
// Maximum amount of parallel concurrencies:
if ( this.current + 1 > this.options.concurrency ) {

@@ -65,11 +90,12 @@ return;

.then( ( ...output ) => {
this.events.emit( "resolve", ...output );
this.next();
this.emit( "resolve", ...output );
} )
.catch( ( error ) => {
this.events.emit( "reject", error );
.catch( error => {
this.emit( "reject", error );
} )
.then( () => {
this.next();
} );
} );
}, this.options.interval );
}, parseInt( this.options.interval ) );
}

@@ -84,3 +110,3 @@

stop() {
this.events.emit( "stop" );
this.emit( "stop" );

@@ -98,4 +124,4 @@ this.started = false;

next() {
if ( --this.current === 0 && this.size === 0 ) {
this.events.emit( "end" );
if ( --this.current === 0 && this.collection.size === 0 ) {
this.emit( "end" );
this.stop();

@@ -106,13 +132,24 @@ }

/**
* Sets a `callback` for an `event`.
* Adds a promise to the queue.
*
* @param event Event name
* @param callback Event callback
* @access public
* @param {Promise} promise - Promise to add to the queue
* @throws {Error} - when the promise is not a function
*/
on( event, callback ) {
this.events.on( event, callback );
add( promise ) {
if ( Promise.resolve( promise ) == promise ) {
throw new Error( `You must provide a valid Promise, not ${typeof promise}.` );
}
this.collection.set( this.unique++, promise );
}
};
module.exports = Queue;
/**
* Removes a promise from the queue.
*
* @param {number} key - Promise id
* @return {bool}
*/
remove( key ) {
return this.collection.delete( key );
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc