diff options
author | dartcafe <github@dartcafe.de> | 2021-02-28 01:16:51 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2021-02-28 01:16:51 +0300 |
commit | 25df7307ee5374dc076b793177334bb4bd3a0287 (patch) | |
tree | 90fb3d167351ba2665829ee8fad09510a617ecd1 /src/js/mixins | |
parent | 9a7b8f7b4e41e1dd40d6e7e01939afeac00c25d8 (diff) |
load poll list via long poll and remove timed polling
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'src/js/mixins')
-rw-r--r-- | src/js/mixins/watchPolls.js | 83 |
1 files changed, 56 insertions, 27 deletions
diff --git a/src/js/mixins/watchPolls.js b/src/js/mixins/watchPolls.js index 9d71e6a7..9d8916d0 100644 --- a/src/js/mixins/watchPolls.js +++ b/src/js/mixins/watchPolls.js @@ -3,22 +3,33 @@ import axios from '@nextcloud/axios' import { generateUrl } from '@nextcloud/router' export const watchPolls = { + data() { + return { + cancelToken: null, + restart: false, + watching: true, + lastUpdated: Math.round(Date.now() / 1000), + } + }, + methods: { + watchPollsRestart() { + this.restart = true + this.cancelToken.cancel() + }, + async watchPolls() { console.debug('polls', 'Watch for updates') - this.cancelToken = axios.CancelToken.source() - let endPoint = 'apps/polls' - - if (this.$route.name === 'publicVote') { - endPoint = endPoint + '/s/' + this.$route.params.token - } else if (this.$route.name === 'vote') { - endPoint = endPoint + '/poll/' + this.$route.params.id - } else { - this.watching = false - } while (this.watching) { + let endPoint = 'apps/polls' + if (this.$route.name === 'publicVote') { + endPoint = endPoint + '/s/' + this.$route.params.token + } else { + endPoint = endPoint + '/poll/' + (this.$route.params.id ?? 0) + } + try { const response = await axios.get(generateUrl(endPoint + '/watch'), { params: { offset: this.lastUpdated }, @@ -30,45 +41,63 @@ export const watchPolls = { response.data.updates.forEach((item) => { this.lastUpdated = (item.updated > this.lastUpdated) ? item.updated : this.lastUpdated + // an updated poll table is reported if (item.table === 'polls') { - dispatches.push('poll/get') + if (this.$route.name !== 'publicVote') { + // load poll list only, when not in public poll + dispatches.push('polls/load') + } + if (item.pollId === parseInt(this.$route.params.id)) { + // if current poll is affected, load current poll configuration + dispatches.push('poll/get') + } } else { + // a table of the current poll was reported, load + // corresponding stores dispatches.push(item.table + '/list') } }) + + // execute all loads within one promise const requests = dispatches.map(dispatches => this.$store.dispatch(dispatches)) await Promise.all(requests) this.watching = true - } catch (error) { + } catch (e) { this.watching = false - if (axios.isCancel(error)) { - console.debug('Watch canceld') - } else if (error.response) { - - if (error.response.status === 304) { - + if (axios.isCancel(e)) { + if (this.restart) { + console.debug('restart watch') this.watching = true + this.restart = false + this.cancelToken = axios.CancelToken.source() + } else { + console.debug('Watch canceled') + this.watching = true + this.restart = false + return + } + } else if (e.response) { - } else if (error.response.status === 503) { - + if (e.response.status === 304) { + this.watching = true + continue + } else if (e.response.status === 503) { console.debug('Server not available, reconnect watch in 30 sec') await new Promise(resolve => setTimeout(resolve, 30000)) this.watching = true - + continue } else { - - console.error('Unhandled error watching polls', error) - + console.error('Unhandled error watching polls', e) + return } - } else if (error.request) { - + } else if (e.request) { console.debug('Watch aborted') this.watching = true - + return } } } |