Add fetch lifespan to avoid repeated stores refreshes

This commit is contained in:
Bubka 2023-11-02 14:54:12 +01:00
parent d8861ba475
commit 2dc8734980
4 changed files with 51 additions and 41 deletions

View File

@ -9,6 +9,7 @@ export const useGroups = defineStore({
state: () => { state: () => {
return { return {
items: [], items: [],
fetchedOn: null,
} }
}, },
@ -58,9 +59,17 @@ export const useGroups = defineStore({
* Fetches the groups collection from the backend * Fetches the groups collection from the backend
*/ */
async fetch() { async fetch() {
// We do not want to fetch fresh data multiple times in the same 2s timespan
const age = Math.floor(Date.now() - this.fetchedOn)
const isNotFresh = age > 2000
if (isNotFresh) {
this.fetchedOn = Date.now()
await groupService.getAll().then(response => { await groupService.getAll().then(response => {
this.items = response.data this.items = response.data
}) })
}
}, },
/** /**

View File

@ -12,6 +12,8 @@ export const useTwofaccounts = defineStore({
items: [], items: [],
selectedIds: [], selectedIds: [],
filter: '', filter: '',
backendWasNewer: false,
fetchedOn: null,
} }
}, },
@ -69,7 +71,7 @@ export const useTwofaccounts = defineStore({
hasNoneSelected(state) { hasNoneSelected(state) {
return state.selectedIds.length == 0 return state.selectedIds.length == 0
}, }
}, },
actions: { actions: {
@ -78,35 +80,36 @@ export const useTwofaccounts = defineStore({
* Refreshes the accounts collection using the backend * Refreshes the accounts collection using the backend
*/ */
async fetch() { async fetch() {
await twofaccountService.getAll(! useUserStore().preferences.getOtpOnRequest).then(response => { // We do not want to fetch fresh data multiple times in the same 2s timespan
this.items = response.data const age = Math.floor(Date.now() - this.fetchedOn)
}) const isNotFresh = age > 2000
},
/** if (isNotFresh) {
* Tells if the store is up-to-date with the backend this.fetchedOn = Date.now()
*/
async isUpToDateWithBackend() { await twofaccountService.getAll(! useUserStore().preferences.getOtpOnRequest).then(response => {
let isUpToDate = true // Defines if the store was up-to-date with the backend
await twofaccountService.getAll().then(response => { this.backendWasNewer = response.data.length !== this.items.length
isUpToDate = response.data.length === this.items.length
this.items.forEach((item) => { this.items.forEach((item) => {
let matchingBackendItem = response.data.find(e => e.id === item.id) let matchingBackendItem = response.data.find(e => e.id === item.id)
if (matchingBackendItem == undefined) { if (matchingBackendItem == undefined) {
isUpToDate = false this.backendWasNewer = true
return; return;
} }
for (const field in item) { for (const field in item) {
if (item[field] != matchingBackendItem[field]) { if (field !== 'otp' && item[field] != matchingBackendItem[field]) {
isUpToDate = false this.backendWasNewer = true
return; return;
} }
} }
}) })
})
return isUpToDate // Updates the state
this.items = response.data
})
}
else this.backendWasNewer = false
}, },
/** /**
@ -148,8 +151,6 @@ export const useTwofaccounts = defineStore({
}) })
useNotifyStore().success({ text: trans('twofaccounts.accounts_deleted') }) useNotifyStore().success({ text: trans('twofaccounts.accounts_deleted') })
}) })
//this.refresh()
} }
}, },

View File

@ -37,7 +37,7 @@
isAdmin: response.data.is_admin, isAdmin: response.data.is_admin,
}) })
router.push({ name: 'accounts', params: { toRefresh: true } }) router.push({ name: 'accounts' })
}) })
.catch(error => { .catch(error => {
if( error.response.status === 401 ) { if( error.response.status === 401 ) {

View File

@ -1,5 +1,4 @@
<script setup> <script setup>
import twofaccountService from '@/services/twofaccountService' import twofaccountService from '@/services/twofaccountService'
import TotpLooper from '@/components/TotpLooper.vue' import TotpLooper from '@/components/TotpLooper.vue'
import GroupSwitch from '@/components/GroupSwitch.vue' import GroupSwitch from '@/components/GroupSwitch.vue'
@ -53,15 +52,16 @@
}) })
onMounted(async () => { onMounted(async () => {
// This SFC is reached only if the user has some twofaccounts in the store (see the starter middleware). // This SFC is reached only if the user has some twofaccounts (see the starter middleware).
// This allows to display accounts without latency. // This allows to display accounts without latency.
// We now check the twofaccounts store state in case the backend data have changed. //
const isUpToDate = await twofaccounts.isUpToDateWithBackend() // We sync the store with the backend again to
if (! isUpToDate) { twofaccounts.fetch().then(() => {
await twofaccounts.fetch() if (twofaccounts.backendWasNewer) {
notify.info({ text: trans('commons.data_refreshed_to_reflect_server_changes'), duration: 10000 }) notify.info({ text: trans('commons.data_refreshed_to_reflect_server_changes'), duration: 10000 })
} }
//groups.refresh() })
groups.fetch()
}) })
/** /**
@ -70,7 +70,7 @@
function postGroupAssignementUpdate() { function postGroupAssignementUpdate() {
// we fetch the accounts again to prevent the js collection being // we fetch the accounts again to prevent the js collection being
// desynchronize from the backend php collection // desynchronize from the backend php collection
fetchAccounts(true) twofaccounts.fetch()
notify.success({ text: trans('twofaccounts.accounts_moved') }) notify.success({ text: trans('twofaccounts.accounts_moved') })
} }