Skip to content

Commit

Permalink
Added tests, test-coverage and license
Browse files Browse the repository at this point in the history
  • Loading branch information
JMPerez committed Feb 17, 2015
1 parent d9747fc commit 6240b63
Show file tree
Hide file tree
Showing 11 changed files with 609 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmignore
@@ -1,2 +1,3 @@
.jshintrc
.npmignore
node_modules
6 changes: 6 additions & 0 deletions .travis.yml
@@ -0,0 +1,6 @@
language: node_js
node_js:
- "0.11"
- "0.10"
- "0.8"
after_success: make coveralls
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 José M. Pérez

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions Makefile
@@ -0,0 +1,27 @@
# ==============================================================================
# Node Tests
# ==============================================================================

REPORTER = spec

test:
@NODE_ENV=test NODE_PATH=lib ./node_modules/.bin/mocha \
--reporter $(REPORTER)

coverage:
@NODE_ENV=test NODE_PATH=lib ./node_modules/.bin/mocha \
--require blanket \
--reporter html-cov > ./test/coverage.html

coveralls:
$(MAKE) test

@NODE_ENV=test NODE_PATH=lib ./node_modules/.bin/mocha \
--require blanket \
--reporter mocha-lcov-reporter | ./node_modules/coveralls/bin/coveralls.js

# ==============================================================================
# Static Analysis
# ==============================================================================

.PHONY: test coverage
10 changes: 8 additions & 2 deletions README.md
Expand Up @@ -5,6 +5,12 @@ This is a small library to limit the amount of promises run per unit of time. It

It doesn't have any dependencies. If you are running this on Node.js, you will need to pass whatever Promise library you are using in the constructor.

Then, you add functions to the `PromiseThrottle` that, once called, return a `Promise`.

## Installation

Install the module with: `npm install promise-throttle

## Example

```javascript
Expand All @@ -27,6 +33,6 @@ It doesn't have any dependencies. If you are running this on Node.js, you will n

```
## Todo
## License
- [] Tests
MIT
89 changes: 89 additions & 0 deletions lib/main.js
@@ -0,0 +1,89 @@
/* exported PromiseThrottle */

'use strict';

/**
* @constructor
* @param {number} requestsPerSecond The amount of requests per second
* the library will limit to
*/
function PromiseThrottle(options) {
this.requestsPerSecond = options.requestsPerSecond;
this.promiseImplementation = options.promiseImplementation || Promise;
this.startTimesArray = [];
this.queued = [];
}

/**
* Adds a promise
* @param {Promise} promise The promise to be added
*/
PromiseThrottle.prototype.add = function (promise) {
var self = this;
return new self.promiseImplementation(function(resolve, reject) {
self.queued.push({
resolve: resolve,
reject: reject,
promise: promise
});

self.dequeue();
});
};

/**
* Adds all the promises passed as parameters
* @param {array} promises An array of promises
*/
PromiseThrottle.prototype.addAll = function (promises) {
promises.forEach(function(promise) {
this.add(promise);
}.bind(this));
};

/**
* Dequeues a promise
*/
PromiseThrottle.prototype.dequeue = function () {
// have we issued more requests than requestsPerSecond
// during last second ?
if (this.queued.length === 0) {
return;
}

if (this.startTimesArray.length < this.requestsPerSecond) {
this._execute();
} else {
var referencePosition = this.startTimesArray.length - this.requestsPerSecond,
timeDiff = (new Date()).getTime() - this.startTimesArray[referencePosition].getTime();
if (timeDiff > 1000) {
this._execute();
} else {
// we have reached the limit, schedule a dequeue operation
//console.log('Scheduling');
var self = this;
setTimeout(function() {
self.dequeue();
}, timeDiff);
}
}
};

/**
* Executes the promise
*/
PromiseThrottle.prototype._execute = function () {
this.startTimesArray.push(new Date());
if (this.startTimesArray.length > this.requestsPerSecond) {
var referencePosition = this.startTimesArray.length - this.requestsPerSecond;
this.startTimesArray = this.startTimesArray.slice(referencePosition);
}
var candidate = this.queued.shift();
candidate.promise().then(function(r) {
candidate.resolve(r);
}).catch(function(r) {
candidate.reject(r);
});
};

module.exports = PromiseThrottle;
4 changes: 2 additions & 2 deletions main.js
Expand Up @@ -20,7 +20,7 @@ function PromiseThrottle(options) {
*/
PromiseThrottle.prototype.add = function (promise) {
var self = this;
return new self.promiseImplementation(function(resolve, reject) {
return new this.promiseImplementation(function(resolve, reject) {
self.queued.push({
resolve: resolve,
reject: reject,
Expand All @@ -38,7 +38,7 @@ PromiseThrottle.prototype.add = function (promise) {
PromiseThrottle.prototype.addAll = function (promises) {
promises.forEach(function(promise) {
this.add(promise);
});
}.bind(this));
};

/**
Expand Down
26 changes: 21 additions & 5 deletions package.json
@@ -1,17 +1,33 @@
{
"name": "promise-throttle",
"version": "0.0.1",
"version": "0.0.2",
"homepage": "https://github.com/jmperez/promise-throttle",
"description": "A library to throttle promises",
"main": "main.js",
"main": "lib/main.js",
"author": "Jose M. Perez",
"license": "MIT",
"repository": {
"type" : "git",
"url" : "https://github.com/jmperez/promise-throttle.git"
"type": "git",
"url": "https://github.com/jmperez/promise-throttle.git"
},
"scripts": {
"test": "make test"
},
"config": {
"blanket": {
"pattern": "//^(?!.*node_modules.*$).*lib//"
}
},
"keywords": [
"promise",
"throttle"
]
],
"devDependencies": {
"mocha-lcov-reporter": "0.0.1",
"mocha": "~2.1.0",
"coveralls": "~2.11.2",
"blanket": "~1.1.6",
"promise": "~6.1.0",
"sinon": "~1.12.2"
}
}

0 comments on commit 6240b63

Please sign in to comment.