queue-promise
Advanced tools
Comparing version 0.1.2 to 1.0.0
@@ -7,46 +7,25 @@ "use strict"; | ||
var _typeof2 = require("babel-runtime/helpers/typeof"); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _typeof3 = _interopRequireDefault(_typeof2); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _promise = require("babel-runtime/core-js/promise"); | ||
var _createClass = 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _promise2 = _interopRequireDefault(_promise); | ||
var _events = require("events"); | ||
var _extends2 = require("babel-runtime/helpers/extends"); | ||
var _events2 = _interopRequireDefault(_events); | ||
var _extends3 = _interopRequireDefault(_extends2); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _map = require("babel-runtime/core-js/map"); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var _map2 = _interopRequireDefault(_map); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
var _getPrototypeOf = require("babel-runtime/core-js/object/get-prototype-of"); | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); | ||
var _classCallCheck2 = require("babel-runtime/helpers/classCallCheck"); | ||
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); | ||
var _createClass2 = require("babel-runtime/helpers/createClass"); | ||
var _createClass3 = _interopRequireDefault(_createClass2); | ||
var _possibleConstructorReturn2 = require("babel-runtime/helpers/possibleConstructorReturn"); | ||
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); | ||
var _inherits2 = require("babel-runtime/helpers/inherits"); | ||
var _inherits3 = _interopRequireDefault(_inherits2); | ||
var _events = require("events"); | ||
var _events2 = _interopRequireDefault(_events); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
/** | ||
* A simple and small library for promise-based queues. | ||
*/ | ||
var Queue = function (_EventEmitter) { | ||
(0, _inherits3.default)(Queue, _EventEmitter); | ||
_inherits(Queue, _EventEmitter); | ||
@@ -57,5 +36,6 @@ /** | ||
* @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 | ||
* @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) | ||
*/ | ||
@@ -72,19 +52,27 @@ | ||
/** | ||
* Used to generate unique id for each promise. | ||
* Amount of promises currently handled. | ||
* | ||
* @type {number} | ||
*/ | ||
/** | ||
* A collection to store unresolved promises in. | ||
* | ||
* @type {Map} | ||
*/ | ||
function Queue() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
(0, _classCallCheck3.default)(this, Queue); | ||
_classCallCheck(this, Queue); | ||
// Default options: | ||
var _this = (0, _possibleConstructorReturn3.default)(this, (Queue.__proto__ || (0, _getPrototypeOf2.default)(Queue)).call(this)); | ||
var _this = _possibleConstructorReturn(this, (Queue.__proto__ || Object.getPrototypeOf(Queue)).call(this)); | ||
_this.collection = new _map2.default(); | ||
_this.collection = new Map(); | ||
_this.unique = 0; | ||
_this.current = 0; | ||
_this.options = {}; | ||
_this.started = false; | ||
_this.interval = null; | ||
_this.options = (0, _extends3.default)({ | ||
_this.interval = 0; | ||
_this.options = _extends({ | ||
concurrency: 5, | ||
@@ -103,3 +91,2 @@ interval: 500 | ||
* @emits error | ||
* @access public | ||
*/ | ||
@@ -111,3 +98,3 @@ | ||
* | ||
* @type {Interval} | ||
* @type {number} | ||
*/ | ||
@@ -117,15 +104,16 @@ | ||
/** | ||
* Amount of promises currently handled. | ||
* Queue config. | ||
* | ||
* @type {number} | ||
* @type {Object} | ||
*/ | ||
/** | ||
* A collection to store unresolved promises in. | ||
* Used to generate unique id for each promise. | ||
* | ||
* @type {Map} | ||
* @type {number} | ||
*/ | ||
(0, _createClass3.default)(Queue, [{ | ||
_createClass(Queue, [{ | ||
key: "start", | ||
@@ -173,3 +161,2 @@ value: function start() { | ||
* @emits stop | ||
* @access public | ||
*/ | ||
@@ -182,4 +169,6 @@ | ||
clearInterval(this.interval); | ||
this.started = false; | ||
this.interval = clearInterval(this.interval); | ||
this.interval = 0; | ||
} | ||
@@ -191,3 +180,2 @@ | ||
* @emits end | ||
* @access private | ||
*/ | ||
@@ -207,4 +195,4 @@ | ||
* | ||
* @param {Promise} promise - Promise to add to the queue | ||
* @throws {Error} - when the promise is not a function | ||
* @param {Promise} promise Promise to add to the queue | ||
* @throws {Error} when the promise is not a function | ||
*/ | ||
@@ -215,4 +203,4 @@ | ||
value: function add(promise) { | ||
if (_promise2.default.resolve(promise) == promise) { | ||
throw new Error("You must provide a valid Promise, not " + (typeof promise === "undefined" ? "undefined" : (0, _typeof3.default)(promise)) + "."); | ||
if (Promise.resolve(promise) == promise) { | ||
throw new Error("You must provide a valid Promise, not " + (typeof promise === "undefined" ? "undefined" : _typeof(promise)) + "."); | ||
} | ||
@@ -226,4 +214,4 @@ | ||
* | ||
* @param {number} key - Promise id | ||
* @return {bool} | ||
* @param {number} key Promise id | ||
* @return {boolean} | ||
*/ | ||
@@ -237,2 +225,3 @@ | ||
}]); | ||
return Queue; | ||
@@ -239,0 +228,0 @@ }(_events2.default); |
{ | ||
"name": "queue-promise", | ||
"version": "0.1.2", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
"queue", | ||
"promise" | ||
"queue-tasks", | ||
"queue-request", | ||
"promise", | ||
"promise-chain", | ||
"promise-queue" | ||
], | ||
@@ -23,19 +27,25 @@ "description": "A simple and small library for promise-based queues", | ||
"devDependencies": { | ||
"mocha": "^3.5.0", | ||
"flow-bin": "^0.56.0", | ||
"mocha": "^4.0.0", | ||
"babel": "^6.23.0", | ||
"babel-cli": "^6.26.0", | ||
"babel-eslint": "^8.0.1", | ||
"babel-preset-flow": "^6.23.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" | ||
"babel-plugin-transform-class-properties": "^6.24.1", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"eslint": "^4.8.0", | ||
"eslint-config-google": "^0.9.1", | ||
"eslint-plugin-flowtype": "^2.35.0", | ||
"eslint-plugin-import": "^2.7.0" | ||
}, | ||
"scripts": { | ||
"flow": "flow", | ||
"clean": "rm -rf dist", | ||
"build": "babel src -d dist", | ||
"watch": "babel src -d dist -w", | ||
"prepare": "npm run clean && npm run build" | ||
"eslint": "node_modules/.bin/eslint --fix src", | ||
"prepare": "npm run clean && npm run eslint && npm run flow && npm run build" | ||
} | ||
} |
118
src/index.js
@@ -0,3 +1,8 @@ | ||
// @flow | ||
import EventEmitter from "events"; | ||
/** | ||
* A simple and small library for promise-based queues. | ||
*/ | ||
export default class Queue extends EventEmitter { | ||
@@ -9,3 +14,3 @@ /** | ||
*/ | ||
collection = new Map; | ||
collection: Map<number, () => Promise<*>> = new Map; | ||
@@ -17,3 +22,3 @@ /** | ||
*/ | ||
unique = 0; | ||
unique: number = 0; | ||
@@ -25,5 +30,12 @@ /** | ||
*/ | ||
current = 0; | ||
current: number = 0; | ||
/** | ||
* Queue config. | ||
* | ||
* @type {Object} | ||
*/ | ||
options: Object = {}; | ||
/** | ||
* Whenever the queue has started. | ||
@@ -33,3 +45,3 @@ * | ||
*/ | ||
started = false; | ||
started: boolean = false; | ||
@@ -39,5 +51,5 @@ /** | ||
* | ||
* @type {Interval} | ||
* @type {number} | ||
*/ | ||
interval = null; | ||
interval: number = 0; | ||
@@ -48,7 +60,8 @@ /** | ||
* @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 | ||
* @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) | ||
*/ | ||
constructor( options = {} ) { | ||
constructor(options: Object = {}): void { | ||
super(); | ||
@@ -58,5 +71,5 @@ | ||
this.options = { | ||
concurrency : 5, | ||
interval : 500, | ||
...options | ||
concurrency: 5, | ||
interval: 500, | ||
...options, | ||
}; | ||
@@ -72,18 +85,17 @@ } | ||
* @emits error | ||
* @access public | ||
*/ | ||
start() { | ||
if ( this.started ) { | ||
start(): void { | ||
if (this.started) { | ||
return; | ||
} | ||
this.emit( "start" ); | ||
this.emit("start"); | ||
this.started = true; | ||
this.interval = setInterval( () => { | ||
this.emit( "tick" ); | ||
this.started = true; | ||
this.interval = setInterval(() => { | ||
this.emit("tick"); | ||
this.collection.forEach( ( promise, id ) => { | ||
this.collection.forEach((promise, id) => { | ||
// Maximum amount of parallel concurrencies: | ||
if ( this.current + 1 > this.options.concurrency ) { | ||
if (this.current + 1 > this.options.concurrency) { | ||
return; | ||
@@ -93,16 +105,16 @@ } | ||
this.current++; | ||
this.remove( id ); | ||
this.remove(id); | ||
promise() | ||
.then( ( ...output ) => { | ||
this.emit( "resolve", ...output ); | ||
} ) | ||
.catch( error => { | ||
this.emit( "reject", error ); | ||
} ) | ||
.then( () => { | ||
.then((...output) => { | ||
this.emit("resolve", ...output); | ||
}) | ||
.catch((error) => { | ||
this.emit("reject", error); | ||
}) | ||
.then(() => { | ||
this.next(); | ||
} ); | ||
} ); | ||
}, parseInt( this.options.interval ) ); | ||
}); | ||
}); | ||
}, parseInt(this.options.interval)); | ||
} | ||
@@ -114,9 +126,10 @@ | ||
* @emits stop | ||
* @access public | ||
*/ | ||
stop() { | ||
this.emit( "stop" ); | ||
stop(): void { | ||
this.emit("stop"); | ||
this.started = false; | ||
this.interval = clearInterval( this.interval ); | ||
clearInterval(this.interval); | ||
this.started = false; | ||
this.interval = 0; | ||
} | ||
@@ -128,7 +141,6 @@ | ||
* @emits end | ||
* @access private | ||
*/ | ||
next() { | ||
if ( --this.current === 0 && this.collection.size === 0 ) { | ||
this.emit( "end" ); | ||
next(): void { | ||
if (--this.current === 0 && this.collection.size === 0) { | ||
this.emit("end"); | ||
this.stop(); | ||
@@ -141,11 +153,13 @@ } | ||
* | ||
* @param {Promise} promise - Promise to add to the queue | ||
* @throws {Error} - when the promise is not a function | ||
* @param {Promise} promise Promise to add to the queue | ||
* @throws {Error} when the promise is not a function | ||
*/ | ||
add( promise ) { | ||
if ( Promise.resolve( promise ) == promise ) { | ||
throw new Error( `You must provide a valid Promise, not ${typeof promise}.` ); | ||
add(promise: () => Promise<*>): void { | ||
if (Promise.resolve(promise) == promise) { | ||
throw new Error( | ||
`You must provide a valid Promise, not ${typeof promise}.` | ||
); | ||
} | ||
this.collection.set( this.unique++, promise ); | ||
this.collection.set(this.unique++, promise); | ||
} | ||
@@ -156,8 +170,8 @@ | ||
* | ||
* @param {number} key - Promise id | ||
* @return {bool} | ||
* @param {number} key Promise id | ||
* @return {boolean} | ||
*/ | ||
remove( key ) { | ||
return this.collection.delete( key ); | ||
remove(key: number): boolean { | ||
return this.collection.delete(key); | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
16951
12
340
1
14