Dashboard-Ladezeit durch geteilte Datenabfragen optimiert
This commit is contained in:
54
frontend/composables/useDashboardData.ts
Normal file
54
frontend/composables/useDashboardData.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
type DashboardCoreData = {
|
||||
createdDocuments: any[]
|
||||
incomingInvoices: any[]
|
||||
}
|
||||
|
||||
const CACHE_TTL_MS = 30_000
|
||||
|
||||
type DashboardCacheEntry = {
|
||||
cachedAt: number
|
||||
data: DashboardCoreData | null
|
||||
pendingRequest: Promise<DashboardCoreData> | null
|
||||
}
|
||||
|
||||
const tenantCaches = new Map<string, DashboardCacheEntry>()
|
||||
|
||||
/**
|
||||
* Bündelt die großen, von mehreren Dashboard-Karten benötigten Abfragen.
|
||||
* So werden identische Requests beim parallelen Mounten nur einmal ausgeführt.
|
||||
*/
|
||||
export const useDashboardData = () => {
|
||||
const auth = useAuthStore()
|
||||
|
||||
const getTenantKey = () => String(auth.activeTenant || auth.activeTenantData?.id || "default")
|
||||
|
||||
const loadCoreData = async (force = false): Promise<DashboardCoreData> => {
|
||||
const tenantKey = getTenantKey()
|
||||
const cache = tenantCaches.get(tenantKey) || { cachedAt: 0, data: null, pendingRequest: null }
|
||||
tenantCaches.set(tenantKey, cache)
|
||||
const cacheIsFresh = cache.data && Date.now() - cache.cachedAt < CACHE_TTL_MS
|
||||
|
||||
if (!force && cacheIsFresh) return cache.data
|
||||
if (!force && cache.pendingRequest) return cache.pendingRequest
|
||||
|
||||
cache.pendingRequest = Promise.all([
|
||||
useEntities("createddocuments").select(),
|
||||
useEntities("incominginvoices").select()
|
||||
])
|
||||
.then(([createdDocuments, incomingInvoices]) => {
|
||||
cache.data = {
|
||||
createdDocuments: createdDocuments || [],
|
||||
incomingInvoices: incomingInvoices || []
|
||||
}
|
||||
cache.cachedAt = Date.now()
|
||||
return cache.data
|
||||
})
|
||||
.finally(() => {
|
||||
cache.pendingRequest = null
|
||||
})
|
||||
|
||||
return cache.pendingRequest
|
||||
}
|
||||
|
||||
return { loadCoreData }
|
||||
}
|
||||
Reference in New Issue
Block a user