Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Griffiths committed Dec 19, 2011
0 parents commit 9c5236f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
Empty file added README
Empty file.
76 changes: 76 additions & 0 deletions downloadQueue.js
@@ -0,0 +1,76 @@
function DownloadQueue(options)
{
this.queue = [];

if (options)
{
this.maxConnections = options.maxConnections || 4;
this.maxConnections = options.downloadDirectory || Ti.Filesystem.tempDirectory;
}
}
DownloadQueue.prototype.maxConnections = 4;
DownloadQueue.prototype.downloadDirectory = Ti.Filesystem.tempDirectory;
DownloadQueue.prototype.queue = [];

DownloadQueue.prototype.add = function (fileDescriptor)
{
this.queue.push(fileDescriptor);
};

DownloadQueue.prototype.process = function ()
{
var dq = this;
var dataDirectory = dq.downloadDirectory;

dq.httpConnections = dq.httpConnections || 0;

var httpConnections = this.httpConnections, queue = this.queue, maxConnections = this.maxConnections, item;
while (httpConnections < maxConnections && (item = queue.shift()))
{
dq.httpConnections++;

var filePath = dataDirectory + item.filename;
var conenction = Ti.Network.createHTTPClient({
file: filePath,
ondatastream: (function (fileDescriptor)
{
return function (event)
{
fileDescriptor.filePath = filePath;
fileDescriptor.progress(event);
}
})(item),
onload: (function (fileDescriptor)
{
return function (event)
{
Ti.fireEvent("downloadqueue:complete", fileDescriptor);
dq.httpConnections--;

if (dq.queue.length > 0)
{
dq.process();
}
else if (dq.httpConnections == 0)
{
Ti.fireEvent("downloadqueue:queuecomplete", dq);
}

}
})(item),
onerror: function (event)
{
dq.httpConnections--;
Ti.API.error("Fail to download");
}
});

conenction.open("GET", item.url);
conenction.send();
}
};

exports.createDownloadQueue = function (options)
{
return new DownloadQueue(options);
};

0 comments on commit 9c5236f

Please sign in to comment.