Make INIT an action, split up state setting into multiple mutations

This commit is contained in:
James Lyne 2022-06-23 13:26:52 +01:00
parent 6a91404a27
commit 66fe07b17f
8 changed files with 78 additions and 63 deletions

View File

@ -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);
}
}
}
})();

View File

@ -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);

View File

@ -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));

View File

@ -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));
}

View File

@ -15,6 +15,7 @@
*/
export enum ActionTypes {
INIT = "init",
LOAD_CONFIGURATION = "loadConfiguration",
START_UPDATES = "startUpdates",
STOP_UPDATES = "stopUpdates",

View File

@ -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<ActionContext<State, State>, "commit">
export interface Actions {
[ActionTypes.INIT](
{commit}: AugmentedActionContext,
payload: LiveAtlasGlobalConfig,
):Promise<void>
[ActionTypes.LOAD_CONFIGURATION](
{commit}: AugmentedActionContext,
):Promise<void>
@ -71,6 +75,12 @@ export interface Actions {
}
export const actions: ActionTree<State, State> & Actions = {
async [ActionTypes.INIT]({commit, state, dispatch}, config: LiveAtlasGlobalConfig): Promise<void> {
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<void> {
await dispatch(ActionTypes.STOP_UPDATES, undefined);
commit(MutationTypes.RESET, undefined);

View File

@ -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',

View File

@ -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<S = State> = {
[MutationTypes.INIT](state: S, config: LiveAtlasGlobalConfig): void
[MutationTypes.SET_UI_CONFIGURATION](state: S, config: LiveAtlasUIConfig): void
[MutationTypes.SET_SERVERS](state: S, config: Map<string, LiveAtlasServerDefinition>): 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<LiveAtlasWorldDefinition>): void
[MutationTypes.SET_COMPONENTS](state: S, components: LiveAtlasPartialComponentConfig | LiveAtlasComponentConfig): void
[MutationTypes.SET_MARKER_SETS](state: S, markerSets: Map<string, LiveAtlasMarkerSet>): void
@ -96,10 +99,7 @@ export type Mutations<S = State> = {
}
export const mutations: MutationTree<State> & 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<State> & 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<string, LiveAtlasServerDefinition>) {
state.servers = servers;
if(state.currentServer && !state.servers.has(state.currentServer.id)) {
state.currentServer = undefined;
@ -163,11 +168,6 @@ export const mutations: MutationTree<State> & 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<LiveAtlasWorldDefinition>) {
state.worlds.clear();