aboutsummaryrefslogtreecommitdiff
path: root/app/public/static/polltask.js
blob: 8bdd9be51fb9a1d53f742e266933d6cf83bdff7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// @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 > 30) {
				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)
	})
}