From 71b77f635956c2fcfb4b41ef32a5dd6ab18e2276 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Sat, 21 Mar 2015 12:49:53 +0530 Subject: [PATCH] queue multiple cache requests --- static/js/fs.js | 59 +++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/static/js/fs.js b/static/js/fs.js index 032cde1ee..1c2c7d346 100644 --- a/static/js/fs.js +++ b/static/js/fs.js @@ -6,6 +6,7 @@ pandora.fs = (function() { downloads: {}, enabled: false, }, + queue = [], requestedBytes = 100*1024*1024*1024; // 100GB if(window.webkitRequestFileSystem) { @@ -40,32 +41,42 @@ pandora.fs = (function() { resolution: pandora.user.ui.videoResolution, size: 0 }; - pandora.api.get({id: id, keys: ['parts']}, function(result) { - var parts = result.data.parts, sizes = []; - downloadPart(0); - function downloadPart(part) { - that.downloadVideoURL(id, pandora.user.ui.videoResolution, part + 1, false, function(result) { - result.progress = 1/parts * (part + result.progress); - that.downloads[id].progress = result.progress; - if (result.cancel) { - that.downloads[id].cancel = result.cancel; - } - if (result.total) { - sizes[part] = result.total; - that.downloads[id].size = Ox.sum(sizes); - } - if (result.url) { - if (part + 1 == parts) { - delete that.downloads[id]; - } else { - downloadPart(part + 1); + queue.length + ? queue.push([id, callback]) + : startDownload(id, callback); + + function startDownload(id, callback) { + pandora.api.get({id: id, keys: ['parts']}, function(result) { + var parts = result.data.parts, sizes = []; + downloadPart(0); + + function downloadPart(part) { + that.downloadVideoURL(id, pandora.user.ui.videoResolution, part + 1, false, function(result) { + result.progress = 1/parts * (part + result.progress); + that.downloads[id].progress = result.progress; + if (result.cancel) { + that.downloads[id].cancel = result.cancel; } - } - callback && callback(result); - }); - } - }); + if (result.total) { + sizes[part] = result.total; + that.downloads[id].size = Ox.sum(sizes); + } + if (result.url) { + if (part + 1 == parts) { + delete that.downloads[id]; + if (queue.length) { + startDownload.apply(null, queue.shift()); + } + } else { + downloadPart(part + 1); + } + } + callback && callback(result); + }); + } + }); + } }; that.removeVideo = function(id, callback) {