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) { selectBot({ commit }, botId: string) {
commit('selectBot', botId); 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) { if (state.refreshing) {
return; return;
} }
@ -160,32 +177,23 @@ export default function createBotStore(store) {
} }
}, },
startRefresh({ getters, state, dispatch, commit }, runNow: boolean) { startRefresh({ state, dispatch, commit }) {
console.log('starting refresh');
// Start refresh timer
if (getters.hasBots !== true) {
console.log('Not logged in.');
return;
}
console.log('Starting automatic refresh.'); console.log('Starting automatic refresh.');
if (runNow) { dispatch('allRefreshFrequent');
dispatch('refreshFrequent', false);
dispatch('refreshSlow', true); if (!state.refreshInterval) {
// Set interval for refresh
const refreshInterval = window.setInterval(() => {
dispatch('allRefreshFrequent');
}, 5000);
commit('setRefreshInterval', refreshInterval);
} }
if (state.autoRefresh) { dispatch('allRefreshSlow', false);
if (!state.refreshInterval) { if (!state.refreshIntervalSlow) {
// Set interval for refresh const refreshIntervalSlow = window.setInterval(() => {
const refreshInterval = window.setInterval(() => { dispatch('allRefreshSlow', false);
dispatch('refreshFrequent'); }, 60000);
}, 5000); commit('setRefreshIntervalSlow', refreshIntervalSlow);
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

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

View File

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