start client engine only once

This commit is contained in:
Settel 2021-08-09 11:16:45 +02:00
parent 34132c7703
commit f7084da78e

View File

@ -5,15 +5,36 @@ export default (context, inject) => {
const engine = { const engine = {
lastFetched: [0, 0, 0, 0, 0], lastFetched: [0, 0, 0, 0, 0],
isActive: false,
shouldStop: false,
async start() { async start() {
if (!store.state.engine.user) { if (!store.state.engine.user) {
if (!await this.fetchUserInfo()) { if (!await this.fetchUserInfo()) {
window.location.href = "/" window.location.href = "/"
} }
} }
if (this.isActive) {
console.warn('attempt to start already running engine!')
return
}
this.isActive = true
this.fetchUpdate() this.fetchUpdate()
}, },
stop() {
if (!this.isActive) return
this.shouldStop = true
},
async fetchUpdate() { async fetchUpdate() {
if (this.shouldStop || !this.isActive) {
this.isActive = false
this.shouldStop = false
return
}
let delay = 0 let delay = 0
try { try {
const url = buildUrl($config.serverBaseUrl, { const url = buildUrl($config.serverBaseUrl, {