aboutsummaryrefslogtreecommitdiff
path: root/app/static/polltask.js
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2018-05-23 21:12:52 +0100
committerrubenwardy <rw@rubenwardy.com>2018-05-23 21:15:21 +0100
commitd5342d7096e8bb568f1af7bdc9200b03537bb9fc (patch)
treeb59e4d6a9b0b8c289cec58b597c9fccbba15e8cd /app/static/polltask.js
parent59f75bb71c361ae442febf3666838ef055849c30 (diff)
downloadcheatdb-d5342d7096e8bb568f1af7bdc9200b03537bb9fc.tar.xz
Move static and uploads to public dir
Diffstat (limited to 'app/static/polltask.js')
-rw-r--r--app/static/polltask.js61
1 files changed, 0 insertions, 61 deletions
diff --git a/app/static/polltask.js b/app/static/polltask.js
deleted file mode 100644
index f6b528d..0000000
--- a/app/static/polltask.js
+++ /dev/null
@@ -1,61 +0,0 @@
-// @author rubenwardy
-// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3-or-Later
-
-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 if (res.status == "FAILURE" || res.status == "REVOKED") {
- reject(res.error || "Unknown server error")
- } 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)
- })
-}