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 {showSplashError} from "@/util/splash";
import ConfigurationError from "@/errors/ConfigurationError"; import ConfigurationError from "@/errors/ConfigurationError";
import OverviewerMapProvider from "@/providers/OverviewerMapProvider"; import OverviewerMapProvider from "@/providers/OverviewerMapProvider";
import {ActionTypes} from "@/store/action-types";
const splash = document.getElementById('splash'), const splash = document.getElementById('splash'),
svgs = import.meta.globEager('/assets/icons/*.svg'); svgs = import.meta.globEager('/assets/icons/*.svg');
@ -61,37 +62,39 @@ registerMapProvider('overviewer', OverviewerMapProvider);
const config = window.liveAtlasConfig; const config = window.liveAtlasConfig;
window.liveAtlasLoaded = true; window.liveAtlasLoaded = true;
try { (async() => {
config.servers = loadConfig(config); try {
store.commit(MutationTypes.INIT, config); config.servers = loadConfig(config);
await store.dispatch(ActionTypes.INIT, config);
if(store.state.servers.size > 1) { if (store.state.servers.size > 1) {
const lastSegment = window.location.pathname.split('/').pop(), const lastSegment = window.location.pathname.split('/').pop(),
serverName = lastSegment && store.state.servers.has(lastSegment) ? lastSegment : store.state.servers.keys().next().value; serverName = lastSegment && store.state.servers.has(lastSegment) ? lastSegment : store.state.servers.keys().next().value;
//Update url if server doesn't exist //Update url if server doesn't exist
if(serverName !== lastSegment) { if (serverName !== lastSegment) {
window.history.replaceState({}, '', serverName + window.location.hash); 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); const app = createApp(App)
} else { .use(store)
store.commit(MutationTypes.SET_CURRENT_SERVER, store.state.servers.keys().next().value); .use(Notifications)
} .use(VueClipboard);
const app = createApp(App) // app.config.performance = true;
.use(store) app.mount('#app');
.use(Notifications) } catch (e) {
.use(VueClipboard); if (e instanceof ConfigurationError) {
console.error('LiveAtlas configuration is invalid:', e);
// app.config.performance = true; showSplashError('LiveAtlas configuration is invalid:\n' + e, true);
app.mount('#app'); } else {
} catch (e) { console.error('LiveAtlas failed to load:', e);
if(e instanceof ConfigurationError) { showSplashError('LiveAtlas failed to load:\n' + e, true);
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, config);
this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION_HASH, response.confighash || 0); 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_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_WORLDS, buildWorlds(response, this.config));
this.store.commit(MutationTypes.SET_COMPONENTS, buildComponents(response, this.config)); this.store.commit(MutationTypes.SET_COMPONENTS, buildComponents(response, this.config));
this.store.commit(MutationTypes.SET_LOGGED_IN, response.loggedin || false); 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;'); const result = await runSandboxed(response + ' return overviewerConfig;');
this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION, OverviewerMapProvider.buildServerConfig(result)); 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_WORLDS, this.buildWorlds(result));
this.store.commit(MutationTypes.SET_COMPONENTS, OverviewerMapProvider.buildComponents(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))); Pl3xmapMapProvider.getJSON(`${baseUrl}tiles/${name}/settings.json`, this.configurationAbort!.signal)));
this.store.commit(MutationTypes.SET_SERVER_CONFIGURATION, config); 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_WORLDS, this.buildWorlds(response, worldResponses));
this.store.commit(MutationTypes.SET_COMPONENTS, Pl3xmapMapProvider.buildComponents(response)); this.store.commit(MutationTypes.SET_COMPONENTS, Pl3xmapMapProvider.buildComponents(response));
} }

View File

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

View File

