Skip to content

Commit

Permalink
1.8.1 Pluggable Promise library
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrondin committed Jul 14, 2015
1 parent 09a7936 commit 9d834e8
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ It returns `true` if the strategy was executed.

### schedule()

Adds a request to the queue. This is the Promise version of `submit`. It uses the built-in [Promise](http://caniuse.com/#feat=promises) object.
Adds a request to the queue. This is the Promise version of `submit`. It uses the [bluebird](https://github.com/petkaantonov/bluebird) package if available and otherwise uses the built-in [Promise](http://caniuse.com/#feat=promises) object.

```js
var fn = function(arg1, arg2, argN) {
Expand All @@ -94,6 +94,15 @@ limiter.schedule(fn, arg1, arg2, argN);

In plain language, `schedule` takes a function fn and a list of arguments. Fn must return a promise. `schedule` returns a promise that will be executed according to the rate limits. It's safe to mix `submit` and `schedule` in the same limiter.

It's also possible to replace the Promise library used.

```js
var Bottleneck = require("bottleneck");
Bottleneck.Promise = myPromiseLibrary;

var limiter = new Bottleneck(maxConcurrent, minTime, highWater, strategy);
```

#### Gotchas

* When using `submit`, if a callback isn't necessary, you must pass `null` or an empty function instead. It will not work if you forget to do this.
Expand Down
15 changes: 13 additions & 2 deletions bottleneck.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
slice = [].slice;

Bottleneck = (function() {
var e;

Bottleneck.strategy = Bottleneck.prototype.strategy = {
LEAK: 1,
OVERFLOW: 2,
Expand All @@ -14,6 +16,15 @@

Bottleneck.Cluster = Bottleneck.prototype.Cluster = require("./Cluster");

Bottleneck.Promise = Bottleneck.prototype.Promise = (function() {
try {
return require("bluebird");
} catch (_error) {
e = _error;
return Promise;
}
})();

function Bottleneck(maxNb, minTime, highWater, strategy) {
this.maxNb = maxNb != null ? maxNb : 0;
this.minTime = minTime != null ? minTime : 0;
Expand Down Expand Up @@ -122,7 +133,7 @@
return cb.apply({}, Array.prototype.concat.call({}, args));
});
};
return new Promise((function(_this) {
return new Bottleneck.prototype.Promise((function(_this) {
return function(resolve, reject) {
return _this.submit.apply({}, Array.prototype.concat.call(wrapped, function() {
var args, error;
Expand Down Expand Up @@ -186,7 +197,7 @@

}).call(this);

},{"./Cluster":2}],2:[function(require,module,exports){
},{"./Cluster":2,"bluebird":undefined}],2:[function(require,module,exports){
// Generated by CoffeeScript 1.9.2
(function() {
var Cluster,
Expand Down
2 changes: 1 addition & 1 deletion bottleneck.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bottleneck",
"main": "bottleneck.js",
"version": "1.7.1",
"version": "1.8.1",
"homepage": "https://github.com/SGrondin/bottleneck",
"authors": [
"SGrondin <github@simongrondin.name>"
Expand Down
13 changes: 12 additions & 1 deletion lib/Bottleneck.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bottleneck",
"version": "1.8.0",
"version": "1.8.1",
"description": "Async rate limiter",
"main": "lib/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion scripts/recompile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ echo 'Compiling bottleneck...'
node_modules/coffee-script/bin/coffee -c src/*.coffee
rm lib/*.js
mv src/*.js lib/
node_modules/browserify/bin/cmd.js lib/index.js > bottleneck.js
node_modules/browserify/bin/cmd.js -u bluebird lib/index.js > bottleneck.js
node_modules/uglify-js/bin/uglifyjs bottleneck.js -o bottleneck.min.js

echo 'Done!'
3 changes: 2 additions & 1 deletion src/Bottleneck.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class Bottleneck
Bottleneck.strategy = Bottleneck::strategy = {LEAK:1, OVERFLOW:2, BLOCK:3}
Bottleneck.Cluster = Bottleneck::Cluster = require "./Cluster"
Bottleneck.Promise = Bottleneck::Promise = try require "bluebird" catch e then Promise
constructor: (@maxNb=0, @minTime=0, @highWater=0, @strategy=Bottleneck::strategy.LEAK) ->
@_nextRequest = Date.now()
@_nbRunning = 0
Expand Down Expand Up @@ -53,7 +54,7 @@ class Bottleneck
(task.apply {}, args)
.then (args...) -> cb.apply {}, Array::concat.call [], null, args
.catch (args...) -> cb.apply {}, Array::concat.call {}, args
new Promise (resolve, reject) =>
new Bottleneck::Promise (resolve, reject) =>
@submit.apply {}, Array::concat.call wrapped, (error, args...) ->
(if error? then reject else resolve).apply {}, args
changeSettings: (@maxNb=@maxNb, @minTime=@minTime, @highWater=@highWater, @strategy=@strategy) ->
Expand Down

0 comments on commit 9d834e8

Please sign in to comment.