diff options
author | dartcafe <github@dartcafe.de> | 2021-11-02 13:41:37 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2021-11-02 13:41:37 +0300 |
commit | c6664ed887bdf2c38b2d446c5bc29f5dca1a9041 (patch) | |
tree | b34ede630f6524f667794d16cb5826e1a9966da0 /src/js/mixins/watchPolls.js | |
parent | 7b6eb007ba858eaf6cfc157542c5d033f0e0e9f2 (diff) |
fixes, tidy, optimizations
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'src/js/mixins/watchPolls.js')
-rw-r--r-- | src/js/mixins/watchPolls.js | 109 |
1 files changed, 56 insertions, 53 deletions
diff --git a/src/js/mixins/watchPolls.js b/src/js/mixins/watchPolls.js index 8bbb4b15..df357957 100644 --- a/src/js/mixins/watchPolls.js +++ b/src/js/mixins/watchPolls.js @@ -23,8 +23,8 @@ import axios from '@nextcloud/axios' import { generateUrl } from '@nextcloud/router' -import { NotReady } from '../Exceptions/Exceptions' import { getCurrentUser } from '@nextcloud/auth' +import { mapState } from 'vuex' export const watchPolls = { data() { @@ -34,23 +34,21 @@ export const watchPolls = { watching: true, lastUpdated: Math.round(Date.now() / 1000), retryCounter: 0, - retryTimeout: 30000, maxTries: 5, endPoint: '', isLoggedin: !!getCurrentUser(), isAdmin: !!getCurrentUser()?.isAdmin, - sleepTimeout: 30, - sessionCount: 0, - updateType: 'noPolling', + sleepTimeout: 30, // seconds } }, - methods: { - cancelWatch() { - console.debug('[polls]', 'Cancel watch') - this.cancelToken.cancel() - }, + computed: { + ...mapState({ + updateType: (state) => state.appSettings.appSettings.updateType, + }), + }, + methods: { async watchPolls() { if (this.cancelToken) { // there is already a cancelToken, so just cancel the previous session and exit @@ -62,7 +60,7 @@ export const watchPolls = { this.cancelToken = axios.CancelToken.source() while (this.retryCounter < this.maxTries) { - this.updateType = await this.$store.dispatch('appSettings/getUpdateType') + await this.$store.dispatch('appSettings/get') if (this.updateType === 'noPolling') { console.debug('[polls]', 'Polling for updates is disabled') @@ -70,42 +68,74 @@ export const watchPolls = { } try { - await this.getRoute() - - const response = await axios.get(generateUrl(this.endPoint), { - params: { offset: this.lastUpdated }, - cancelToken: this.cancelToken.token, - }) - + const response = await this.fetchUpdates() + console.debug('[polls]', `Update detected (${this.updateType})`, response.data.updates) + this.loadTables(response.data.updates) this.retryCounter = 0 // reset retryCounter after we got a valid response - - console.debug('[polls]', 'update detected', response.data.updates) - await this.loadTables(response.data.updates) - } catch (e) { if (axios.isCancel(e)) { - console.debug('[polls]', 'Watch canceled') + this.handleCanceledRequest() } else if (e.response?.status === 304) { - await this.handleNotModifiedResponse() + this.handleNotModifiedResponse() } else { await this.handleConnectionError(e) - await new Promise((resolve) => setTimeout(resolve, e.name === 'NotReady' ? 2000 : this.retryTimeout)) } } if (this.updateType === 'periodicPolling') { - console.debug('[polls]', 'Sleep for', this.sleepTimeout, 'seconds') + console.debug('[polls]', `Sleep ${this.sleepTimeout} seconds`) await this.sleep(this.sleepTimeout) } } + // invalidate the cancel token before leaving this.cancelToken = null + + if (this.retryCounter) { + console.debug('[polls]', `Cancel watch after ${this.retryCounter} failed requests`) + } + }, + + cancelWatch() { + this.cancelToken.cancel() }, sleep(timeout) { return new Promise((resolve) => setTimeout(resolve, timeout * 1000)) }, + async fetchUpdates() { + if (this.$route.name === 'publicVote') { + this.endPoint = `apps/polls/s/${this.$route.params.token}/watch` + } else { + this.endPoint = `apps/polls/poll/${this.$route.params.id ?? 0}/watch` + } + return await axios.get(generateUrl(this.endPoint), { + params: { offset: this.lastUpdated }, + cancelToken: this.cancelToken.token, + }) + }, + + handleCanceledRequest() { + console.debug('[polls]', 'Fetch canceled') + this.cancelToken = axios.CancelToken.source() + }, + + handleNotModifiedResponse() { + console.debug('[polls]', `No updates (using ${this.updateType})`) + this.retryCounter = 0 // reset retryCounter, after we get a 304 + }, + + async handleConnectionError(e) { + this.retryCounter += 1 + console.debug('[polls]', e.message ?? `No response - request aborted - failed request ${this.retryCounter}/${this.maxTries}`) + + if (e.response) { + console.error('[polls]', 'Unhandled error watching polls', e) + } + await this.sleep(this.sleepTimeout) + }, + async loadTables(tables) { let dispatches = [] @@ -139,32 +169,5 @@ export const watchPolls = { await Promise.all(dispatches.map((dispatches) => this.$store.dispatch(dispatches))) }, - getRoute() { - if (this.$route.name === null) { - throw new NotReady('Router not initialized') - } - - if (this.$route.name === 'publicVote') { - this.endPoint = `apps/polls/s/${this.$route.params.token}/watch` - } else { - this.endPoint = `apps/polls/poll/${this.$route.params.id ?? 0}/watch` - } - }, - - handleNotModifiedResponse() { - console.debug('[polls]', 'Not modified') - this.retryCounter = 0 // reset retryCounter, after we get a 304 - }, - - handleConnectionError(e) { - this.retryCounter += 1 - console.debug('[polls]', e.message ?? 'No response - request aborted - failed request', '-', `${this.retryCounter}/${this.maxTries}`) - - if (e.response) { - console.error('[polls]', 'Unhandled error watching polls', e) - } - - }, - }, } |