Reorder botStoreWrapper

This commit is contained in:
Matthias 2021-08-28 17:56:51 +02:00
parent a2d8b68abb
commit 8579499418

View File

@ -1,22 +1,48 @@
import { BotStoreActions, BotStoreGetters, createBotSubStore } from './ftbot'; import { BotStoreActions, BotStoreGetters, createBotSubStore } from './ftbot';
interface FTMultiBotState {
selectedBot: string;
availableBots: string[];
}
export default function createBotStore() { export default function createBotStore() {
const state = { const state: FTMultiBotState = {
selectedBot: 'ftbot.0', selectedBot: 'ftbot.0',
availableBots: ['ftbot.0'],
}; };
const actions = {
// Actions automatically filled below // All getters working on all bots should be prefixed with all.
addBot(context, botName: string) { const getters = {
// selectedBot(state: FTMultiBotState) {
return state.selectedBot;
},
allAvailableBots(state: FTMultiBotState) {
return state.availableBots;
}, },
}; };
const getters = {};
// Autocreate getters // Autocreate getters
Object.keys(BotStoreGetters).forEach((e) => { Object.keys(BotStoreGetters).forEach((e) => {
getters[e] = (state, getters) => { getters[e] = (state, getters) => {
return getters[`${state.selectedBot}/${e}`]; return getters[`${state.selectedBot}/${e}`];
}; };
}); });
const mutations = {
addBot(state: FTMultiBotState, botName: string) {
state.availableBots = [...state.availableBots, botName];
},
};
const actions = {
// Actions automatically filled below
addBot({ getters, commit }, botName: string) {
if (getters.allAvailableBots.includes(botName)) {
// throw 'Bot already present';
// TODO: handle error!
}
commit('addBot', botName);
},
};
// Autocreate Actions // Autocreate Actions
Object.keys(BotStoreActions).forEach((e) => { Object.keys(BotStoreActions).forEach((e) => {
actions[e] = ({ state, dispatch }, ...args) => { actions[e] = ({ state, dispatch }, ...args) => {
@ -30,6 +56,8 @@ export default function createBotStore() {
'ftbot.0': createBotSubStore(), 'ftbot.0': createBotSubStore(),
}, },
state, state,
mutations,
getters, getters,
actions, actions,
}; };