From dc0a03daa879187ba4583a143ccd47885611c94e Mon Sep 17 00:00:00 2001 From: James Lyne Date: Mon, 30 Aug 2021 22:08:51 +0100 Subject: [PATCH] Refactor server configuration loading - Move store cleanup to single RESET mutation - Move showSplash, RESET and STOP_UPDATES calls into loadConfiguration --- src/App.vue | 29 +++++++--------- src/store/actions.ts | 4 --- src/store/mutation-types.ts | 4 +-- src/store/mutations.ts | 69 +++++++++++++++++++++---------------- 4 files changed, 53 insertions(+), 53 deletions(-) diff --git a/src/App.vue b/src/App.vue index d34050a..07615ea 100644 --- a/src/App.vue +++ b/src/App.vue @@ -49,10 +49,18 @@ export default defineComponent({ configurationHash = computed(() => store.state.configurationHash), chatBoxEnabled = computed(() => store.state.components.chatBox), chatVisible = computed(() => store.state.ui.visibleElements.has('chat')), - configAttempts = ref(0), + loggedIn = computed(() => store.state.loggedIn), + + loading = ref(false), + loadingAttempts = ref(0), loadConfiguration = async () => { try { + showSplash(); + loading.value = true; + + await store.dispatch(ActionTypes.STOP_UPDATES, undefined); + store.commit(MutationTypes.RESET, undefined); await store.dispatch(ActionTypes.LOAD_CONFIGURATION, undefined); await store.dispatch(ActionTypes.START_UPDATES, undefined); @@ -65,7 +73,7 @@ export default defineComponent({ (map as HTMLElement).focus(); } }); - } catch(e) { + } catch(e: any) { //Request was aborted, probably because another server was selected before the request finished. Don't retry if(e instanceof DOMException && e.name === 'AbortError') { return; @@ -73,8 +81,10 @@ export default defineComponent({ const error = `Failed to load server configuration for '${store.state.currentServer!.id}'`; console.error(`${error}:`, e); - showSplashError(`${error}\n${e}`, false, ++configAttempts.value); + showSplashError(`${error}\n${e}`, false, ++loadingAttempts.value); setTimeout(() => loadConfiguration(), 1000); + } finally { + loading.value = false; } }, @@ -141,28 +151,15 @@ export default defineComponent({ watch(title, (title) => document.title = title); watch(currentUrl, (url) => window.history.replaceState({}, '', url)); watch(currentServer, (newServer?: LiveAtlasServerDefinition) => { - showSplash(); - if(!newServer) { return; } - //Cleanup - store.commit(MutationTypes.CLEAR_PLAYERS, undefined); - store.commit(MutationTypes.SET_MAX_PLAYERS, 0); - store.commit(MutationTypes.CLEAR_CURRENT_MAP, undefined); - store.commit(MutationTypes.CLEAR_PARSED_URL, undefined); - store.commit(MutationTypes.CLEAR_WORLDS, undefined); - store.commit(MutationTypes.CLEAR_MARKER_SETS, undefined); - window.history.replaceState({}, '', newServer.id); loadConfiguration(); }, {deep: true}); watch(configurationHash, async (newHash, oldHash) => { if(newHash && oldHash) { - showSplash(); - store.commit(MutationTypes.CLEAR_PARSED_URL, undefined); - await store.dispatch(ActionTypes.STOP_UPDATES, undefined); await loadConfiguration(); } }); diff --git a/src/store/actions.ts b/src/store/actions.ts index 1023d3f..fd93c46 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -148,10 +148,6 @@ export const actions: ActionTree & Actions = { }, async [ActionTypes.STOP_UPDATES]({state}) { - if(!state.currentWorld) { - return Promise.reject("No current world"); - } - state.currentMapProvider!.stopUpdates(); }, diff --git a/src/store/mutation-types.ts b/src/store/mutation-types.ts index 2740d6c..68b2a39 100644 --- a/src/store/mutation-types.ts +++ b/src/store/mutation-types.ts @@ -22,10 +22,8 @@ export enum MutationTypes { CLEAR_SERVER_CONFIGURATION_HASH = 'clearServerConfigurationHash', SET_SERVER_MESSAGES = 'setServerMessages', SET_WORLDS = 'setWorlds', - CLEAR_WORLDS = 'clearWorlds', SET_COMPONENTS = 'setComponents', SET_MARKER_SETS = 'setMarkerSets', - CLEAR_MARKER_SETS = 'clearMarkerSets', ADD_WORLD = 'addWorld', SET_WORLD_STATE = 'setWorldState', ADD_MARKER_SET_UPDATES = 'addMarkerSetUpdates', @@ -47,7 +45,6 @@ export enum MutationTypes { SET_CURRENT_ZOOM = 'setCurrentZoom', SET_PARSED_URL = 'setParsedUrl', CLEAR_PARSED_URL = 'clearParsedUrl', - CLEAR_CURRENT_MAP = 'clearCurrentMap', SET_FOLLOW_TARGET = 'setFollowTarget', SET_PAN_TARGET = 'setPanTarget', @@ -63,4 +60,5 @@ export enum MutationTypes { SET_SIDEBAR_SECTION_COLLAPSED_STATE = 'setSidebarSectionCollapsedState', SET_LOGGED_IN = 'setLoggedIn', + RESET = 'reset' } diff --git a/src/store/mutations.ts b/src/store/mutations.ts index d9972f0..81d05a9 100644 --- a/src/store/mutations.ts +++ b/src/store/mutations.ts @@ -56,10 +56,8 @@ export type Mutations = { [MutationTypes.CLEAR_SERVER_CONFIGURATION_HASH](state: S): void [MutationTypes.SET_SERVER_MESSAGES](state: S, messages: LiveAtlasServerMessageConfig): void [MutationTypes.SET_WORLDS](state: S, worlds: Array): void - [MutationTypes.CLEAR_WORLDS](state: S): void [MutationTypes.SET_COMPONENTS](state: S, components: LiveAtlasPartialComponentConfig | LiveAtlasComponentConfig): void [MutationTypes.SET_MARKER_SETS](state: S, worlds: Map): void - [MutationTypes.CLEAR_MARKER_SETS](state: S): void [MutationTypes.ADD_WORLD](state: S, world: LiveAtlasWorldDefinition): void [MutationTypes.SET_WORLD_STATE](state: S, worldState: LiveAtlasWorldState): void [MutationTypes.ADD_MARKER_SET_UPDATES](state: S, updates: Map): void @@ -82,7 +80,6 @@ export type Mutations = { [MutationTypes.SET_CURRENT_ZOOM](state: S, payload: number): void [MutationTypes.SET_PARSED_URL](state: S, payload: LiveAtlasParsedUrl): void [MutationTypes.CLEAR_PARSED_URL](state: S): void - [MutationTypes.CLEAR_CURRENT_MAP](state: S): void [MutationTypes.SET_FOLLOW_TARGET](state: S, payload: LiveAtlasPlayer): void [MutationTypes.SET_PAN_TARGET](state: S, payload: LiveAtlasPlayer): void [MutationTypes.CLEAR_FOLLOW_TARGET](state: S, a?: void): void @@ -96,6 +93,7 @@ export type Mutations = { [MutationTypes.SET_SIDEBAR_SECTION_COLLAPSED_STATE](state: S, payload: {section: LiveAtlasSidebarSection, state: boolean}): void [MutationTypes.SET_LOGGED_IN](state: S, payload: boolean): void + [MutationTypes.RESET](state: S): void } export const mutations: MutationTree & Mutations = { @@ -211,18 +209,6 @@ export const mutations: MutationTree & Mutations = { } }, - [MutationTypes.CLEAR_WORLDS](state: State) { - state.worlds.clear(); - state.maps.clear(); - - state.followTarget = undefined; - state.panTarget = undefined; - - state.currentWorldState.timeOfDay = 0; - state.currentWorldState.raining = false; - state.currentWorldState.thundering = false; - }, - //Updates the state of optional components (chat, link button, etc) //Can be called with a LiveAtlasComponentConfig object to replace the whole state //or a LiveAtlasPartialComponentConfig object for partial updates to the existing state @@ -246,11 +232,6 @@ export const mutations: MutationTree & Mutations = { } }, - [MutationTypes.CLEAR_MARKER_SETS](state: State) { - state.markerSets.clear(); - state.pendingSetUpdates.clear(); - }, - [MutationTypes.ADD_WORLD](state: State, world: LiveAtlasWorldDefinition) { state.worlds.set(world.name, world); }, @@ -549,16 +530,6 @@ export const mutations: MutationTree & Mutations = { state.parsedUrl = payload; }, - //Clear the current map/world - [MutationTypes.CLEAR_CURRENT_MAP](state: State) { - state.markerSets.clear(); - state.pendingSetUpdates.clear(); - state.pendingTileUpdates = []; - - state.currentWorld = undefined; - state.currentMap = undefined; - }, - //Clear any existing parsed url [MutationTypes.CLEAR_PARSED_URL](state: State) { state.parsedUrl = undefined; @@ -633,5 +604,43 @@ export const mutations: MutationTree & Mutations = { [MutationTypes.SET_LOGGED_IN](state: State, payload: boolean): void { state.loggedIn = payload; + }, + + //Cleanup for switching servers or reloading the configuration + [MutationTypes.RESET](state: State): void { + state.maxPlayers = 0; + state.parsedUrl = undefined; + state.currentWorld = undefined; + state.currentMap = undefined; + + state.markerSets.clear(); + state.pendingSetUpdates.clear(); + state.pendingTileUpdates = []; + + state.worlds.clear(); + state.maps.clear(); + state.currentZoom = 0; + state.currentLocation = {x: 0, y: 0, z: 0}; + + state.followTarget = undefined; + state.panTarget = undefined; + + state.currentWorldState.timeOfDay = 0; + state.currentWorldState.raining = false; + state.currentWorldState.thundering = false; + + state.configuration.title = ''; + + state.components.markers.showLabels= false; + state.components.playerMarkers = undefined; + state.components.coordinatesControl = undefined; + state.components.clockControl = undefined; + state.components.linkControl = false; + state.components.layerControl = false; + state.components.logoControls = []; + state.components.chatSending = undefined; + state.components.chatBox = undefined; + state.components.chatBalloons = false; + state.components.login = false; } }