Skip to content

Commit

Permalink
Add in exponential backoff for downloadResource()
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleady committed May 29, 2020
1 parent 82efbc4 commit 0a42034
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "R20Exporter",
"version": "0.7",
"version": "0.7.1",
"manifest_version": 2,
"description": "Export a Roll20 Campaign with all its assets to a ZIP file.",
"permissions": [
Expand Down Expand Up @@ -30,4 +30,4 @@
"run_at": "document_idle"
}
]
}
}
24 changes: 16 additions & 8 deletions src/R20Exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ class R20Exporter {
return result
}


jsonToBlob(obj) {
return new Blob([JSON.stringify(obj, undefined, 4)], { "type": 'text/json' })
}
Expand Down Expand Up @@ -588,8 +588,8 @@ class R20Exporter {
img.src = url
}

downloadResource(url, cb, errorCB = null) {
const id = this.newPendingOperation()
downloadResource(url, cb, errorCB = null, retryId = undefined, expBackoff = 1) {
const id = retryId || this.newPendingOperation()

// Security in Chrome prevents getting http urls entirely.
if (window.location.protocol === "https:" && url.startsWith("http:"))
Expand All @@ -598,6 +598,8 @@ class R20Exporter {
fetch(url).then((response) => {
if (response.status == 200 || response.status == 0) {
return Promise.resolve(response.blob())
} else if (response.status == 404 || response.status == 403) {
return Promise.reject(new Error("DO_NOT_RETRY"))
} else {
return Promise.reject(new Error(response.statusText))
}
Expand All @@ -608,9 +610,15 @@ class R20Exporter {
cb(blob)
}
).catch((error) => {
this.completedOperation(id)
if (errorCB)
errorCB()
if (expBackoff < 30 && error.message != "DO_NOT_RETRY") {
setTimeout(() => {
this.downloadResource(url, cb, errorCB, id, expBackoff * (1.5 + Math.random()))
}, expBackoff * 1000)
} else {
this.completedOperation(id)
if (errorCB)
errorCB()
}
}
)
}
Expand Down Expand Up @@ -1034,7 +1042,7 @@ class ModalWindow {
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}
/* Modal Content */
.modal-content {
background-color: #fefefe;
Expand Down Expand Up @@ -1231,4 +1239,4 @@ function addExportButton() {
if (window.$ !== undefined)
addExportButton()
else
window.addEventListener("DOMContentLoaded", addExportButton)
window.addEventListener("DOMContentLoaded", addExportButton)

0 comments on commit 0a42034

Please sign in to comment.