Move refresh actions to subBotStore

This commit is contained in:
Matthias 2021-08-29 15:56:54 +02:00
parent ea71dcc190
commit b23c122629
2 changed files with 50 additions and 44 deletions

View File

@ -165,36 +165,7 @@ export default function createBotStore(store) {
commit('setRefreshing', false); commit('setRefreshing', false);
} }
}, },
async refreshSlow({ dispatch, getters, state }, forceUpdate = false) {
if (state.refreshing && !forceUpdate) {
return;
}
// Refresh data only when needed
if (forceUpdate || getters[`${BotStoreGetters.refreshRequired}`]) {
const updates: Promise<AxiosInstance>[] = [];
updates.push(dispatch('getPerformance'));
updates.push(dispatch('getProfit'));
updates.push(dispatch('getTrades'));
/* white/blacklist might be refreshed more often as they are not expensive on the backend */
updates.push(dispatch('getWhitelist'));
updates.push(dispatch('getBlacklist'));
await Promise.all(updates);
dispatch('setRefreshRequired', false);
}
},
refreshFrequent({ dispatch }, slow = true) {
if (slow) {
dispatch('refreshSlow', false);
}
// Refresh data that's needed in near realtime
dispatch('getOpenTrades');
dispatch('getState');
dispatch('getLocks');
},
refreshOnce({ dispatch }) {
dispatch('getVersion');
},
startRefresh({ getters, state, dispatch, commit }, runNow: boolean) { startRefresh({ getters, state, dispatch, commit }, runNow: boolean) {
console.log('starting refresh'); console.log('starting refresh');
// Start refresh timer // Start refresh timer
@ -205,22 +176,22 @@ export default function createBotStore(store) {
console.log('Starting automatic refresh.'); console.log('Starting automatic refresh.');
if (runNow) { if (runNow) {
dispatch('refreshFrequent', false); dispatch('refreshFrequent', false);
}
if (state.autoRefresh && !state.refreshInterval) {
// Set interval for refresh
const refreshInterval = window.setInterval(() => {
dispatch('refreshFrequent');
}, 5000);
commit('setRefreshInterval', refreshInterval);
}
if (runNow) {
dispatch('refreshSlow', true); dispatch('refreshSlow', true);
} }
if (state.autoRefresh && !state.refreshIntervalSlow) { if (state.autoRefresh) {
const refreshIntervalSlow = window.setInterval(() => { if (!state.refreshInterval) {
dispatch('refreshSlow', false); // Set interval for refresh
}, 60000); const refreshInterval = window.setInterval(() => {
commit('setRefreshIntervalSlow', refreshIntervalSlow); dispatch('refreshFrequent');
}, 5000);
commit('setRefreshInterval', refreshInterval);
}
if (!state.refreshIntervalSlow) {
const refreshIntervalSlow = window.setInterval(() => {
dispatch('refreshSlow', false);
}, 60000);
commit('setRefreshIntervalSlow', refreshIntervalSlow);
}
} }
}, },
stopRefresh({ state, commit }: { state: FTMultiBotState; commit: any }) { stopRefresh({ state, commit }: { state: FTMultiBotState; commit: any }) {

View File

@ -37,7 +37,8 @@ import {
getAllPlotConfigNames, getAllPlotConfigNames,
storePlotConfigName, storePlotConfigName,
} from '@/shared/storage'; } from '@/shared/storage';
import axios from 'axios'; import axios, { AxiosInstance } from 'axios';
import state, { FtbotStateType } from './state'; import state, { FtbotStateType } from './state';
import { showAlert } from '../alerts'; import { showAlert } from '../alerts';
@ -89,6 +90,9 @@ export enum BotStoreGetters {
export enum BotStoreActions { export enum BotStoreActions {
ping = 'ping', ping = 'ping',
setRefreshRequired = 'setRefreshRequired', setRefreshRequired = 'setRefreshRequired',
refreshSlow = 'refreshSlow',
refreshFrequent = 'refreshFrequent',
refreshOnce = 'refreshOnce',
setDetailTrade = 'setDetailTrade', setDetailTrade = 'setDetailTrade',
setSelectedPair = 'setSelectedPair', setSelectedPair = 'setSelectedPair',
getTrades = 'getTrades', getTrades = 'getTrades',
@ -406,6 +410,37 @@ export function createBotSubStore(botId: string) {
[BotStoreActions.setRefreshRequired]({ commit }, refreshRequired: boolean) { [BotStoreActions.setRefreshRequired]({ commit }, refreshRequired: boolean) {
commit('updateRefreshRequired', refreshRequired); commit('updateRefreshRequired', refreshRequired);
}, },
[BotStoreActions.refreshOnce]({ dispatch }) {
dispatch('getVersion');
},
async [BotStoreActions.refreshSlow]({ dispatch, getters, state }, forceUpdate = false) {
if (state.refreshing && !forceUpdate) {
return;
}
// Refresh data only when needed
if (forceUpdate || getters[`${BotStoreGetters.refreshRequired}`]) {
const updates: Promise<AxiosInstance>[] = [];
updates.push(dispatch('getPerformance'));
updates.push(dispatch('getProfit'));
updates.push(dispatch('getTrades'));
/* white/blacklist might be refreshed more often as they are not expensive on the backend */
updates.push(dispatch('getWhitelist'));
updates.push(dispatch('getBlacklist'));
await Promise.all(updates);
dispatch('setRefreshRequired', false);
}
},
[BotStoreActions.refreshFrequent]({ dispatch }, slow = true) {
if (slow) {
dispatch('refreshSlow', false);
}
// Refresh data that's needed in near realtime
dispatch('getOpenTrades');
dispatch('getState');
dispatch('getLocks');
},
[BotStoreActions.setDetailTrade]({ commit }, trade: Trade) { [BotStoreActions.setDetailTrade]({ commit }, trade: Trade) {
commit('setDetailTrade', trade); commit('setDetailTrade', trade);
}, },