fix download queue

This commit is contained in:
j 2015-03-21 13:06:16 +05:30
parent 3229c732d4
commit d3ce9198ce

View file

@ -6,6 +6,7 @@ pandora.fs = (function() {
downloads: {}, downloads: {},
enabled: false, enabled: false,
}, },
active,
queue = [], queue = [],
requestedBytes = 100*1024*1024*1024; // 100GB requestedBytes = 100*1024*1024*1024; // 100GB
@ -29,29 +30,8 @@ pandora.fs = (function() {
return pandora.getVideoURLName(id, resolution, part, track).replace('/', '::'); return pandora.getVideoURLName(id, resolution, part, track).replace('/', '::');
} }
that.cacheVideo = function(id, callback) { function cacheVideo(id, callback) {
if (that.getVideoURL(id, pandora.user.ui.videoResolution, 1) || that.downloads[id]) { active = true;
callback({progress: 1});
return;
} else {
that.downloads = that.downloads || {};
that.downloads[id] = {
added: new Date(),
cancel: function() {
},
id: id + '::' + pandora.user.ui.videoResolution,
item: id,
progress: 0,
resolution: pandora.user.ui.videoResolution,
size: 0
};
queue.length
? queue.push([id, callback])
: startDownload(id, callback);
}
function startDownload(id, callback) {
pandora.api.get({id: id, keys: ['parts']}, function(result) { pandora.api.get({id: id, keys: ['parts']}, function(result) {
var parts = result.data.parts, sizes = []; var parts = result.data.parts, sizes = [];
downloadPart(0); downloadPart(0);
@ -70,8 +50,9 @@ pandora.fs = (function() {
if (result.url) { if (result.url) {
if (part + 1 == parts) { if (part + 1 == parts) {
delete that.downloads[id]; delete that.downloads[id];
active = false;
if (queue.length) { if (queue.length) {
startDownload.apply(null, queue.shift()); cacheVideo.apply(null, queue.shift());
} }
} else { } else {
downloadPart(part + 1); downloadPart(part + 1);
@ -82,6 +63,29 @@ pandora.fs = (function() {
} }
}); });
} }
that.cacheVideo = function(id, callback) {
if (that.getVideoURL(id, pandora.user.ui.videoResolution, 1) || that.downloads[id]) {
callback({progress: 1});
return;
} else {
that.downloads = that.downloads || {};
that.downloads[id] = {
added: new Date(),
cancel: function() {
},
id: id + '::' + pandora.user.ui.videoResolution,
item: id,
progress: 0,
resolution: pandora.user.ui.videoResolution,
size: 0
};
(active || queue.length)
? queue.push([id, callback])
: cacheVideo(id, callback);
}
}; };
that.removeVideo = function(id, callback) { that.removeVideo = function(id, callback) {