Autocreate "all_" getters from list

This commit is contained in:
Matthias 2021-09-03 16:54:03 +02:00
parent 52ab11b376
commit 3147654b7c

View File

@ -18,11 +18,15 @@ export enum MultiBotStoreGetters {
selectedBotObj = 'selectedBotObj', selectedBotObj = 'selectedBotObj',
allAvailableBots = 'allAvailableBots', allAvailableBots = 'allAvailableBots',
allAvailableBotsList = 'allAvailableBotsList', allAvailableBotsList = 'allAvailableBotsList',
// Automatically created entries
allIsBotOnline = 'allIsBotOnline', allIsBotOnline = 'allIsBotOnline',
allAutoRefresh = 'allAutoRefresh', allAutoRefresh = 'allAutoRefresh',
allClosedTrades = 'allClosedTrades', allClosedTrades = 'allClosedTrades',
allProfit = 'allProfit',
} }
const createAllGetters = ['isBotOnline', 'autoRefresh', 'closedTrades', 'profit'];
export default function createBotStore(store) { export default function createBotStore(store) {
const state: FTMultiBotState = { const state: FTMultiBotState = {
selectedBot: '', selectedBot: '',
@ -60,28 +64,6 @@ export default function createBotStore(store) {
[MultiBotStoreGetters.allAvailableBotsList](state: FTMultiBotState): string[] { [MultiBotStoreGetters.allAvailableBotsList](state: FTMultiBotState): string[] {
return Object.keys(state.availableBots); return Object.keys(state.availableBots);
}, },
[MultiBotStoreGetters.allIsBotOnline](state: FTMultiBotState, getters): {} {
const result = {};
getters.allAvailableBotsList.forEach((e) => {
result[e] = getters[`${e}/isBotOnline`];
});
return result;
},
[MultiBotStoreGetters.allAutoRefresh](state: FTMultiBotState, getters): {} {
const result = {};
getters.allAvailableBotsList.forEach((e) => {
result[e] = getters[`${e}/autoRefresh`];
});
return result;
},
[MultiBotStoreGetters.allClosedTrades](state: FTMultiBotState, getters): MultiClosedTrades {
const result = {};
getters.allAvailableBotsList.forEach((e) => {
result[e] = getters[`${e}/closedTrades`];
});
return result;
},
}; };
// Autocreate getters from botStores // Autocreate getters from botStores
Object.keys(BotStoreGetters).forEach((e) => { Object.keys(BotStoreGetters).forEach((e) => {
@ -90,6 +72,20 @@ export default function createBotStore(store) {
}; };
}); });
// Create selected getters
createAllGetters.forEach((e: string) => {
const getterName = `all${e.charAt(0).toUpperCase() + e.slice(1)}`;
console.log('creatin ', e, getterName);
getters[getterName] = (state, getters) => {
const result = {};
getters.allAvailableBotsList.forEach((botId) => {
result[botId] = getters[`${botId}/${e}`];
});
return result;
};
});
const mutations = { const mutations = {
selectBot(state: FTMultiBotState, botId: string) { selectBot(state: FTMultiBotState, botId: string) {
if (botId in state.availableBots) { if (botId in state.availableBots) {
@ -178,7 +174,7 @@ export default function createBotStore(store) {
await dispatch('pingAll'); await dispatch('pingAll');
const updates: Promise<AxiosInstance>[] = []; const updates: Promise<AxiosInstance>[] = [];
updates.push(dispatch('allRefreshFrequent', false)); updates.push(dispatch('allRefreshFrequent', false));
updates.push(dispatch('allRefreshSlow')); updates.push(dispatch('allRefreshSlow', true));
// updates.push(dispatch('getDaily')); // updates.push(dispatch('getDaily'));
// updates.push(dispatch('getBalance')); // updates.push(dispatch('getBalance'));