diff --git a/bottleneck.js b/bottleneck.js index 98bcd01..8feef1a 100644 --- a/bottleneck.js +++ b/bottleneck.js @@ -15,19 +15,20 @@ } Bottleneck.prototype._tryToRun = function() { - var done, next, wait; + var done, index, next, wait; if ((this._nbRunning < this.maxNb || this.maxNb <= 0) && this._queue.length > 0) { this._nbRunning++; wait = Math.max(this._nextRequest - Date.now(), 0); this._nextRequest = Date.now() + wait + this.minTime; next = this._queue.shift(); done = false; - return this._timeouts.push(setTimeout((function(_this) { + return index = this._timeouts.push(setTimeout((function(_this) { return function() { return next.task.apply({}, next.args.concat(function() { var _ref; if (!done) { done = true; + _this._timeouts[index - 1] = null; _this._nbRunning--; _this._tryToRun(); return (_ref = next.cb) != null ? _ref.apply({}, Array.prototype.slice.call(arguments, 0)) : void 0; diff --git a/bottleneck.min.js b/bottleneck.min.js index 3093e34..a7e80b3 100644 --- a/bottleneck.min.js +++ b/bottleneck.min.js @@ -1 +1 @@ -(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o0){this._nbRunning++;wait=Math.max(this._nextRequest-Date.now(),0);this._nextRequest=Date.now()+wait+this.minTime;next=this._queue.shift();done=false;return this._timeouts.push(setTimeout(function(_this){return function(){return next.task.apply({},next.args.concat(function(){var _ref;if(!done){done=true;_this._nbRunning--;_this._tryToRun();return(_ref=next.cb)!=null?_ref.apply({},Array.prototype.slice.call(arguments,0)):void 0}}))}}(this),wait))}};Bottleneck.prototype.submit=function(){var args,cb,task,_i;task=arguments[0],args=3<=arguments.length?__slice.call(arguments,1,_i=arguments.length-1):(_i=1,[]),cb=arguments[_i++];this._queue.push({task:task,args:args,cb:cb});return this._tryToRun()};Bottleneck.prototype.changeSettings=function(maxNb,minTime){this.maxNb=maxNb!=null?maxNb:this.maxNb;this.minTime=minTime!=null?minTime:this.minTime;return this};Bottleneck.prototype.stopAll=function(){var a,_i,_len,_ref;_ref=this._timeouts;for(_i=0,_len=_ref.length;_i<_len;_i++){a=_ref[_i];clearTimeout(a)}return this._tryToRun=function(){}};return Bottleneck}();module.exports=Bottleneck}).call(this)},{}],2:[function(require,module,exports){(function(global){(function(){module.exports=require("./Bottleneck");if(global.window!=null){global.window.Bottleneck=module.exports}}).call(this)}).call(this,typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./Bottleneck":1}]},{},[2]); \ No newline at end of file +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o0){this._nbRunning++;wait=Math.max(this._nextRequest-Date.now(),0);this._nextRequest=Date.now()+wait+this.minTime;next=this._queue.shift();done=false;return index=this._timeouts.push(setTimeout(function(_this){return function(){return next.task.apply({},next.args.concat(function(){var _ref;if(!done){done=true;_this._timeouts[index-1]=null;_this._nbRunning--;_this._tryToRun();return(_ref=next.cb)!=null?_ref.apply({},Array.prototype.slice.call(arguments,0)):void 0}}))}}(this),wait))}};Bottleneck.prototype.submit=function(){var args,cb,task,_i;task=arguments[0],args=3<=arguments.length?__slice.call(arguments,1,_i=arguments.length-1):(_i=1,[]),cb=arguments[_i++];this._queue.push({task:task,args:args,cb:cb});return this._tryToRun()};Bottleneck.prototype.changeSettings=function(maxNb,minTime){this.maxNb=maxNb!=null?maxNb:this.maxNb;this.minTime=minTime!=null?minTime:this.minTime;return this};Bottleneck.prototype.stopAll=function(){var a,_i,_len,_ref;_ref=this._timeouts;for(_i=0,_len=_ref.length;_i<_len;_i++){a=_ref[_i];clearTimeout(a)}return this._tryToRun=function(){}};return Bottleneck}();module.exports=Bottleneck}).call(this)},{}],2:[function(require,module,exports){(function(global){(function(){module.exports=require("./Bottleneck");if(global.window!=null){global.window.Bottleneck=module.exports}}).call(this)}).call(this,typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./Bottleneck":1}]},{},[2]); \ No newline at end of file diff --git a/lib/Bottleneck.js b/lib/Bottleneck.js index d4e4211..427424d 100644 --- a/lib/Bottleneck.js +++ b/lib/Bottleneck.js @@ -14,19 +14,20 @@ } Bottleneck.prototype._tryToRun = function() { - var done, next, wait; + var done, index, next, wait; if ((this._nbRunning < this.maxNb || this.maxNb <= 0) && this._queue.length > 0) { this._nbRunning++; wait = Math.max(this._nextRequest - Date.now(), 0); this._nextRequest = Date.now() + wait + this.minTime; next = this._queue.shift(); done = false; - return this._timeouts.push(setTimeout((function(_this) { + return index = this._timeouts.push(setTimeout((function(_this) { return function() { return next.task.apply({}, next.args.concat(function() { var _ref; if (!done) { done = true; + _this._timeouts[index - 1] = null; _this._nbRunning--; _this._tryToRun(); return (_ref = next.cb) != null ? _ref.apply({}, Array.prototype.slice.call(arguments, 0)) : void 0; diff --git a/package.json b/package.json index 3ddd7b4..ee450b0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bottleneck", - "version": "1.1.0", + "version": "1.1.1", "description": "Async rate limiter", "main": "lib/index.js", "scripts": { diff --git a/src/Bottleneck.coffee b/src/Bottleneck.coffee index fd7a139..bbf6167 100644 --- a/src/Bottleneck.coffee +++ b/src/Bottleneck.coffee @@ -11,10 +11,11 @@ class Bottleneck @_nextRequest = Date.now() + wait + @minTime next = @_queue.shift() done = false - @_timeouts.push setTimeout () => + index = @_timeouts.push setTimeout () => next.task.apply {}, next.args.concat () => if not done done = true + @_timeouts[index-1] = null @_nbRunning-- @_tryToRun() next.cb?.apply {}, Array::slice.call arguments, 0