From a86a0e62a55f4855dfc483f64f8c62b34c7ab07b Mon Sep 17 00:00:00 2001 From: Settel Date: Fri, 13 Aug 2021 00:30:17 +0200 Subject: [PATCH] refactoring: split engine.js into files --- client/src/plugins/engine/fetchUpdate.js | 43 +++++++++++ client/src/plugins/engine/fetchUserInfo.js | 13 ++++ client/src/plugins/engine/index.js | 87 +++------------------- client/src/plugins/engine/start.js | 15 ++++ client/src/plugins/engine/stop.js | 4 + 5 files changed, 84 insertions(+), 78 deletions(-) create mode 100644 client/src/plugins/engine/fetchUpdate.js create mode 100644 client/src/plugins/engine/fetchUserInfo.js create mode 100644 client/src/plugins/engine/start.js create mode 100644 client/src/plugins/engine/stop.js diff --git a/client/src/plugins/engine/fetchUpdate.js b/client/src/plugins/engine/fetchUpdate.js new file mode 100644 index 0000000..7a14aad --- /dev/null +++ b/client/src/plugins/engine/fetchUpdate.js @@ -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) +} diff --git a/client/src/plugins/engine/fetchUserInfo.js b/client/src/plugins/engine/fetchUserInfo.js new file mode 100644 index 0000000..c086c4e --- /dev/null +++ b/client/src/plugins/engine/fetchUserInfo.js @@ -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 +} diff --git a/client/src/plugins/engine/index.js b/client/src/plugins/engine/index.js index 38e4e39..c404f94 100644 --- a/client/src/plugins/engine/index.js +++ b/client/src/plugins/engine/index.js @@ -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,83 +10,11 @@ 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) } diff --git a/client/src/plugins/engine/start.js b/client/src/plugins/engine/start.js new file mode 100644 index 0000000..7900a94 --- /dev/null +++ b/client/src/plugins/engine/start.js @@ -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() +} diff --git a/client/src/plugins/engine/stop.js b/client/src/plugins/engine/stop.js new file mode 100644 index 0000000..6f2d16d --- /dev/null +++ b/client/src/plugins/engine/stop.js @@ -0,0 +1,4 @@ +export default function() { + if (!this.isActive) return + this.shouldStop = true +}