@ -20,7 +20,7 @@ import {State} from "@/store/state";
import {ActionTypes} from "@/store/action-types"; import {ActionTypes} from "@/store/action-types";
import {Mutations} from "@/store/mutations"; import {Mutations} from "@/store/mutations";
import {DynmapMarkerUpdate, DynmapTileUpdate} from "@/dynmap"; import {DynmapMarkerUpdate, DynmapTileUpdate} from "@/dynmap";
import {LiveAtlasMarkerSet, LiveAtlasPlayer, LiveAtlasWorldDefinition} from "@/index"; import {LiveAtlasGlobalConfig, LiveAtlasMarkerSet, LiveAtlasPlayer, LiveAtlasWorldDefinition} from "@/index";
import {nextTick} from "vue"; import {nextTick} from "vue";
import {startUpdateHandling, stopUpdateHandling} from "@/util/markers"; import {startUpdateHandling, stopUpdateHandling} from "@/util/markers";
@ -32,6 +32,10 @@ type AugmentedActionContext = {
} & Omit<ActionContext<State, State>, "commit"> } & Omit<ActionContext<State, State>, "commit">
export interface Actions { export interface Actions {
[ActionTypes.INIT](
{commit}: AugmentedActionContext,
payload: LiveAtlasGlobalConfig,
):Promise<void>
[ActionTypes.LOAD_CONFIGURATION]( [ActionTypes.LOAD_CONFIGURATION](
{commit}: AugmentedActionContext, {commit}: AugmentedActionContext,
):Promise<void> ):Promise<void>
@ -71,6 +75,12 @@ export interface Actions {
} }
export const actions: ActionTree<State, State> & 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> { async [ActionTypes.LOAD_CONFIGURATION]({commit, state, dispatch}): Promise<void> {
await dispatch(ActionTypes.STOP_UPDATES, undefined); await dispatch(ActionTypes.STOP_UPDATES, undefined);
commit(MutationTypes.RESET, undefined); commit(MutationTypes.RESET, undefined);

View File

@ -15,11 +15,12 @@
*/ */
export enum MutationTypes { export enum MutationTypes {
INIT ='init', SET_UI_CONFIGURATION ='setUIConfiguration',
SET_MESSAGES ='setMessages',
SET_SERVERS = 'setServers',
SET_SERVER_CONFIGURATION = 'setServerConfiguration', SET_SERVER_CONFIGURATION = 'setServerConfiguration',
SET_SERVER_CONFIGURATION_HASH = 'setServerConfigurationHash', SET_SERVER_CONFIGURATION_HASH = 'setServerConfigurationHash',
SET_SERVER_MESSAGES = 'setServerMessages',
SET_WORLDS = 'setWorlds', SET_WORLDS = 'setWorlds',
SET_COMPONENTS = 'setComponents', SET_COMPONENTS = 'setComponents',
SET_MARKER_SETS = 'setMarkerSets', SET_MARKER_SETS = 'setMarkerSets',

View File

@ -29,7 +29,6 @@ import {
LiveAtlasUIElement, LiveAtlasUIElement,
LiveAtlasWorldDefinition, LiveAtlasWorldDefinition,
LiveAtlasParsedUrl, LiveAtlasParsedUrl,
LiveAtlasGlobalConfig,
LiveAtlasServerMessageConfig, LiveAtlasServerMessageConfig,
LiveAtlasPlayer, LiveAtlasPlayer,
LiveAtlasMarkerSet, LiveAtlasMarkerSet,
@ -38,9 +37,12 @@ import {
LiveAtlasPartialComponentConfig, LiveAtlasPartialComponentConfig,
LiveAtlasComponentConfig, LiveAtlasComponentConfig,
LiveAtlasUIModal, LiveAtlasUIModal,
LiveAtlasSidebarSectionState, LiveAtlasMarker, LiveAtlasMapViewTarget LiveAtlasSidebarSectionState,
LiveAtlasMarker,
LiveAtlasMapViewTarget,
LiveAtlasGlobalMessageConfig,
LiveAtlasUIConfig, LiveAtlasServerDefinition
} from "@/index"; } from "@/index";
import {getGlobalMessages} from "@/util";
import {getServerMapProvider} from "@/util/config"; import {getServerMapProvider} from "@/util/config";
import {getDefaultPlayerImage} from "@/util/images"; import {getDefaultPlayerImage} from "@/util/images";
@ -50,10 +52,11 @@ export type CurrentMapPayload = {
} }
export type Mutations<S = State> = { 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](state: S, config: LiveAtlasServerConfig): void
[MutationTypes.SET_SERVER_CONFIGURATION_HASH](state: S, hash: number): 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_WORLDS](state: S, worlds: Array<LiveAtlasWorldDefinition>): void
[MutationTypes.SET_COMPONENTS](state: S, components: LiveAtlasPartialComponentConfig | LiveAtlasComponentConfig): void [MutationTypes.SET_COMPONENTS](state: S, components: LiveAtlasPartialComponentConfig | LiveAtlasComponentConfig): void
[MutationTypes.SET_MARKER_SETS](state: S, markerSets: Map<string, LiveAtlasMarkerSet>): 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 = { export const mutations: MutationTree<State> & Mutations = {
[MutationTypes.INIT](state: State, config: LiveAtlasGlobalConfig) { [MutationTypes.SET_UI_CONFIGURATION](state: State, config: LiveAtlasUIConfig) {
const messageConfig = config?.messages || {},
uiConfig = config?.ui || {};
try { try {
const uiSettings = JSON.parse(localStorage.getItem('uiSettings') || '{}'); 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); console.warn('Failed to load saved UI settings', e);
} }
state.messages = Object.assign(state.messages, getGlobalMessages(messageConfig)); if(typeof config.playersAboveMarkers === 'boolean') {
state.ui.playersAboveMarkers = config.playersAboveMarkers;
if(typeof uiConfig.playersAboveMarkers === 'boolean') {
state.ui.playersAboveMarkers = uiConfig.playersAboveMarkers;
} }
if(typeof uiConfig.compactPlayerMarkers === 'boolean') { if(typeof config.compactPlayerMarkers === 'boolean') {
state.ui.compactPlayerMarkers = uiConfig.compactPlayerMarkers; state.ui.compactPlayerMarkers = config.compactPlayerMarkers;
} }
if(typeof uiConfig.disableContextMenu === 'boolean') { if(typeof config.disableContextMenu === 'boolean') {
state.ui.disableContextMenu = uiConfig.disableContextMenu; state.ui.disableContextMenu = config.disableContextMenu;
} }
if(typeof uiConfig.disableMarkerUI === 'boolean') { if(typeof config.disableMarkerUI === 'boolean') {
state.ui.disableMarkerUI = uiConfig.disableMarkerUI; state.ui.disableMarkerUI = config.disableMarkerUI;
} }
if(typeof uiConfig.playersSearch === 'boolean') { if(typeof config.playersSearch === 'boolean') {
state.ui.playersSearch = uiConfig.playersSearch; state.ui.playersSearch = config.playersSearch;
} }
if(typeof uiConfig.customLoginUrl === 'string') { if(typeof config.customLoginUrl === 'string') {
state.ui.customLoginUrl = uiConfig.customLoginUrl; 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)) { if(state.currentServer && !state.servers.has(state.currentServer.id)) {
state.currentServer = undefined; state.currentServer = undefined;
@ -163,11 +168,6 @@ export const mutations: MutationTree<State> & Mutations = {
state.configurationHash = hash; 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 //Sets the list of worlds, and their settings, from the initial config fetch
[MutationTypes.SET_WORLDS](state: State, worlds: Array<LiveAtlasWorldDefinition>) { [MutationTypes.SET_WORLDS](state: State, worlds: Array<LiveAtlasWorldDefinition>) {
state.worlds.clear(); state.worlds.clear();