From 510bf50ff2fb29817fc754d6793e506e3414bc6b Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 11 May 2018 15:29:26 +0100 Subject: [PATCH] Remove meta refresh on task page, use JS to poll --- app/static/package_create.js | 49 ---------------------- app/static/polltask.js | 56 +++++++++++++++++++++++++ app/templates/packages/create_edit.html | 1 + app/templates/tasks/view.html | 16 ++++--- app/views/tasks.py | 2 + 5 files changed, 69 insertions(+), 55 deletions(-) create mode 100644 app/static/polltask.js diff --git a/app/static/package_create.js b/app/static/package_create.js index 927273fa..65685505 100644 --- a/app/static/package_create.js +++ b/app/static/package_create.js @@ -6,55 +6,6 @@ $(function() { $(".pkg_meta").show() } - function getJSON(url, method) { - return new Promise(function(resolve, reject) { - fetch(new Request(url, { - method: method || "get", - credentials: "same-origin", - headers: { - "Accept": "application/json", - }, - })).then(function(response) { - response.text().then(function(txt) { - resolve(JSON.parse(txt)) - }).catch(reject) - }).catch(reject) - }) - } - - function performTask(url) { - return new Promise(function(resolve, reject) { - getJSON(url, "post").then(function(startResult) { - console.log(startResult) - if (typeof startResult.poll_url == "string") { - var tries = 0; - function retry() { - tries++; - if (tries > 10) { - reject("timeout") - } else { - console.log("Polling task in " + (tries*100) + "ms") - setTimeout(step, tries*100) - } - } - function step() { - getJSON(startResult.poll_url).then(function(res) { - if (res.status == "SUCCESS") { - console.log("Got result") - resolve(res.result) - } else { - retry() - } - }).catch(retry) - } - retry() - } else { - reject("Start task didn't return string!") - } - }).catch(reject) - }) - } - function repoIsSupported(url) { try { return URI(url).hostname() == "github.com" diff --git a/app/static/polltask.js b/app/static/polltask.js new file mode 100644 index 00000000..bc9d8028 --- /dev/null +++ b/app/static/polltask.js @@ -0,0 +1,56 @@ +function getJSON(url, method) { + return new Promise(function(resolve, reject) { + fetch(new Request(url, { + method: method || "get", + credentials: "same-origin", + headers: { + "Accept": "application/json", + }, + })).then(function(response) { + response.text().then(function(txt) { + resolve(JSON.parse(txt)) + }).catch(reject) + }).catch(reject) + }) +} + +function pollTask(poll_url, disableTimeout) { + return new Promise(function(resolve, reject) { + var tries = 0; + function retry() { + tries++; + if (!disableTimeout && tries > 10) { + reject("timeout") + } else { + const interval = Math.min(tries*100, 1000) + console.log("Polling task in " + interval + "ms") + setTimeout(step, interval) + } + } + function step() { + getJSON(poll_url).then(function(res) { + if (res.status == "SUCCESS") { + console.log("Got result") + resolve(res.result) + } else { + retry() + } + }).catch(retry) + } + retry() + }) +} + + +function performTask(url) { + return new Promise(function(resolve, reject) { + getJSON(url, "post").then(function(startResult) { + console.log(startResult) + if (typeof startResult.poll_url == "string") { + pollTask(startResult.poll_url).then(resolve).catch(reject) + } else { + reject("Start task didn't return string!") + } + }).catch(reject) + }) +} diff --git a/app/templates/packages/create_edit.html b/app/templates/packages/create_edit.html index 3378a5d5..9049e817 100644 --- a/app/templates/packages/create_edit.html +++ b/app/templates/packages/create_edit.html @@ -55,6 +55,7 @@ {% if not package.title %} +