Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rob Griffiths
committed
Dec 19, 2011
0 parents
commit 9c5236f
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |