diff options
author | dartcafe <github@dartcafe.de> | 2021-03-06 11:37:01 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2021-03-06 11:37:01 +0300 |
commit | c55bb68f8a770dcc7af810b91bd59b1f1d3de746 (patch) | |
tree | 1d26b9ad5dff330148989394f71c435f510b6bed /src/js/mixins/watchPolls.js | |
parent | 5a5dd80dea464c1bf8ead9130cdfbcc58ba675d9 (diff) |
rearranging components
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'src/js/mixins/watchPolls.js')
-rw-r--r-- | src/js/mixins/watchPolls.js | 103 |
1 files changed, 50 insertions, 53 deletions
diff --git a/src/js/mixins/watchPolls.js b/src/js/mixins/watchPolls.js index b34d683c..c4c764fb 100644 --- a/src/js/mixins/watchPolls.js +++ b/src/js/mixins/watchPolls.js @@ -9,6 +9,9 @@ export const watchPolls = { restart: false, watching: true, lastUpdated: Math.round(Date.now() / 1000), + retryCounter: 0, + retryTimeout: 30000, + maxTries: 5, } }, @@ -18,11 +21,45 @@ export const watchPolls = { this.cancelToken.cancel() }, + async loadTables(tables) { + let dispatches = [] + tables.forEach((item) => { + this.lastUpdated = (item.updated > this.lastUpdated) ? item.updated : this.lastUpdated + // an updated poll table is reported + if (item.table === 'polls') { + 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 ?? this.$store.state.share.pollId)) { + // if current poll is affected, load current poll configuration + dispatches.push('poll/get') + // load also options and votes + dispatches.push('votes/list') + dispatches.push('options/list') + } + } else if (['votes', 'options'].includes(item.table)) { + dispatches.push('votes/list') + dispatches.push('options/list') + } else { + // a table of the current poll was reported, load + // corresponding stores + dispatches.push(item.table + '/list') + } + }) + // remove duplicates + dispatches = [...new Set(dispatches)] + // execute all loads within one promise + const requests = dispatches.map(dispatches => this.$store.dispatch(dispatches)) + await Promise.all(requests) + }, + async watchPolls() { console.debug('polls', 'Watch for updates') this.cancelToken = axios.CancelToken.source() + this.retryCounter = 0 - while (this.watching) { + while (this.retryCounter < this.maxTries) { let endPoint = 'apps/polls' if (this.$route.name === 'publicVote') { endPoint = endPoint + '/s/' + this.$route.params.token @@ -35,77 +72,37 @@ export const watchPolls = { params: { offset: this.lastUpdated }, cancelToken: this.cancelToken.token, }) - let dispatches = [] - console.debug('polls', 'update detected', response.data.updates) - - 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') { - 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 ?? this.$store.state.share.pollId)) { - // if current poll is affected, load current poll configuration - dispatches.push('poll/get') - // load also options and votes - dispatches.push('votes/list') - dispatches.push('options/list') - } - } else if (['votes', 'options'].includes(item.table)) { - dispatches.push('votes/list') - dispatches.push('options/list') - } else { - // a table of the current poll was reported, load - // corresponding stores - dispatches.push(item.table + '/list') - } - }) - - // remove duplicates - dispatches = [...new Set(dispatches)] - // execute all loads within one promise - const requests = dispatches.map(dispatches => this.$store.dispatch(dispatches)) - await Promise.all(requests) - - this.watching = true + this.retryCounter = 0 + await this.loadTables(response.data.updates) } catch (e) { - this.watching = false if (axios.isCancel(e)) { if (this.restart) { console.debug('restart watch') - this.watching = true + this.retryCounter = 0 this.restart = false this.cancelToken = axios.CancelToken.source() } else { console.debug('Watch canceled') - this.watching = true - this.restart = false return } } else if (e.response) { - 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 + // timeout of poll --> restart + this.retryCounter = 0 } else { + this.retryCounter++ console.error('Unhandled error watching polls', e) - return + console.debug('error request', this.retryCounter) + await new Promise(resolve => setTimeout(resolve, this.retryTimeout)) } } else if (e.request) { - console.debug('Watch aborted') - this.watching = true - return + this.retryCounter++ + console.debug('No response - request aborted') + console.debug('failed request', this.retryCounter) + await new Promise(resolve => setTimeout(resolve, this.retryTimeout)) } } } |