Signed-off-by: dartcafe <github@dartcafe.de>
This commit is contained in:
dartcafe 2021-02-27 20:30:47 +01:00
Родитель f27e13152e
Коммит 9a7b8f7b4e
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: CCE73CEF3035D3C8
2 изменённых файлов: 81 добавлений и 73 удалений

Просмотреть файл

@ -0,0 +1,77 @@
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
export const watchPolls = {
methods: {
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) {
try {
const response = await axios.get(generateUrl(endPoint + '/watch'), {
params: { offset: this.lastUpdated },
cancelToken: this.cancelToken.token,
})
const dispatches = []
console.debug('polls', 'update detected', response.data.updates)
response.data.updates.forEach((item) => {
this.lastUpdated = (item.updated > this.lastUpdated) ? item.updated : this.lastUpdated
if (item.table === 'polls') {
dispatches.push('poll/get')
} else {
dispatches.push(item.table + '/list')
}
})
const requests = dispatches.map(dispatches => this.$store.dispatch(dispatches))
await Promise.all(requests)
this.watching = true
} catch (error) {
this.watching = false
if (axios.isCancel(error)) {
console.debug('Watch canceld')
} else if (error.response) {
if (error.response.status === 304) {
this.watching = true
} else if (error.response.status === 503) {
console.debug('Server not available, reconnect watch in 30 sec')
await new Promise(resolve => setTimeout(resolve, 30000))
this.watching = true
} else {
console.error('Unhandled error watching polls', error)
}
} else if (error.request) {
console.debug('Watch aborted')
this.watching = true
}
}
}
},
},
}

Просмотреть файл

@ -95,8 +95,6 @@
</template>
<script>
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import { showError, showSuccess } from '@nextcloud/dialogs'
import linkifyUrls from 'linkify-urls'
import { mapState, mapGetters } from 'vuex'
@ -113,6 +111,7 @@ import PublicRegisterModal from '../components/Base/PublicRegisterModal'
import PublicEmail from '../components/Base/PublicEmail'
import Subscription from '../components/Subscription/Subscription'
import VoteTable from '../components/VoteTable/VoteTable'
import { watchPolls } from '../mixins/watchPolls'
export default {
name: 'Vote',
@ -132,6 +131,8 @@ export default {
VoteTable,
},
mixins: [watchPolls],
data() {
return {
cancelToken: null,
@ -302,7 +303,7 @@ export default {
} else {
emit('toggle-sidebar', { open: (window.innerWidth > 920) })
}
this.watchPoll()
this.watchPolls()
},
beforeDestroy() {
@ -353,76 +354,6 @@ export default {
showError(t('polls', 'Error saving email address {emailAddress}', { emailAddress: emailAddress }))
}
},
async watchPoll() {
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) {
try {
const response = await axios.get(generateUrl(endPoint + '/watch'), {
params: { offset: this.lastUpdated },
cancelToken: this.cancelToken.token,
})
const dispatches = []
console.debug('polls', 'update detected', response.data.updates)
response.data.updates.forEach((item) => {
this.lastUpdated = (item.updated > this.lastUpdated) ? item.updated : this.lastUpdated
if (item.table === 'polls') {
dispatches.push('poll/get')
} else {
dispatches.push(item.table + '/list')
}
})
const requests = dispatches.map(dispatches => this.$store.dispatch(dispatches))
await Promise.all(requests)
this.watching = true
} catch (error) {
this.watching = false
if (axios.isCancel(error)) {
console.debug('Watch canceld')
} else if (error.response) {
if (error.response.status === 304) {
this.watching = true
} else if (error.response.status === 503) {
console.debug('Server not available, reconnect watch in 30 sec')
await new Promise(resolve => setTimeout(resolve, 30000))
this.watching = true
} else {
console.error('Unhandled error watching polls', error)
}
} else if (error.request) {
console.debug('Watch aborted')
this.watching = true
}
}
}
},
},
}