Skip to content

Commit

Permalink
Give some love.
Browse files Browse the repository at this point in the history
Abandon Q.
  • Loading branch information
satazor committed Jan 2, 2016
1 parent dd3268b commit b93ea84
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 107 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/node_modules
/npm-debug.*
node_modules
npm-debug.*
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- "0.10"
- "0.8"
- "0.12"
- "4"
- "5"
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2.0.0

- Switch from Q to native promise's
- No more support for promise progress stuff
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
# p-throttler [![Build Status](https://travis-ci.org/IndigoUnited/node-p-throttler.svg?branch=master)](https://travis-ci.org/IndigoUnited/node-p-throttler.svg?branch=master)
# p-throttler

[![NPM version][npm-image]][npm-url] [![Downloads][downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency status][david-dm-image]][david-dm-url] [![Dev Dependency status][david-dm-dev-image]][david-dm-dev-url]

[npm-url]:https://npmjs.org/package/p-throttler
[downloads-image]:http://img.shields.io/npm/dm/p-throttler.svg
[npm-image]:http://img.shields.io/npm/v/p-throttler.svg
[travis-url]:https://travis-ci.org/IndigoUnited/node-p-throttler
[travis-image]:http://img.shields.io/travis/IndigoUnited/node-p-throttler.svg
[david-dm-url]:https://david-dm.org/IndigoUnited/node-p-throttler
[david-dm-image]:https://img.shields.io/david/IndigoUnited/node-p-throttler.svg
[david-dm-dev-url]:https://david-dm.org/IndigoUnited/node-p-throttler#info=devDependencies
[david-dm-dev-image]:https://img.shields.io/david/dev/IndigoUnited/node-p-throttler.svg

A promise based throttler responsible for limiting execution of parallel tasks.
The number of parallel tasks may be limited and configured per type.
Expand Down
65 changes: 36 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

var Q = require('q');
var arrayRemove = require('./lib/arrayRemove');
var Promise = require('promise');

function PThrottler(defaultConcurrency, types) {
this._defaultConcurrency = typeof defaultConcurrency === 'number' ? defaultConcurrency : 10;
Expand All @@ -15,28 +15,36 @@ function PThrottler(defaultConcurrency, types) {
// -----------------

PThrottler.prototype.enqueue = function (func, type) {
var deferred = Q.defer();
var types;
var entry;

type = type || '';
types = Array.isArray(type) ? type : [type];
entry = {
func: func,
types: types,
deferred: deferred
};

// Add the entry to all the types queues
types.forEach(function (type) {
var queue = this._queue[type] = this._queue[type] || [];
queue.push(entry);
}, this);
var deferred = {};
var promise;

promise = new Promise(function (resolve, reject) {
var types;
var entry;

deferred.resolve = resolve;
deferred.reject = reject;

type = type || '';
types = Array.isArray(type) ? type : [type];
entry = {
func: func,
types: types,
deferred: deferred
};
// Add the entry to all the types queues
types.forEach(function (type) {
var queue = this._queue[type] = this._queue[type] || [];
queue.push(entry);
}, this);

// Process the entry shortly later so that handlers can be attached to the returned promise
setImmediate(this._processEntry.bind(this, entry));
}.bind(this));

// Process the entry shortly later so that handlers can be attached to the returned promise
Q.fcall(this._processEntry.bind(this, entry));
deferred.promise = promise;

return deferred.promise;
return promise;
};

PThrottler.prototype.abort = function () {
Expand All @@ -49,11 +57,11 @@ PThrottler.prototype.abort = function () {

// Wait for all pending functions to finish
promises = this._executing.map(function (entry) {
return entry.deferred.promise;
return entry.deferred.promise.catch(function () {}); // Ignore any errors
});

return Q.allSettled(promises)
.then(function () {}); // Resolve with no value
return Promise.all(promises)
.then(function () {});
};

// -----------------
Expand Down Expand Up @@ -86,12 +94,11 @@ PThrottler.prototype._processEntry = function (entry) {

// Execute the function
this._executing.push(entry);
promise = entry.func();
if (typeof promise.then === 'undefined') {
promise = Q.resolve(promise);
}

promise.progress(entry.deferred.notify.bind(entry.deferred));
promise = new Promise(function (resolve, reject) {
Promise.resolve(entry.func())
.then(resolve, reject);
});

promise.then(
this._onFulfill.bind(this, entry, true),
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"description": "A promise based throttler capable of limiting execution of parallel tasks",
"main": "index.js",
"dependencies": {
"q": "~0.9.2"
"promise": "^7.1.1"
},
"devDependencies": {
"mocha": "~1.10.0",
"expect.js": "~0.2.0"
"expect.js": "^0.3.1",
"mocha": "^2.1.0"
},
"scripts": {
"test": "mocha -R spec"
"test": "mocha -R spec --bail"
},
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit b93ea84

Please sign in to comment.