diff --git a/src/sidebar.js b/src/sidebar.js index 5d4cdf3..d7c23d6 100644 --- a/src/sidebar.js +++ b/src/sidebar.js @@ -1,4 +1,9 @@ var concurrency = 5 +var retry_delay = 15/* seconds */ + +function sleep(delay) { + return new Promise((resolve) => setTimeout(resolve, delay*1000)); +} function getCurrentTab() { console.debug("Querying active tab."); @@ -65,7 +70,8 @@ class Book{ .catch((error)=>{ if (!retry){ console.warn(`Failed ${url} - ${error.message}`) - return this.downloadResource(url, true) + return sleep(retry_delay) + .then(()=>{return this.downloadResource(url, true)}) } else { console.error(`Giving up on ${url} - ${error.message}`) throw `Failed second try on ${url} - ${error.message}` @@ -76,7 +82,8 @@ class Book{ return res } else if (!retry){ console.warn(`Failed ${url} - ${res.status} - ${res.statusText}`) - return this.downloadResource(url, true) + return sleep(retry_delay) + .then(()=>{return this.downloadResource(url, true)}) } else { console.error(`Giving up on ${url} - ${res.status} - ${res.statusText}`) throw `Failed second try on ${url} - ${res.status} - ${res.statusText}` @@ -174,22 +181,26 @@ class Book{ downloadContent(){ return Promise.map(Object.keys(this.book_files), (url) => { - return this.downloadAndReport(url, this.book_files[url].filename) - .then((res) => { - if (res.ok){ - this.book_files[url].headers = res.headers - this.book_files[url].mime = res.headers.get('Content-Type') - return res.arrayBuffer().then((arrBuffer)=>{ - this.book_files[url].body = arrBuffer - }) - } - }) - .catch((error)=>{ - throw `Failed to download book content: "${error}"` - }) + return this.downloadContentFile(url) },{concurrency: concurrency}) } + downloadContentFile(url){ + return this.downloadAndReport(url, this.book_files[url].filename) + .then((res) => { + if (res.ok){ + this.book_files[url].headers = res.headers + this.book_files[url].mime = res.headers.get('Content-Type') + return res.arrayBuffer().then((arrBuffer)=>{ + this.book_files[url].body = arrBuffer + }) + } + }) + .catch((error)=>{ + throw `Failed to download book content: "${error}"` + }) + } + insertBookFile(url, filename){ this.book_files[url] = { url: url,