From a8767f33f43d23c78ed7c93482278f90847ce00a Mon Sep 17 00:00:00 2001 From: Richard Wall Date: Fri, 23 Jul 2010 09:11:13 +0100 Subject: get rid of some closures --- index.html | 7 ++----- jarmon.js | 15 ++++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/index.html b/index.html index ed6ec70..3db6594 100644 --- a/index.html +++ b/index.html @@ -44,12 +44,9 @@ * Setup chart date range controls and all charts **/ - var p = new jarmon.Parallimiter(1); + var p = new jarmon.Parallimiter(2); function serialDownloader(url) { - return p.addCallable( - function() { - return jarmon.downloadBinary(url); - }); + return p.addCallable(jarmon.downloadBinary, [url]); } // Extract the chart template from the page diff --git a/jarmon.js b/jarmon.js index 077fc70..8f7edb7 100644 --- a/jarmon.js +++ b/jarmon.js @@ -29,6 +29,7 @@ jarmon.downloadBinary = function(url) { var d = new MochiKit.Async.Deferred(); $.ajax({ + _deferredResult: d, url: url, dataType: 'text', cache: false, @@ -41,9 +42,9 @@ jarmon.downloadBinary = function(url) { }, success: function(data) { try { - d.callback(new BinaryFile(data)); + this._deferredResult.callback(new BinaryFile(data)); } catch(e) { - d.errback(e); + this._deferredResult.errback(e); } }, error: function(xhr, textStatus, errorThrown) { @@ -54,7 +55,7 @@ jarmon.downloadBinary = function(url) { return this.success(xhr.responseBody); } } - d.errback(new Error(xhr.status)); + this._deferredResult.errback(new Error(xhr.status)); } }); return d; @@ -71,7 +72,7 @@ jarmon.Parallimiter = function(limit) { this._currentCallCount = 0; }; -jarmon.Parallimiter.prototype.addCallable = function(callable) { +jarmon.Parallimiter.prototype.addCallable = function(callable, args) { /** * Add a function to be called when the number of in progress calls drops * below the configured limit @@ -79,7 +80,7 @@ jarmon.Parallimiter.prototype.addCallable = function(callable) { * @param callable: A function which returns a Deferred. **/ var d = new MochiKit.Async.Deferred(); - this._callQueue.unshift([callable, d]); + this._callQueue.unshift([d, callable, args]); this._nextCall(); return d; }; @@ -89,12 +90,12 @@ jarmon.Parallimiter.prototype._nextCall = function() { if(this._currentCallCount < this.limit) { this._currentCallCount++; var nextCall = this._callQueue.pop(); - nextCall[0].call().addBoth( + nextCall[1].apply(null, nextCall[2]).addBoth( function(self, d, res) { d.callback(res); self._currentCallCount--; self._nextCall(); - }, this, nextCall[1]); + }, this, nextCall[0]); } } }; -- cgit v1.2.3