move refreshing logic to subbots

This commit is contained in:
Matthias 2021-09-01 19:55:47 +02:00
parent 9d682da2ca
commit be4b90f5a3
3 changed files with 53 additions and 27 deletions

View File

@ -141,7 +141,24 @@ export default function createBotStore(store) {
selectBot({ commit }, botId: string) {
commit('selectBot', botId);
},
async refreshFull({ dispatch, state, commit }, forceUpdate = false) {
allRefreshFrequent({ dispatch, getters }) {
console.log('dispatching all frequent refreshes');
getters.allAvailableBotsList.forEach((e) => {
if (getters[`${e}/${BotStoreGetters.autoRefresh}`]) {
console.log('refreshing ', e);
dispatch(`${e}/${BotStoreActions.refreshFrequent}`);
}
});
},
allRefreshSlow({ dispatch, getters }) {
console.log('dispatching all slow refreshes');
getters.allAvailableBotsList.forEach((e) => {
if (getters[`${e}/${BotStoreGetters.autoRefresh}`]) {
dispatch(`${e}/${BotStoreActions.refreshSlow}`);
}
});
},
async refreshxxFull({ commit, dispatch, getters, state }, forceUpdate = false) {
if (state.refreshing) {
return;
}
@ -160,32 +177,23 @@ export default function createBotStore(store) {
}
},
startRefresh({ getters, state, dispatch, commit }, runNow: boolean) {
console.log('starting refresh');
// Start refresh timer
if (getters.hasBots !== true) {
console.log('Not logged in.');
return;
}
startRefresh({ state, dispatch, commit }) {
console.log('Starting automatic refresh.');
if (runNow) {
dispatch('refreshFrequent', false);
dispatch('refreshSlow', true);
dispatch('allRefreshFrequent');
if (!state.refreshInterval) {
// Set interval for refresh
const refreshInterval = window.setInterval(() => {
dispatch('allRefreshFrequent');
}, 5000);
commit('setRefreshInterval', refreshInterval);
}
if (state.autoRefresh) {
if (!state.refreshInterval) {
// Set interval for refresh
const refreshInterval = window.setInterval(() => {
dispatch('refreshFrequent');
}, 5000);
commit('setRefreshInterval', refreshInterval);
}
if (!state.refreshIntervalSlow) {
const refreshIntervalSlow = window.setInterval(() => {
dispatch('refreshSlow', false);
}, 60000);
commit('setRefreshIntervalSlow', refreshIntervalSlow);
}
dispatch('allRefreshSlow', false);
if (!state.refreshIntervalSlow) {
const refreshIntervalSlow = window.setInterval(() => {
dispatch('allRefreshSlow', false);
}, 60000);
commit('setRefreshIntervalSlow', refreshIntervalSlow);
}
},
stopRefresh({ state, commit }: { state: FTMultiBotState; commit: any }) {

View File

@ -45,7 +45,11 @@ import { showAlert } from '../alerts';
export enum BotStoreGetters {
botName = 'botName',
isBotOnline = 'isBotOnline',
autoRefresh = 'autoRefresh',
refreshNow = 'refreshNow',
refreshing = 'refreshing',
openTrades = 'openTrades',
openTradeCount = 'openTradeCount',
tradeDetail = 'tradeDetail',
@ -152,6 +156,15 @@ export function createBotSubStore(botId: string) {
[BotStoreGetters.autoRefresh](state: FtbotStateType): boolean {
return state.autoRefresh;
},
[BotStoreGetters.refreshNow](state, getters, rootState, rootGetters): boolean {
const bgRefresh = rootGetters['uiSettings/backgroundSync'];
const selectedBot = rootGetters['ftbot/selectedBot'];
if ((selectedBot === botId || bgRefresh) && getters.autoRefresh) {
return true;
}
return false;
},
[BotStoreGetters.plotConfig](state: FtbotStateType) {
return state.customPlotConfig[state.plotConfigName] || { ...EMPTY_PLOTCONFIG };
},
@ -301,6 +314,9 @@ export function createBotSubStore(botId: string) {
setAutoRefresh(state: FtbotStateType, newRefreshValue: boolean) {
state.autoRefresh = newRefreshValue;
},
setRefreshing(state, refreshing: boolean) {
state.refreshing = refreshing;
},
updateRefreshRequired(state: FtbotStateType, refreshRequired: boolean) {
state.refreshRequired = refreshRequired;
},
@ -421,7 +437,7 @@ export function createBotSubStore(botId: string) {
commit('updateRefreshRequired', refreshRequired);
},
[BotStoreActions.setAutoRefresh]({ dispatch, commit }, newRefreshValue) {
// commit('setAutoRefresh', newRefreshValue);
commit('setAutoRefresh', newRefreshValue);
// TODO: Investigate this -
// this ONLY works if ReloadControl is only visible once,otherwise it triggers twice
if (newRefreshValue) {
@ -435,7 +451,7 @@ export function createBotSubStore(botId: string) {
commit('setIsBotOnline', refreshRequired);
},
[BotStoreActions.refreshOnce]({ dispatch }) {
dispatch('getVersion');
dispatch(BotStoreActions.getVersion);
},
async [BotStoreActions.refreshSlow]({ dispatch, getters, state }, forceUpdate = false) {
if (state.refreshing && !forceUpdate) {

View File

@ -20,6 +20,7 @@ export interface FtbotStateType {
ping: string;
isBotOnline: boolean;
autoRefresh: boolean;
refreshing: boolean;
version: string;
lastLogs: LogLine[];
refreshRequired: boolean;
@ -62,6 +63,7 @@ const state = (): FtbotStateType => {
ping: '',
isBotOnline: false,
autoRefresh: false,
refreshing: false,
version: '',
lastLogs: [],
refreshRequired: true,