Prevent api spamming

This commit is contained in:
Matthias 2021-04-20 21:18:57 +02:00
parent 267f462009
commit 741e7878f9

View File

@ -3,6 +3,7 @@ import Vuex from 'vuex';
import userService from '@/shared/userService'; import userService from '@/shared/userService';
import { getCurrentTheme, getTheme, storeCurrentTheme } from '@/shared/themes'; import { getCurrentTheme, getTheme, storeCurrentTheme } from '@/shared/themes';
import { AxiosInstance } from 'axios';
import ftbotModule, { BotStoreGetters } from './modules/ftbot'; import ftbotModule, { BotStoreGetters } from './modules/ftbot';
import alertsModule from './modules/alerts'; import alertsModule from './modules/alerts';
import layoutModule from './modules/layout'; import layoutModule from './modules/layout';
@ -16,6 +17,7 @@ export default new Vuex.Store({
state: { state: {
ping: '', ping: '',
loggedIn: userService.loggedIn(), loggedIn: userService.loggedIn(),
refreshing: false,
autoRefresh: JSON.parse(localStorage.getItem(AUTO_REFRESH) || '{}'), autoRefresh: JSON.parse(localStorage.getItem(AUTO_REFRESH) || '{}'),
isBotOnline: false, isBotOnline: false,
currentTheme: initCurrentTheme, currentTheme: initCurrentTheme,
@ -49,6 +51,9 @@ export default new Vuex.Store({
setAutoRefresh(state, newRefreshValue: boolean) { setAutoRefresh(state, newRefreshValue: boolean) {
state.autoRefresh = newRefreshValue; state.autoRefresh = newRefreshValue;
}, },
setRefreshing(state, refreshing: boolean) {
state.refreshing = refreshing;
},
setIsBotOnline(state, isBotOnline: boolean) { setIsBotOnline(state, isBotOnline: boolean) {
state.isBotOnline = isBotOnline; state.isBotOnline = isBotOnline;
}, },
@ -78,21 +83,37 @@ export default new Vuex.Store({
refreshOnce({ dispatch }) { refreshOnce({ dispatch }) {
dispatch('ftbot/getVersion'); dispatch('ftbot/getVersion');
}, },
refreshAll({ dispatch }, forceUpdate = false) { async refreshAll({ dispatch, state, commit }, forceUpdate = false) {
dispatch('refreshFrequent'); if (state.refreshing) {
dispatch('refreshSlow', forceUpdate); return;
dispatch('ftbot/getDaily'); }
dispatch('ftbot/getBalance'); commit('setRefreshing', true);
/* white/blacklist might be refreshed more often as they are not expensive on the backend */ try {
dispatch('ftbot/getWhitelist'); const updates: Promise<AxiosInstance>[] = [];
dispatch('ftbot/getBlacklist'); updates.push(dispatch('refreshFrequent', false));
updates.push(dispatch('refreshSlow', forceUpdate));
updates.push(dispatch('ftbot/getDaily'));
updates.push(dispatch('ftbot/getBalance'));
/* white/blacklist might be refreshed more often as they are not expensive on the backend */
updates.push(dispatch('ftbot/getWhitelist'));
updates.push(dispatch('ftbot/getBlacklist'));
await Promise.all(updates);
console.log('refreshing_end');
} finally {
commit('setRefreshing', false);
}
}, },
refreshSlow({ dispatch, commit, getters }, forceUpdate = false) { async refreshSlow({ dispatch, commit, getters, state }, forceUpdate = false) {
if (state.refreshing && !forceUpdate) {
return;
}
// Refresh data only when needed // Refresh data only when needed
if (forceUpdate || getters[`ftbot/${BotStoreGetters.refreshRequired}`]) { if (forceUpdate || getters[`ftbot/${BotStoreGetters.refreshRequired}`]) {
dispatch('ftbot/getPerformance'); const updates: Promise<AxiosInstance>[] = [];
dispatch('ftbot/getProfit'); updates.push(dispatch('ftbot/getPerformance'));
dispatch('ftbot/getTrades'); updates.push(dispatch('ftbot/getProfit'));
updates.push(dispatch('ftbot/getTrades'));
await Promise.all(updates);
commit('ftbot/updateRefreshRequired', false); commit('ftbot/updateRefreshRequired', false);
} }
}, },