refactoring: split engine.js into files

This commit is contained in:
Settel 2021-08-13 00:30:17 +02:00
parent da0760850a
commit a86a0e62a5
5 changed files with 84 additions and 78 deletions

View File

@ -0,0 +1,43 @@
import buildUrl from 'build-url'
export default async function() {
const { store, $axios, $config } = this.context
if (this.shouldStop || !this.isActive) {
this.isActive = false
this.shouldStop = false
return
}
let delay = 0
try {
const url = buildUrl($config.serverBaseUrl, {
path: '/api/sync',
queryParams: {
v: store.state.engine.version + 1,
g: store.state.engine.user?.game,
},
})
const response = await $axios.get(url)
store.commit('engine/setJson', response.data)
} catch (e) {
const { status, statusText } = e.response
if (status != 200) {
console.warn(`HTTP ${status} ${statusText}`)
delay = 5000
store.commit('engine/setVersion', -1)
}
}
const now = new Date().getTime()
const last = this.lastFetched.splice(0, 1)
this.lastFetched.push(now)
if (now - last < 1000) {
console.warn('engine: respawning too fast, throttling down')
delay = 5000
}
window.setTimeout(() => {
this.fetchUpdate()
}, delay)
}

View File

@ -0,0 +1,13 @@
export default async function() {
const { store, $axios } = this.context
try {
const response = await $axios.get('/api/userinfo')
store.commit('engine/setUser', response.data)
} catch(e) {
store.commit('engine/setUser', undefined)
return false
}
return true
}

View File

@ -1,4 +1,7 @@
import buildUrl from 'build-url'
import start from './start'
import stop from './stop'
import fetchUpdate from './fetchUpdate'
import fetchUserInfo from './fetchUserInfo'
export default (context, inject) => {
const engine = {
@ -7,82 +10,10 @@ export default (context, inject) => {
isActive: false,
shouldStop: false,
async start() {
const { store, redirect } = this.context
if (!store.state.engine.user) {
if (!await this.fetchUserInfo()) {
redirect('/')
}
}
if (this.isActive) {
console.warn('attempt to start already running engine!')
return
}
this.isActive = true
this.fetchUpdate()
},
stop() {
if (!this.isActive) return
this.shouldStop = true
},
async fetchUpdate() {
const { store, $axios, $config } = this.context
if (this.shouldStop || !this.isActive) {
this.isActive = false
this.shouldStop = false
return
}
let delay = 0
try {
const url = buildUrl($config.serverBaseUrl, {
path: '/api/sync',
queryParams: {
v: store.state.engine.version + 1,
g: store.state.engine.user?.game,
},
})
const response = await $axios.get(url)
store.commit('engine/setJson', response.data)
} catch (e) {
const { status, statusText } = e.response
if (status != 200) {
console.warn(`HTTP ${status} ${statusText}`)
delay = 5000
store.commit('engine/setVersion', -1)
}
}
const now = new Date().getTime()
const last = this.lastFetched.splice(0, 1)
this.lastFetched.push(now)
if (now - last < 1000) {
console.warn('engine: respawning too fast, throttling down')
delay = 5000
}
window.setTimeout(() => {
this.fetchUpdate()
}, delay)
},
async fetchUserInfo() {
const { store, $axios } = this.context
try {
const response = await $axios.get('/api/userinfo')
store.commit('engine/setUser', response.data)
} catch(e) {
store.commit('engine/setUser', undefined)
return false
}
return true
},
start,
stop,
fetchUpdate,
fetchUserInfo,
}
inject('engine', engine)

View File

@ -0,0 +1,15 @@
export default async function() {
const { store, redirect } = this.context
if (!store.state.engine.user) {
if (!await this.fetchUserInfo()) {
redirect('/')
}
}
if (this.isActive) {
console.warn('attempt to start already running engine!')
return
}
this.isActive = true
this.fetchUpdate()
}

View File

@ -0,0 +1,4 @@
export default function() {
if (!this.isActive) return
this.shouldStop = true
}