From 66fe07b17f13313382b194fb19d6081c6627fb3e Mon Sep 17 00:00:00 2001 From: James Lyne Date: Thu, 23 Jun 2022 13:26:52 +0100 Subject: [PATCH] Make INIT an action, split up state setting into multiple mutations --- src/main.ts | 59 ++++++++++++++------------ src/providers/DynmapMapProvider.ts | 2 +- src/providers/OverviewerMapProvider.ts | 2 +- src/providers/Pl3xmapMapProvider.ts | 2 +- src/store/action-types.ts | 1 + src/store/actions.ts | 12 +++++- src/store/mutation-types.ts | 5 ++- src/store/mutations.ts | 58 ++++++++++++------------- 8 files changed, 78 insertions(+), 63 deletions(-) diff --git a/src/main.ts b/src/main.ts index 4e743cd..b9267e0 100644 --- a/src/main.ts +++ b/src/main.ts @@ -31,6 +31,7 @@ import Pl3xmapMapProvider from "@/providers/Pl3xmapMapProvider"; import {showSplashError} from "@/util/splash"; import ConfigurationError from "@/errors/ConfigurationError"; import OverviewerMapProvider from "@/providers/OverviewerMapProvider"; +import {ActionTypes} from "@/store/action-types"; const splash = document.getElementById('splash'), svgs = import.meta.globEager('/assets/icons/*.svg'); @@ -61,37 +62,39 @@ registerMapProvider('overviewer', OverviewerMapProvider); const config = window.liveAtlasConfig; window.liveAtlasLoaded = true; -try { - config.servers = loadConfig(config); - store.commit(MutationTypes.INIT, config); +(async() => { + try { + config.servers = loadConfig(config); + await store.dispatch(ActionTypes.INIT, config); - if(store.state.servers.size > 1) { - const lastSegment = window.location.pathname.split('/').pop(), - serverName = lastSegment && store.state.servers.has(lastSegment) ? lastSegment : store.state.servers.keys().next().value; + if (store.state.servers.size > 1) { + const lastSegment = window.location.pathname.split('/').pop(), + serverName = lastSegment && store.state.servers.has(lastSegment) ? lastSegment : store.state.servers.keys().next().value; - //Update url if server doesn't exist - if(serverName !== lastSegment) { - window.history.replaceState({}, '', serverName + window.location.hash); + //Update url if server doesn't exist + if (serverName !== lastSegment) { + window.history.replaceState({}, '', serverName + window.location.hash); + } + + store.commit(MutationTypes.SET_CURRENT_SERVER, serverName); + } else { + store.commit(MutationTypes.SET_CURRENT_SERVER, store.state.servers.keys().next().value); } - store.commit(MutationTypes.SET_CURRENT_SERVER, serverName); - } else { - store.commit(MutationTypes.SET_CURRENT_SERVER, store.state.servers.keys().next().value); - } + const app = createApp(App) + .use(store) + .use(Notifications) + .use(VueClipboard); - const app = createApp(App) - .use(store) - .use(Notifications) - .use(VueClipboard); - - // app.config.performance = true; - app.mount('#app'); -} catch (e) { - if(e instanceof ConfigurationError) { - console.error('LiveAtlas configuration is invalid:', e); - showSplashError('LiveAtlas configuration is invalid:\n' + e, true); - } else { - console.error('LiveAtlas failed to load:', e); - showSplashError('LiveAtlas failed to load:\n' + e, true); + // app.config.performance = true; + app.mount('#app'); + } catch (e) { + if (e instanceof ConfigurationError) { + console.error('LiveAtlas configuration is invalid:', e); + showSplashError('LiveAtlas configuration is invalid:\n' + e, true); + } else { + console.error('LiveAtlas failed to load:', e); + showSplashError('LiveAtlas failed to load:\n' + e, true); + } } -} +})(); diff --git a/src/providers/DynmapMapProvider.ts b/src/providers/DynmapMapProvider.ts index d7cc60a..5472866 100644 --- a/src/providers/DynmapMapProvider.ts +++ b/src/providers/DynmapMapProvider.ts @@ -140,7 +140,7 @@ export default class DynmapMapProvider extends MapProvider { this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION, config); this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION_HASH, response.confighash || 0); this.store.commit(MutationTypes.SET_MAX_PLAYERS, response.maxcount || 0); - this.store.commit(MutationTypes.SET_SERVER_MESSAGES, buildMessagesConfig(response)); + this.store.commit(MutationTypes.SET_MESSAGES, buildMessagesConfig(response)); this.store.commit(MutationTypes.SET_WORLDS, buildWorlds(response, this.config)); this.store.commit(MutationTypes.SET_COMPONENTS, buildComponents(response, this.config)); this.store.commit(MutationTypes.SET_LOGGED_IN, response.loggedin || false); diff --git a/src/providers/OverviewerMapProvider.ts b/src/providers/OverviewerMapProvider.ts index 05da1f7..ac9deb8 100644 --- a/src/providers/OverviewerMapProvider.ts +++ b/src/providers/OverviewerMapProvider.ts @@ -377,7 +377,7 @@ export default class OverviewerMapProvider extends MapProvider { const result = await runSandboxed(response + ' return overviewerConfig;'); this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION, OverviewerMapProvider.buildServerConfig(result)); - this.store.commit(MutationTypes.SET_SERVER_MESSAGES, OverviewerMapProvider.buildMessagesConfig(result)); + this.store.commit(MutationTypes.SET_MESSAGES, OverviewerMapProvider.buildMessagesConfig(result)); this.store.commit(MutationTypes.SET_WORLDS, this.buildWorlds(result)); this.store.commit(MutationTypes.SET_COMPONENTS, OverviewerMapProvider.buildComponents(result)); diff --git a/src/providers/Pl3xmapMapProvider.ts b/src/providers/Pl3xmapMapProvider.ts index 78a3f4c..423d469 100644 --- a/src/providers/Pl3xmapMapProvider.ts +++ b/src/providers/Pl3xmapMapProvider.ts @@ -467,7 +467,7 @@ export default class Pl3xmapMapProvider extends MapProvider { Pl3xmapMapProvider.getJSON(`${baseUrl}tiles/${name}/settings.json`, this.configurationAbort!.signal))); this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION, config); - this.store.commit(MutationTypes.SET_SERVER_MESSAGES, Pl3xmapMapProvider.buildMessagesConfig(response)); + this.store.commit(MutationTypes.SET_MESSAGES, Pl3xmapMapProvider.buildMessagesConfig(response)); this.store.commit(MutationTypes.SET_WORLDS, this.buildWorlds(response, worldResponses)); this.store.commit(MutationTypes.SET_COMPONENTS, Pl3xmapMapProvider.buildComponents(response)); } diff --git a/src/store/action-types.ts b/src/store/action-types.ts index 84f1dd7..ec479ba 100644 --- a/src/store/action-types.ts +++ b/src/store/action-types.ts @@ -15,6 +15,7 @@ */ export enum ActionTypes { + INIT = "init", LOAD_CONFIGURATION = "loadConfiguration", START_UPDATES = "startUpdates", STOP_UPDATES = "stopUpdates", diff --git a/src/store/actions.ts b/src/store/actions.ts index 9acb5c0..10993bf 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -20,7 +20,7 @@ import {State} from "@/store/state"; import {ActionTypes} from "@/store/action-types"; import {Mutations} from "@/store/mutations"; import {DynmapMarkerUpdate, DynmapTileUpdate} from "@/dynmap"; -import {LiveAtlasMarkerSet, LiveAtlasPlayer, LiveAtlasWorldDefinition} from "@/index"; +import {LiveAtlasGlobalConfig, LiveAtlasMarkerSet, LiveAtlasPlayer, LiveAtlasWorldDefinition} from "@/index"; import {nextTick} from "vue"; import {startUpdateHandling, stopUpdateHandling} from "@/util/markers"; @@ -32,6 +32,10 @@ type AugmentedActionContext = { } & Omit, "commit"> export interface Actions { + [ActionTypes.INIT]( + {commit}: AugmentedActionContext, + payload: LiveAtlasGlobalConfig, + ):Promise [ActionTypes.LOAD_CONFIGURATION]( {commit}: AugmentedActionContext, ):Promise @@ -71,6 +75,12 @@ export interface Actions { } export const actions: ActionTree & Actions = { + async [ActionTypes.INIT]({commit, state, dispatch}, config: LiveAtlasGlobalConfig): Promise { + commit(MutationTypes.SET_UI_CONFIGURATION, config?.ui || {}) + commit(MutationTypes.SET_MESSAGES, config?.messages || {}) + commit(MutationTypes.SET_SERVERS, config.servers) + }, + async [ActionTypes.LOAD_CONFIGURATION]({commit, state, dispatch}): Promise { await dispatch(ActionTypes.STOP_UPDATES, undefined); commit(MutationTypes.RESET, undefined); diff --git a/src/store/mutation-types.ts b/src/store/mutation-types.ts index 9fde586..da1a868 100644 --- a/src/store/mutation-types.ts +++ b/src/store/mutation-types.ts @@ -15,11 +15,12 @@ */ export enum MutationTypes { - INIT ='init', + SET_UI_CONFIGURATION ='setUIConfiguration', + SET_MESSAGES ='setMessages', + SET_SERVERS = 'setServers', SET_SERVER_CONFIGURATION = 'setServerConfiguration', SET_SERVER_CONFIGURATION_HASH = 'setServerConfigurationHash', - SET_SERVER_MESSAGES = 'setServerMessages', SET_WORLDS = 'setWorlds', SET_COMPONENTS = 'setComponents', SET_MARKER_SETS = 'setMarkerSets', diff --git a/src/store/mutations.ts b/src/store/mutations.ts index 461d66a..0c383eb 100644 --- a/src/store/mutations.ts +++ b/src/store/mutations.ts @@ -29,7 +29,6 @@ import { LiveAtlasUIElement, LiveAtlasWorldDefinition, LiveAtlasParsedUrl, - LiveAtlasGlobalConfig, LiveAtlasServerMessageConfig, LiveAtlasPlayer, LiveAtlasMarkerSet, @@ -38,9 +37,12 @@ import { LiveAtlasPartialComponentConfig, LiveAtlasComponentConfig, LiveAtlasUIModal, - LiveAtlasSidebarSectionState, LiveAtlasMarker, LiveAtlasMapViewTarget + LiveAtlasSidebarSectionState, + LiveAtlasMarker, + LiveAtlasMapViewTarget, + LiveAtlasGlobalMessageConfig, + LiveAtlasUIConfig, LiveAtlasServerDefinition } from "@/index"; -import {getGlobalMessages} from "@/util"; import {getServerMapProvider} from "@/util/config"; import {getDefaultPlayerImage} from "@/util/images"; @@ -50,10 +52,11 @@ export type CurrentMapPayload = { } export type Mutations = { - [MutationTypes.INIT](state: S, config: LiveAtlasGlobalConfig): void + [MutationTypes.SET_UI_CONFIGURATION](state: S, config: LiveAtlasUIConfig): void + [MutationTypes.SET_SERVERS](state: S, config: Map): void [MutationTypes.SET_SERVER_CONFIGURATION](state: S, config: LiveAtlasServerConfig): void [MutationTypes.SET_SERVER_CONFIGURATION_HASH](state: S, hash: number): void - [MutationTypes.SET_SERVER_MESSAGES](state: S, messages: LiveAtlasServerMessageConfig): void + [MutationTypes.SET_MESSAGES](state: S, messages: LiveAtlasGlobalMessageConfig|LiveAtlasServerMessageConfig): void [MutationTypes.SET_WORLDS](state: S, worlds: Array): void [MutationTypes.SET_COMPONENTS](state: S, components: LiveAtlasPartialComponentConfig | LiveAtlasComponentConfig): void [MutationTypes.SET_MARKER_SETS](state: S, markerSets: Map): void @@ -96,10 +99,7 @@ export type Mutations = { } export const mutations: MutationTree & Mutations = { - [MutationTypes.INIT](state: State, config: LiveAtlasGlobalConfig) { - const messageConfig = config?.messages || {}, - uiConfig = config?.ui || {}; - + [MutationTypes.SET_UI_CONFIGURATION](state: State, config: LiveAtlasUIConfig) { try { const uiSettings = JSON.parse(localStorage.getItem('uiSettings') || '{}'); @@ -120,33 +120,38 @@ export const mutations: MutationTree & Mutations = { console.warn('Failed to load saved UI settings', e); } - state.messages = Object.assign(state.messages, getGlobalMessages(messageConfig)); - - if(typeof uiConfig.playersAboveMarkers === 'boolean') { - state.ui.playersAboveMarkers = uiConfig.playersAboveMarkers; + if(typeof config.playersAboveMarkers === 'boolean') { + state.ui.playersAboveMarkers = config.playersAboveMarkers; } - if(typeof uiConfig.compactPlayerMarkers === 'boolean') { - state.ui.compactPlayerMarkers = uiConfig.compactPlayerMarkers; + if(typeof config.compactPlayerMarkers === 'boolean') { + state.ui.compactPlayerMarkers = config.compactPlayerMarkers; } - if(typeof uiConfig.disableContextMenu === 'boolean') { - state.ui.disableContextMenu = uiConfig.disableContextMenu; + if(typeof config.disableContextMenu === 'boolean') { + state.ui.disableContextMenu = config.disableContextMenu; } - if(typeof uiConfig.disableMarkerUI === 'boolean') { - state.ui.disableMarkerUI = uiConfig.disableMarkerUI; + if(typeof config.disableMarkerUI === 'boolean') { + state.ui.disableMarkerUI = config.disableMarkerUI; } - if(typeof uiConfig.playersSearch === 'boolean') { - state.ui.playersSearch = uiConfig.playersSearch; + if(typeof config.playersSearch === 'boolean') { + state.ui.playersSearch = config.playersSearch; } - if(typeof uiConfig.customLoginUrl === 'string') { - state.ui.customLoginUrl = uiConfig.customLoginUrl; + if(typeof config.customLoginUrl === 'string') { + state.ui.customLoginUrl = config.customLoginUrl; } + }, - state.servers = config.servers; + // Sets messages from the initial config fetch + [MutationTypes.SET_MESSAGES](state: State, messages: LiveAtlasServerMessageConfig|LiveAtlasGlobalMessageConfig) { + state.messages = Object.assign(state.messages, messages); + }, + + [MutationTypes.SET_SERVERS](state: State, servers: Map) { + state.servers = servers; if(state.currentServer && !state.servers.has(state.currentServer.id)) { state.currentServer = undefined; @@ -163,11 +168,6 @@ export const mutations: MutationTree & Mutations = { state.configurationHash = hash; }, - // Sets messages from the initial config fetch - [MutationTypes.SET_SERVER_MESSAGES](state: State, messages: LiveAtlasServerMessageConfig) { - state.messages = Object.assign(state.messages, messages); - }, - //Sets the list of worlds, and their settings, from the initial config fetch [MutationTypes.SET_WORLDS](state: State, worlds: Array) { state.worlds.clear();