From be4b90f5a33d39f5ed77864b3bb636f3c084daca Mon Sep 17 00:00:00 2001 From: Matthias Date: Wed, 1 Sep 2021 19:55:47 +0200 Subject: [PATCH] move refreshing logic to subbots --- src/store/modules/botStoreWrapper.ts | 58 ++++++++++++++++------------ src/store/modules/ftbot/index.ts | 20 +++++++++- src/store/modules/ftbot/state.ts | 2 + 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/store/modules/botStoreWrapper.ts b/src/store/modules/botStoreWrapper.ts index cf970c7f..97b81a1e 100644 --- a/src/store/modules/botStoreWrapper.ts +++ b/src/store/modules/botStoreWrapper.ts @@ -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 }) { diff --git a/src/store/modules/ftbot/index.ts b/src/store/modules/ftbot/index.ts index 3f1700eb..2b091091 100644 --- a/src/store/modules/ftbot/index.ts +++ b/src/store/modules/ftbot/index.ts @@ -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) { diff --git a/src/store/modules/ftbot/state.ts b/src/store/modules/ftbot/state.ts index f08c0378..ef8d30db 100644 --- a/src/store/modules/ftbot/state.ts +++ b/src/store/modules/ftbot/state.ts @@ -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,