diff options
author | dartcafe <github@dartcafe.de> | 2021-10-30 12:38:34 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2021-10-30 12:38:34 +0300 |
commit | 9499f583eaebb0dff6539fc4083563552754c45d (patch) | |
tree | 2e2511930bcc68d3c23257405b5838f64c66989b /src/js/mixins/watchPolls.js | |
parent | 725fb75b244fde15c20c6a12ee63128ca9eba515 (diff) |
Configuration for disabling polling or usage of periodic polling
Signed-off-by: dartcafe <github@dartcafe.de>
Diffstat (limited to 'src/js/mixins/watchPolls.js')
-rw-r--r-- | src/js/mixins/watchPolls.js | 81 |
1 files changed, 74 insertions, 7 deletions
diff --git a/src/js/mixins/watchPolls.js b/src/js/mixins/watchPolls.js index a03bcad8..42ace663 100644 --- a/src/js/mixins/watchPolls.js +++ b/src/js/mixins/watchPolls.js @@ -39,18 +39,41 @@ export const watchPolls = { endPoint: '', isLoggedin: !!getCurrentUser(), isAdmin: !!getCurrentUser()?.isAdmin, + sleepTimeout: 30, } }, methods: { - watchPollsRestart() { - this.restart = true - this.cancelToken.cancel() + async watchPollsRestart() { + const updateType = await this.$store.dispatch('appSettings/getUpdateType') + + if (updateType === 'longPolling') { + this.restart = true + this.cancelToken.cancel() + } else { + this.restart = false + console.debug('[polls]', 'Long-polls are deactivated') + } + }, async watchPolls() { - console.debug('[polls]', 'Watch for updates') - await this.initWatch() + const updateType = await this.$store.dispatch('appSettings/getUpdateType') + + if (updateType === 'longPolling') { + console.debug('[polls]', 'Using instant updates via long polling') + this.watchPollsLongPolling() + } else if (updateType === 'periodicPolling') { + this.watchPollsPeriodicPolling() + console.debug('[polls]', 'Using periodic polling') + } else { + console.debug('[polls]', 'Polling deactivated') + } + }, + + async watchPollsLongPolling() { + console.debug('[polls]', 'Watch for updates (long polling') + await this.initLongPolling() while (this.retryCounter < this.maxTries) { try { @@ -84,6 +107,45 @@ export const watchPolls = { } }, + async watchPollsPeriodicPolling() { + console.debug('[polls]', 'Watch for updates (periodic polling)') + await this.getRoute() + + while (this.retryCounter < this.maxTries) { + try { + if (this.$route.name === null) { + throw new NotReady('Router not initialized') + } + const response = await axios.get(generateUrl(this.endPoint), { + params: { offset: this.lastUpdated }, + }) + + if (typeof response.data?.updates !== 'object') { + console.debug('[polls]', 'return value is no array') + throw new NotReady('Invalid content') + } + + this.retryCounter = 0 + console.debug('[polls]', 'update detected', response.data.updates) + await this.loadTables(response.data.updates) + + } catch (e) { + if (e.response?.status === 304) { + await this.handleNotModifiedResponse() + } else { + await this.handleConnectionError(e) + await new Promise((resolve) => setTimeout(resolve, e.name === 'NotReady' ? 2000 : this.retryTimeout)) + } + } + console.debug('[polls]', 'Sleep for', this.sleepTimeout, 'seconds') + await this.sleep() + } + }, + + sleep() { + return new Promise((resolve) => setTimeout(resolve, this.sleepTimeout * 1000)) + }, + async loadTables(tables) { let dispatches = [] @@ -117,10 +179,15 @@ export const watchPolls = { await Promise.all(dispatches.map((dispatches) => this.$store.dispatch(dispatches))) }, - initWatch() { + initLongPolling() { this.cancelToken = axios.CancelToken.source() this.retryCounter = 0 this.restart = false + this.getRoute() + }, + + getRoute() { + this.retryCounter = 0 if (this.$route.name === 'publicVote') { this.endPoint = `apps/polls/s/${this.$route.params.token}/watch` } else { @@ -132,7 +199,7 @@ export const watchPolls = { if (this.restart) { // Restarting of poll was initiated console.debug('[polls]', 'watch canceled - restart watch') - this.initWatch() + this.initLongPolling() } else { // we will exit here console.debug('[polls]', 'watch canceled') |