better session handling

- separated from component
- handle visibilityState / closing of the tab

Signed-off-by: chandi Langecker <git@chandi.it>
This commit is contained in:
chandi Langecker
2022-09-05 11:53:13 +02:00
parent 6bfb54e2b3
commit 38aed97d69
7 changed files with 143 additions and 98 deletions

View File

@@ -227,7 +227,7 @@ import FilterOffIcon from 'vue-material-design-icons/FilterOff.vue'
import ArrowCollapseVerticalIcon from 'vue-material-design-icons/ArrowCollapseVertical.vue'
import ArrowExpandVerticalIcon from 'vue-material-design-icons/ArrowExpandVertical.vue'
import SessionList from './SessionList'
import { isNotifyPushEnabled } from '../listeners'
import { isNotifyPushEnabled } from '../sessions'
export default {
name: 'Controls',

View File

@@ -81,8 +81,7 @@ import Stack from './Stack.vue'
import { NcEmptyContent } from '@nextcloud/vue'
import GlobalSearchResults from '../search/GlobalSearchResults.vue'
import { showError } from '../../helpers/errors.js'
import { sessionApi } from '../../services/SessionApi'
import { isNotifyPushEnabled } from '../../listeners'
import { createSession } from '../../sessions.js'
export default {
name: 'Board',
@@ -131,13 +130,11 @@ export default {
},
watch: {
id(newValue, oldValue) {
if (oldValue) {
if (this.session) {
// close old session
sessionApi.closeSession(oldValue, this.token)
this.token = null
this.session.close()
}
// create new session
this.ensureSession(newValue)
this.session = createSession(newValue)
this.fetchData()
},
@@ -146,35 +143,11 @@ export default {
},
},
created() {
if (isNotifyPushEnabled()) {
// create a session
this.ensureSession()
}
this.session = createSession(this.id)
this.fetchData()
if (isNotifyPushEnabled()) {
// regularly let the server know that we are still here
this.sessionInterval = setInterval(() => {
this.ensureSession()
}, 25 * 1000)
// we don't get events pushed for sessions that have expired,
// so we poll the list of sessions every minute when there
// are other sessions active
this.refreshInterval = setInterval(() => {
if (this.board?.activeSessions?.length) {
this.refreshData()
}
}, 60 * 1000)
}
},
beforeDestroy() {
if (isNotifyPushEnabled()) {
sessionApi.closeSession(this.id, this.token)
clearInterval(this.sessionInterval)
clearInterval(this.refreshInterval)
}
this.session.close()
},
methods: {
async fetchData() {
@@ -189,28 +162,6 @@ export default {
this.loading = false
},
async ensureSession(boardId = this.id) {
if (this.token) {
try {
await sessionApi.syncSession(boardId, this.token)
} catch (err) {
// session probably expired, let's try again
// with a fresh session
this.token = null
setTimeout(() => {
this.ensureSession()
}, 100)
}
} else {
try {
const res = await sessionApi.createSession(boardId)
this.token = res.token
} catch (err) {
showError(err)
}
}
},
async refreshData() {
await this.$store.dispatch('refreshBoard', this.id)
},